I have a process which is executed by init.d script on the background. Eg:

case "$1" in 
       /bin/myprocess &
       killall myprocess
       killall myprocess
       /bin/myprocess &

In certain conditions, myprocess can fail and return. Is there any (standard) way how to detect its fail and restart in automatically?

  • Sure, but it varies based on distribution. Pretty much all of them provide some kind of service manager. Nov 19, 2012 at 9:39
  • There is no standard distribution, but buildroot. So I have to do it manually...
    – Honza
    Nov 19, 2012 at 12:24

9 Answers 9


The easiest way would be to add it to /etc/inittab, which is designed to do this sort of thing:

respawn If the process does not exist, start the process. Do not wait for its termination (continue scanning the /etc/inittab file). Restart the process when it dies. If the process exists, do nothing and continue scanning the /etc/inittab file.

For example, you could do this:

# Run my stuff
  • 4
    Note, /etc/inittab works (or even exists) if and only if you have a sysvinit-based init system. With upstart and with systemd it doesn't. You have to install either busybox (very primitive shell making the sysadm recover tasks painful, but it can substitute a sysvinit-compatible initd) or sysvinit (it is a fossil). In a docker container, only the first is not painful.
    – peterh
    Jul 5, 2017 at 11:02

Buildroot has three possible init systems, so there are three ways to do this:

BusyBox init

With this, one adds an entry to /etc/inittab.


Note that BusyBox init has an idiosyncratic /etc/inittab format. The second field is meaningless, and the first field is not an ID but a device basename.

Linux "System V" init

Again, one adds an entry to /etc/inittab.



One writes a unit file in, say, /etc/systemd/system/myprocess.service:

Description=My Process



Enable this to autostart at bootup with:

systemctl enable myprocess.service

Start it manually with:

systemctl start myprocess.service

Further reading

  • but when you use this approach inittab then your process is no longer accessible via the 'service' interface right? i.e you can't go service mything start or service mything stop anymore....is there a way to have the best of both? i.e uncrashable sysvinit service, but also have it usable via 'service' ?
    – horseyguy
    Apr 26, 2019 at 9:02
  • Running systemd, so the obvious choice I would have is #3 above. Yet for some unknown reason, my nohup ffmpeg ... & daemon won't save its files when launched this way, so I have to start it manually for it to work. Out of 4 similar daemons, rarely do all four continue 24 hours. Usually one fails for unknown reason[s] and sometime two, meaning I really need this auto-restart. I'm upvoting one of the options below until I should find it doesn't work.
    – kenneth558
    Oct 21, 2020 at 5:31

What about creating a subshell with a loop that calls constantly the same process?

If it ends, the next iteration of the loop goes on and starts it again.

(while true; do 
done) &

If the subshell dies, it's over though. The only possibility in that case would be to create another process (I'll call it necromancer) that checks whether yourprocess is alive, start it if it isn't and run this necromancer with cron, so that you can check that regularly.

Next step would be wondering what could happen if cron dies, but at some point you should feel safe and stop worrying.


You could make use of Monit . It's really easy to use and quite flexible. See for example this configuration for restarting the Tomcat process on failure.

check process tomcat with pidfile /var/run/tomcat.pid
   start program = "/etc/init.d/tomcat start"
   stop  program = "/etc/init.d/tomcat stop"
   if failed port 8080 type tcp then restart

It also has a lot of configuration examples for many use cases.


You may use restarter

   restarter -c /bin/myprocess &
   pkill -f myprocess

On newer systems use systemd which solves all those trivial issues


If you're not a super user or root, and if your Linux system has Docker installed, then you can create a docker image of your process, using docker to restart your process if the system is rebooted.

File: docker-compose.yml

version: "3"
    image: your_docker_image:latest
    - 8080:8080   # just use 8080 as an example
    restart: always  # this is where your process can be guaranteed to restart

To start your docker container,

docker-compose up -d

I find it's easy to handle my-own-process with auto-restart if I am not a super user of the system.

For a sample example of how to create a docker image, here is a quick example:

File: Dockerfile

FROM alpine:3.5

RUN apk update && apk upgrade && rm -rf /var/cache/apk/*
COPY my-process-server /app
RUN ln -s /app/my-process-server /usr/local/bin/my-process-server


CMD ["my-process-server"]
  • He is asking about init.d scripts. Adding docker on top is really not the tool to auto restart a daemon. It's like automating a computer power cycle if the daemon stops. Jun 8, 2021 at 5:37
  • init.d scripts aren't available to non-root users. This one is prefaced by being a possibility for those who do not have that access. A cron job to run testing scripts would work as well though. Sep 27, 2022 at 16:09

In my case, as a quick-fix, I modified and used the solution of @Trylks to wrap the program I was launching. I wanted it to end only on clean exit.

Should run in most shells:


echo ""
echo "Use: $0 ./program"
echo ""

#eg="/usr/bin/apt update"

echo "Executing $1 ..."

(while [ $EXIT_CODE -gt 0 ]; do
    # loops on error code: greater-than 0
done) &

(Edit): Sometimes programs hang without quitting, for no apparent reason. (Yes, of course there's always a reason but it can take a lot of time and effort to find it, particularly if it's not your own code.)

The problem I had was that the process (a Python server) was hanging from time to time, so I needed to regularly kill and restart it. I did it with a cron task that runs every couple of hours. Here's the shell script:


# This cron script restarts the server
# Look for a running instance of myapp.py
p=$(ps -eaf | grep "[m]yapp.py")
# Get the second item; the process number
n=$(echo $p | awk '{print $2}')
# If it's not empty, kill the process
if [ "$n" ]
   kill $n
# Start a new instance
python3 myapp.py

  • How is this better than accepted answer?
    – Toto
    Mar 8 at 19:35
  • The accepted answer waits for the process has quit and then restarts it, but there are some cases where it doesn't quit but fails to continue working as it should. Using cron guarantees a restart no matter what.
    – Graham
    Mar 17 at 15:23

The process can be launched with until loop as it's been answered here: How do I write a bash script to restart a process if it dies?. Until loop works with the requirement to run it the background, see sample:

(until /bin/myprocess; do
    echo "'/bin/myprocess' crashed with exit code $?. Restarting..." >&2
    sleep 1
done) & 

Comparing to the @Trylks answer with while loop the last couldn't restart process when it's written in entrypoint.sh script of a Dockerfile. Despite it the until loop is working there too..

Your Answer

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

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