5

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

2 Answers 2

4

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.

-3

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.

5
  • You repeat information that's in the answer by digijay.
    – vidarlo
    Feb 4, 2023 at 22:07
  • @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. Feb 4, 2023 at 22:26
  • 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. Feb 4, 2023 at 22:30
  • @Boolean_Type I didn't mention this because it was not part of the question. Thus, there is no reason to downvote my answer.
    – digijay
    Jun 20, 2023 at 15:07
  • @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. Jun 21, 2023 at 13:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .