0

I have a script that runs infinitely at startup, and another script that shuts it down safely. The second script requires connection to a remote database. They are both declared in the same service as ExecStart and ExecStop respectively. I need the second script to block shutdown/reboot until it completes. Currently the first script works fine but the second script is terminated early. Here is what I have so far:

[Unit]
DefaultDependencies=no
Wants=network-online.target
After=network.target network-online.target
Before=reboot.target shutdown.target halt.target

[Service]
Type=oneshot
RemainAfterExit=true
User=test
ExecStart=/usr/bin/python /home/test/test.py
ExecStop=/usr/bin/sh /home/test/test
KillMode=none

[Install]
WantedBy=multi-user.target

I tried using the following: systemd to wait for command to complete before restart/shutdown or killing other processes but it did not solve my problem, and it seems like his are commands that run relatively quickly.

Mocking
  • 131
  • 1
  • 8
  • You say you have a script that runs infinitely - you refer to the python script `/home/test/test.py` here? If it does indeed run infinitely (does not terminate) then I'd question the use of Type=oneshot here, which in turn, and in conjunction with the RemainAfterExit=true directive may influence the behaviour of the ExecStop command you are experiencing. – Benjamin Apr 21 '16 at 12:17
  • @Benjamin It didn't make a difference for the problem I was trying to solve, but you're right, it doesn't make sense to make it a oneshot. – Mocking Apr 21 '16 at 19:14

1 Answers1

2

I added the following and it worked:

TimeoutSec=300min

My final configuration is:

[Unit]
DefaultDependencies=no
Wants=network-online.target
After=network.target network-online.target
Before=reboot.target shutdown.target halt.target

[Service]
Type=simple
RemainAfterExit=true
User=test
ExecStart=/usr/bin/python /home/test/test.py
ExecStop=/usr/bin/sh /home/test/test
KillMode=none
TimeoutSec=300min

[Install]
WantedBy=multi-user.target

Note: I purposely did not make the TimeoutSec=infinity

Mocking
  • 131
  • 1
  • 8