Score:3

What is the meaning of the "/dev/null 2>&1" in a Cronjob entry?

cn flag

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
Score:2
mx flag

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.

Score:-3
sh flag

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.

vidarlo avatar
ar flag
You repeat information that's in the answer by digijay.
Boolean_Type avatar
sh flag
@vidarlo, no, I'm not. digijay just says: "to avoid being spammed by lots of irrelevant messages from service scripts". Nothing about mail sending. And no explanation what are irrelevant messages and where they are if we have to avoid their appearance.
Boolean_Type avatar
sh flag
Sometime ago I've found digijay's answer, but I still couldn't understand, _what exactly_ we try to hide by redirecting, 'cause their answer focuses on the redirect, not the purpose of that redirect. I've figured it out and decided to share it with community.
digijay avatar
mx flag
@Boolean_Type I didn't mention this because it was not part of the question. Thus, there is no reason to downvote my answer.
Boolean_Type avatar
sh flag
@digijay I understand, I just wanted to expand your answer and add some additional info. It's shared and normal practice on Stackoverflow, but here my answer was downvoted as like what I wrote is not a correct info. ¯\\(ツ)/¯ And no, I didn't downvote your answer.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.