Control python script like linux cli applicaitons

Viewed 97

I have a python script which I want to control using linux commands. e.g. like we control mysql: service mysql restart

How to achieve this kind of functionality. I saw some bash scripts doing this but don't have any knowledge in bash scripting. Thanks.

UPDATE:

I have a site_monitor.py script which I included in site_monitor.service in /etc/systemd/system/.

[Unit]
Description=Site Monitor Service
After=multi-user.target

[Service]
Type=simple
Restart=always
ExecStart=/usr/bin/python3 /home/hemantsah/WisdomLeaf/site_monitor/site_monitor.py

[Install]
WantedBy=multi-user.target

After doing systemctl daemon-reload.service, I started the script using systemctl start site_monitor.service

Listing all the services using systemctl list-units --type=service, I can see the service running, but it's not doing anything.

If I run my python script in terminal using python3 site_monitor.py, then it works.

I found just now if I start the service and check the status using sudo service site_monitor status, I checked after starting the service, it was fine , checking after sometime again gave me this error:

hemantsah@pop-os:/etc/systemd/system$ sudo service site_monitor status
ā— site_monitor.service - Site Monitor Service
     Loaded: loaded (/etc/systemd/system/site_monitor.service; disabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Thu 2021-11-18 10:47:30 IST; 16s ago
    Process: 111989 ExecStart=/usr/bin/python3 /home/hemantsah/WisdomLeaf/site_monitor/site_monitor.py (code=exited, status=1/FAILURE)
   Main PID: 111989 (code=exited, status=1/FAILURE)

Nov 18 10:47:30 pop-os systemd[1]: site_monitor.service: Scheduled restart job, restart counter is at 5.
Nov 18 10:47:30 pop-os systemd[1]: Stopped Site Monitor Service.
Nov 18 10:47:30 pop-os systemd[1]: site_monitor.service: Start request repeated too quickly.
Nov 18 10:47:30 pop-os systemd[1]: site_monitor.service: Failed with result 'exit-code'.
Nov 18 10:47:30 pop-os systemd[1]: Failed to start Site Monitor Service.
Nov 18 10:47:34 pop-os systemd[1]: site_monitor.service: Start request repeated too quickly.
Nov 18 10:47:34 pop-os systemd[1]: site_monitor.service: Failed with result 'exit-code'.
Nov 18 10:47:34 pop-os systemd[1]: Failed to start Site Monitor Service.

Running sudo journalctl -u site_monitor.service gave me following error:

Nov 18 10:10:37 pop-os systemd[1]: site_monitor.service: Scheduled restart job, restart counter is at 4.
Nov 18 10:10:37 pop-os systemd[1]: Stopped Site Monitor Service.
Nov 18 10:10:37 pop-os systemd[1]: Started Site Monitor Service.
Nov 18 10:10:37 pop-os python3[111023]: Traceback (most recent call last):
Nov 18 10:10:37 pop-os python3[111023]:   File "/home/hemantsah/WisdomLeaf/site_monitor/site_monitor.py", line 3, in <module>
Nov 18 10:10:37 pop-os python3[111023]:     from bs4 import BeautifulSoup
Nov 18 10:10:37 pop-os python3[111023]: ModuleNotFoundError: No module named 'bs4'
Nov 18 10:10:37 pop-os systemd[1]: site_monitor.service: Main process exited, code=exited, status=1/FAILURE
Nov 18 10:10:37 pop-os systemd[1]: site_monitor.service: Failed with result 'exit-code'.
1 Answers

I guess you already have systemd in your machine, but in case you don't, you can install it via package manager, e.g. apt:

sudo apt-get install systemd

You can then, create your own systemd service. To do so, just create a new file in /etc/systemd/systemd/, something like /etc/systemd/systemd/your_service_name.service. That file should look like this:

[Unit]
Description= My service
After=multi-user.target

[Service]
Type=simple
WorkingDirectory=/path/to/your/working/dir/
User=<user>
Restart=always
ExecStart=/usr/bin/python3 /path/to/your/script/<script_name>.py

[Install]
WantedBy=multi-user.target

Of course you can use different python 3 binaries instead of /usr/bin/python3. Also, the service configuration itself can be different, the example above is just a basic service structure.

After creating this file (with root permissions), you should reload the daemon with:

sudo systemctl daemon-reload

And if you want to keep your script enabled even if the server/machine restarts, run:

sudo systemctl enable your_service_name.service

Finally, you can start your service using the following:

sudo systemctl start your_service_name.service
Related