0

I have a program that must work forever until kill -HUP command. But sometime this program can exit with an error code or it can be killed by OS for overusing memory.

Is there any standard linux command which can monitor any command and restart them on exit?

Something like this: forever my-command -with some parameters

It can be done via bash script:

#!/bin/bash

while true
do
  my-command -with some parameters
done

, but I'd better to use somethings standard rather than writing my own scripts.

3
  • 2
    They are multiple ways to do this. Usually you have some other process that monitors your program and restarts it as needed. systemd and other init systems provide this feature on the system level, you only need to write the proper configuration files.
    – dirkt
    Jun 20, 2020 at 10:10
  • 1
    A systemd service sounds sensible. But if your program is "overusing memory" or crashing for some reason you should fix it rather than work around the issues.
    – Kate
    Jun 20, 2020 at 13:31
  • 1
    Your bash example restarts the command even after kill -HUP... Jun 20, 2020 at 14:33

1 Answer 1

2

This is a SystemD unit file which would restart the command until it is killed by SIGHUP. If the program shall be allowed to exit with a success exit code then use Restart=on-failure instead.

[Unit]
Description=A program

[Service]
Type=simple
ExecStart=/path/to/my-command -with some parameters
Restart=always
# Restart=on-failure
RestartPreventExitStatus=SIGHUP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.