[英]running python script as a systemd service
I have a python script myScript.py
which is writing on a file every 2 second.我有一个 python 脚本
myScript.py
,它每 2 秒写入一个文件。 But when I want to run this script as a systemd
service, service works but not writing on file.但是当我想将此脚本作为
systemd
服务运行时,服务可以工作但不能写入文件。
I created a myscript.service
file on /lib/systemd/system/
and designed as below:我在
/lib/systemd/system/
上创建了一个myscript.service
文件,设计如下:
[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:而
myScript.py
是:
import time
while True:
with open("/home/pala/Documents/file.txt", "a") as myFile:
myFile.write("--**--")
time.sleep(2)
service
from your code:service
的过程: At first, add the following shebang in above of your_script.py
:首先,在
your_script.py
上面添加以下shebang :
#!/usr/bin/env python
I use the following instruction for my own services creation:我使用以下说明创建自己的服务:
Suppose your service name is "test" , then create files below:假设您的服务名称是"test" ,然后在下面创建文件:
[Unit]
SourcePath=/etc/init.d/test
[Service]
ExecStart=/etc/init.d/test start
ExecStop=/etc/init.d/test stop
#!/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:然后我为上述配置创建一个安装:
#!/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"
Set the access permission to your_script.py
file:设置对
your_script.py
文件的访问权限:
$ 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.然后使用
systemctl
触发服务或根据需要重新启动您的机器。
Then start your service:然后启动你的服务:
$ sudo service test start
You can check its status:您可以检查其状态:
$ sudo service test status
[ NOTE ]: [注意]:
test
, Your_User_Name
, Your_path
and your_script.py
static variables with your variables in the above scripts.test
、 Your_User_Name
、 Your_path
和your_script.py
静态变量替换为上述脚本中的变量。maybe it helps to add a Working directory at myscript.service:也许它有助于在 myscript.service 添加一个工作目录:
[Service]
(...)
WorkingDirectory=/home/pi/your_working_directory
Best Regards Kilian最好的问候基利安
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.