running python script as a systemd service

我们两清 提交于 2019-11-30 17:17:23

问题


I have a python script myScript.py which is writing on a file every 2 second. But when I want to run this script as a systemd service, service works but not writing on file.

I created a myscript.service file on /lib/systemd/system/ and designed as below:

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

[Service]
Type=idle
ExecStart=/usr/bin/python /home/pala/PycharmProjects/myScript.py

[Install]
WantedBy=multi-user.target

and myScript.py is:

import time
while True:

    with open("/home/pala/Documents/file.txt", "a") as myFile:
        myFile.write("--**--")

    time.sleep(2)

回答1:


This is the procedure of creating a service from your code:

At first, add the following shebang in above of your_script.py:

#!/usr/bin/env python

I'm using the following instruction for my own services creation:

Suppose your service name is "test", then create the below files:

test.service

[Unit]
SourcePath=/etc/init.d/test
[Service]
ExecStart=/etc/init.d/test start
ExecStop=/etc/init.d/test stop

test.sh

#!/usr/bin/env bash

# Quick start-stop-daemon example, derived from Debian /etc/init.d/ssh
set -e

# Must be a valid filename
NAME=this_is_a_test
PIDFILE=/var/run/$NAME.pid
#This is the command to be run, give the full pathname
DAEMON=/home/Your_User_Name/Your_path/your_script.py

case "$1" in
  start)
        echo -n "Starting daemon: "$NAME
    start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_OPTS
        echo "."
    ;;
  stop)
        echo -n "Stopping daemon: "$NAME
    start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
        echo "."
    ;;
  restart)
        echo -n "Restarting daemon: "$NAME
    start-stop-daemon --stop --quiet --oknodo --retry 30 --pidfile $PIDFILE
    start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_OPTS
    echo "."
    ;;

  *)
    echo "Usage: "$1" {start|stop|restart}"
    exit 1
esac

exit 0

Then I create an installation for the above configuration:

install.sh

#!/usr/bin/env bash

echo "create a test service ..."
cp test.sh /etc/init.d/test
cp test.service /etc/systemd/system
chmod +x /etc/init.d/test
# sed -i "s/Your_User_Name/you_path/g" /etc/init.d/test
echo "created the test service"

Finally, do:

Set the access permission to your_script.py file:

$ chmod 755 <your_script.py>

Then install the service with:

$ sudo bash ./install.sh

Then trigger the service with systemctl or restart your machine if needed.

Then start your service:

$ sudo service test start

You can check its status:

$ sudo service test status

[NOTE]:

  • Replace the test, Your_User_Name, Your_path and your_script.py static variables with your variables in the above scripts.
  • Difference between a service and systemctl



来源:https://stackoverflow.com/questions/42735934/running-python-script-as-a-systemd-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!