Systemd Execstart

时间:2017-08-17 17:16:10

标签: python systemd

我有一个Python脚本,我想作为守护进程运行。我是通过创建单元文件/etc/systemd/system/service

来做到这一点的

我希望将其作为systemctl start/stop/restart myservice

运行

依赖于此start stop个参数,我正在处理系统信号,例如SIGUPSIGINT

问题是我可以运行我的脚本,因为Python main.py启动/停止/重启并且我的逻辑有效。

但是在转换成单元文件后,这个python文件被ExecStart调用,我不知道如何在那里传递参数?

    [Unit] 
    Description=This service monitors docker daemon for events
    After=multi-user.target

    [Service]
    Type=simple
    ExecStart=/home/PycharmProjects/python_tests/service-discovery/utils/auto_registeration_script/main.py
    User=root
    WorkingDirectory=/home/PycharmProjects/python_tests/service-discovery/utils/auto_registeration_script/
    Restart=on-failure

    [Install]
    WantedBy=multi-user.target

1 个答案:

答案 0 :(得分:0)

不是你实际上正在运行" main.py start"和" main.py停止"?在那种情况下,你编写了一个"分叉"服务。

[Unit] 
Description=This service monitors docker daemon for events
After=multi-user.target

[Service]
Type=forking
Environment=script=/home/PycharmProjects/python_tests/service-discovery/utils/auto_registeration_script/main.py
ExecStart=$script start
ExecStop=$script stop
User=root
WorkingDirectory=/home/PycharmProjects/python_tests/service-discovery/utils/auto_registeration_script/
Restart=on-failure

[Install]
WantedBy=multi-user.target