A memo when using systemd to keep a Python script running as a daemon on CentOS 7 at all times.


3/15 17:32 Addendum From here
You pointed out that / usr / lib / systemd / system / is where to put the services contained in the packages installed by the system. As you pointed out, it seems to be better to put it under / etc / systemd / system /, so the following has been corrected. Thank you for pointing out!
3/15 17:32 Addendum

Will be added later. For the time being, a real memo.

Create a unit file like the one below in / etc / systemd / system with the file name "xxxxx.service".

[Unit]
Description=Sample Hogehoge Script
[Service]
ExecStart=/home/hogemi/fuga.py hikisu
Restart=always
Type=simple
PIDFile=/var/run/fuga.pid
[Install]
WantedBy=multi-user.target

In the examples of some of the ancestors found, there were many that had Type = forking, but this is the behavior on the script side that "when started, it terminates itself and leaves a child". Set Type = simple for scripts that stay in the foreground.

If the above unit file was created as fuga.service,

systemctl start fuga

The service starts with.

In the unit file mentioned earlier, if Type = simple is not set, systemctl itself will not end when the type that stays in the foreground is started. If Type = simple, systemctl will end properly when the command is issued.

More on that later.

Recommended Posts

A memo when using systemd to keep a Python script running as a daemon on CentOS 7 at all times.
A note on using tab completion when running Python interactively on Windows
A memo with Python2.7 and Python3 on CentOS
A memo of a tutorial on running python on heroku
How to manage arguments when implementing a Python script as a command line tool
A convenient function memo to use when you want to enter the debugger if an error occurs when running a Python script.
A little trick to know when writing a Twilio application using Python on AWS Lambda
[Grasshopper] When creating a data tree on Python script
What's in that variable (when running a Python script)
Minimum memo when using Python on Mac (pyenv edition)
Summary of points to keep in mind when writing a program that runs on Python 2.5
A memo when creating a directed graph using Graphviz in Python
[GCP] A memorandum when running a Python program on Cloud Functions
Things to keep in mind when using Python with AtCoder
Things to keep in mind when using cgi with python.
When you want to hit a UNIX command on Python
I was a little addicted to installing Python3.3 + mod_wsgi3.4 on Sakura VPS (CentOS), so a retrospective memo
When I tried to scrape using requests in python, I was addicted to SSLError, so a workaround memo
Create a shell script to run the python file multiple times
Things to note when running Python on EC2 from AWS Lambda
How to build a Python environment using Virtualenv on Ubuntu 18.04 LTS
Determine if standard output is piped when running a Python script
Everything from building a Python environment to running it on Windows
Create a python environment on centos
Install Python on CentOS using Pyenv
Launch a Python script as a service
Build a python3 environment on CentOS7
Install Python on CentOS using pyenv
Things to watch out for when creating a Python environment on a Mac
When running a Python shell from Electron, pass multiple arguments to run Python.
A memo when setting up a Docker container for using JUMAN ++, KNP, python