Hi fenor and welcome to the LQ forums!
1/ What program are we talking about? A background service ("daemon")? A regular user program?
The reason I ask is because there are mechanisms in place (like PIDs in /var/run, /etc/inittab) that can
help restart programs automatically when they unexpectedly stop.
2/ In the pipeline
count=`ps aux|grep programtostart|wc -l`
the grep process you start can appear in the output of the "ps" command, then gets through the grep and into wc (thus providing a false match).
Your debug.txt file should show you this.
Bottom line, you'll need to filter more than just grep'ing the program's name.
Edit: it seems druuna provides the solution to this one.
3/ Another important aspect could be logging: does the program write any output to a file?
And how is it started up? Does the shell that starts it up wait for the program to finish (to log exit code and then restart it),
or is it started in background?