Possible duplicate:
How to write a bash script to restart the process if it dies?
I made a C program that sometimes crashes and I can’t fix it (some problem with getaddrinfo, which is pretty spontaneous). I would like to restart the program after a crash. I thought it would be easy. I was going to separate the problematic libcurl code with the plug and see how I can detect the process from closing so that it can be forked again. However, I went with the “simple” ability to try to reload the entire program and restore the data from the file.
I tried this:
#!/bin/sh
while true; do
cd "~/ProgramDir"
exec "~/ProgramDir/Program"
done
But when the program crashes, it starts to output the next execution to the terminal input, if that makes sense. SO, if I pretend my program is just a Hello World program, then it will do something like this:
bash-3.2$ start.sh
Hello World!
Hello World!
bus error
bash-3.2$ Hello World!
-bash: Hello: command not found
bash-3.2$ Hello World!
-bash: Hello: command not found
He will not continue the program as before. The terminal considers that the program has exited, but then outputs the result of the next execution to the terminal input.
What is the right way to do this?
source
share