Can someone explain to me what is the meaning of "2>&1" doing here in the below cron job
0 23 * * * wget -q -O /dev/null "https://example.com/index.php" > /dev/null 2>&1
It means that stderr
(2
- containing error messages from the executed command or script) is redirected (>&
) to stdout
(1
- the output of the command) and that the latter is being redirected to /dev/null
(the null device).
This way you can suppress all messages that might be issued by the executed command. In cron this is often done to avoid being spammed by lots of irrelevant messages from service scripts. Nevertheless be careful with this as some messages might be important.
More information on output redirects can be found in The Linux Documentation Project here and here. The null device is described here.
I would just like to expand on @digijay's answer a bit, explaining not "what > /dev/null 2>&1
does" but "why it's there".
1. Cron actions write appropriate data to different system files. For instance, popular destinations for logging cron errors include /var/log/cron
, /var/log/messages
and /var/log/syslog
.
2. By default cron jobs sends an email to the user account executing the cronjob.
So, to avoid cases 1 and 2 (you don't want neither to see any information / error logs, nor to get any emails from cron, you just want cron to work silently), you should redirect stderr
and stdout
to /dev/null
.