'Adding time stamp to log file in bash script
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?
Solution 1:[1]
Possible reason is different paths for date in terminal and sh: try using full path to the date which you use directly from command line, i.e. $(/bin/date)
Solution 2:[2]
As mentioned by @{fedorqui 'SO stop harming'} using >> appends to the end of the log file.
Alternatively, this will also limit the log to 1MB:
tail -c 1MB fail.log; echo $(date) run script > file.log
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Wintermute |
| Solution 2 | ntg |
