cyberpirate92
cyberpirate92

Reputation: 3166

How to redirect stderr to a file and some echos (not stdout) to the same file

I am learning bash scripting. I am trying to write a simple script that runs apt-get update in regular intervals using crontab for scheduling. I am trying to send the errors (with the time the error occurred )to a log file but I am only getting the error and not the time,

The script file ,

echo "----------   $(date +"%d %B %G") , $(date +%r) :";
apt-get update;

The crontab file (sudo crontab -e)

0 */5 * * * /root/scripts/update_sys.sh 2>/root/logs/updateLog 1>/dev/null

as far as I understood the echo statement in the script file dumps the text to the standard output, thus it ends up in /dev/null, but how could I include it in the log file without the entire info ( I mean info that apt-get dumps like Hit http://in.archive.ubuntu.com precise-backports/main Sources ) ?

Upvotes: 1

Views: 133

Answers (1)

alvits
alvits

Reputation: 6758

If you can modify the script but not the crontab then the simplest is to redirect the echoes in the script to stderr.

echo "----------   $(date +"%d %B %G") , $(date +%r) :" >&2

All stdout will still go to /dev/null and all stderr will be logged along with the echoes to stderr.

If you can modify the crontab but not the script, then the simplest would be devnull's suggestion.

If you can modify either, then you have a choice.

Upvotes: 2

Related Questions