My script is as follows
if ps ax | grep -v grep | grep ./program > /dev/null
then
exit
else
echo "---------------------------------------------------" >> Debug.log
echo "Starting program at: $(date)" >> Debug.log
./program >> Debug.log 2>&1
fi
exit
Via crontab, this script is run every minute. It checks to see if a certain program is running, if it is, great, if not, starts it up.
Now I would like to append timestamps every time the script runs into Debug.log if it found ./program to be running. So under the then line, I added:
echo "Time: $(date)" >> Debug.log
This command does not output anything to Debug.log. It does work however directly from the command line. Why is that so, and can I remedy the problem?
date +"Time: %C" >>Debug.logwould seem to do what you want, probably more transparently and succinctly.grep thing | grep -v grepcan usually be refactored to something likegrep [t]hingwhere the idea is to use a regex which does not match itself.Debug.log, while you should indicate the full path of that file.