0

I an using Ubuntu 14.04. I have a script that is supposed to run at all times. The easy way would be to use crontab to run another script that checks if script1 is running and if not restart it.

I would like to avoid crontab and if possible any su command (I would like to run this without any additional settings as root). Also as root I have a script that cleans (kills) all processes once a day for the user I plan on running script1 from. I want to restart script1 after the cleanup and in between this interval if script1 stops.

2 Answers 2

1

Install the script as a systemd service. systemd will automatically restart it if it exits.

This isn't very difficult, you need to write a short something.service file (with Restarts=always somewhere in it), put/link it in the appropriate directory, and issue a couple of systemctl commands. See here for a tutorial.

PS: In 14.04 systemd is available but isn't used much by the system itself. It gets more pervasive in later releases.

8
  • Ubuntu 14.04 uses upstart, not systemd, I believe.
    – Kusalananda
    Jun 23, 2018 at 10:28
  • Not used for init, but still available AFAIK. And 14.04 is nearing EOL (<one year) so future viewers of the questions are likely to have even more systemd-friendly systems.
    – xenoid
    Jun 23, 2018 at 10:29
  • user@byga1:~$ systemctl --type=service bash: systemctl: command not found user@byga1:~$ Jun 23, 2018 at 11:08
  • And besides, as far as i know systemd is setup by root. I would have to add the script to run from the desired user, etc Jun 23, 2018 at 11:16
  • @CiprianTudor Fortunately it is not as far as you know. systemd has both a system level (which sets up the system and requires root permission) and a user level. Jun 23, 2018 at 11:22
1

Trivially a process can be started only by a running process. If all processes of the script account get killed then the solution can be outside the scope of this user only. But the two cases (killed by root vs. crashed / exited) can be handled differently.

restart after exit

That one is easy:

#! /bin/bash
while true; do /path/to/script.sh; done

restart after daily kill

  1. The kill script restarts the above wrapper script.
  2. A script running as a different user checks regularly whether the script is still running and restarts it if necessary. This does require sudo / su or a SUID binary, though, if this is not run by root.
1
  • Yes, i could this from the root account, but would like to avoid it. As root i do a kill -9 -1 for the user accout every midnight. Seen somewhere something start-stop daemon with pid file but have no ideea how to implement it. Also read about lock dir&file. Have no ideea if any survive after the process cleanup Jun 23, 2018 at 11:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .