0

I have a Python script which I want to run as a daemon. I am doing that by creating a unit file /etc/systemd/system/service

and I want to run it as systemctl start/stop/restart myservice

and depending on this start stop arguments I am handling system signals like SIGUP,SIGINT

Problem is i can run my script as Python main.py start/stop/restart and my logic works.

But after converting into a unit file this python file is invoked by ExecStart and I don't know how to pass arguments there?

    [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
3
  • 1
    What kind of arguments are you trying to pass? you could just append them to the ExecStart line if you know them beforehand, or use environment vars as another way of configuring the service Commented Aug 17, 2017 at 17:22
  • I wanna take start/stop/restart from CMD of systemctl and pass it to execstart as arguement to my python script so that my script can run based on the arguement came in Commented Aug 17, 2017 at 20:47
  • Can you please give me an example of how to do this @omu_negru Commented Aug 17, 2017 at 20:48

1 Answer 1

0

Isn't it that you are actually running "main.py start" and "main.py stop"? In that case you have programmed a "forking" service.

[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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.