【问题标题】:running python script as a systemd service将 python 脚本作为 systemd 服务运行
【发布时间】:2017-08-01 20:01:11
【问题描述】:

我有一个 python 脚本myScript.py,它每 2 秒写入一个文件。但是当我想将此脚本作为systemd 服务运行时,服务可以工作,但不能写入文件。

我在/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

myScript.py 是:

import time
while True:

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

    time.sleep(2)

【问题讨论】:

  • 你能发布sudo systemctl status myservice的输出吗?我试过这个,它使用我的家庭目录完美地工作。将服务文件添加到/lib/systemd/system 后,您是否运行了sudo systemctl daemon-reload?另外,是什么让您认为该服务有效?
  • 输出为:systemctl status myscript.service ● myscript.service - 我的脚本服务已加载:已加载(/lib/systemd/system/myscript.service;已禁用;供应商预设:已启用) 活动:活动(运行)自 Cts 2017-03-11 21:54:59 +03 起; 2min 7s 前 Main PID: 16614 (python) CGroup: /system.slice/myscript.service └─16614 /usr/bin/python /home/pala/PycharmProjects/myScript.py Mar 11 21:54:59 palaPC systemd[1 ]: 启动了我的脚本服务。
  • 服务工作没有任何问题,如您所见,但我的脚本必须每 2 秒创建和写入 file.txt。问题是:文件不是由服务创建的
  • 我写过“sudo systemctl daemon-reload”,但是没有用。重新加载计算机后,服务工作并创建了文件。感谢您所做的一切。
  • 您在创建服务文件后是否运行过sudo system systemctl start

标签: python ubuntu systemd


【解决方案1】:

也许在 myscript.service 添加一个工作目录会有所帮助:

[Service]
(...)
WorkingDirectory=/home/pi/your_working_directory

最好的问候 基连

【讨论】:

    【解决方案2】:

    这是从您的代码创建service 的过程:

    首先,在your_script.py的上面添加以下shebang

    #!/usr/bin/env python
    

    我使用以下说明创建自己的服务:

    假设你的服务名称是"test",那么在下面创建文件:

    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
    

    然后我为上面的配置创建一个安装:

    安装.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"
    

    最后,做:

    设置your_script.py文件的访问权限:

    $ chmod 755 <your_script.py>
    

    然后安装服务:

    $ sudo bash ./install.sh
    

    然后使用systemctl 触发服务或在需要时重新启动您的机器。

    然后启动你的服务:

    $ sudo service test start
    

    你可以查看它的状态:

    $ sudo service test status
    

    [注意]:


    【讨论】:

    • 这确实回答了“将 python 脚本作为启动服务运行”而不是“...作为 systemd 服务”的问题。这个问题可能会受到 XY 问题的影响,但恕我直言,现在转移话题还为时过早。
    • 投票失败,因为:OP 中的问题与查找 python 无关,因此 env python 是多余的。使用带有 systemd 的 init.d 样式的脚本比原始帖子更糟糕。使用 cp 而不是 install 来安装可执行文件是不好的风格。对于脚本来说,它并不像对于实际编译的程序那样糟糕,但是使用 install 会更好。
    • @SamHartman shebang 很重要,否则service test status 中有错误。这个问题我回答的很好,如果你有答案,请放上去。
    • @Benyamin Jafari 我投票决定关闭。我认为回答并投票结束同一个问题是不合适的。
    猜你喜欢
    • 1970-01-01
    • 2015-10-09
    • 1970-01-01
    • 1970-01-01
    • 2021-06-21
    • 1970-01-01
    • 2018-04-06
    • 2019-08-19
    • 2013-03-30
    相关资源
    最近更新 更多