mowwwalker
mowwwalker

Reputation: 17382

What does this cronjob do?

I have some sort of learning block for cron, and no matter what I read, I can never get a get understanding of it. I asked for help from my webhost to create a cron job that runs a python script every two hours.

This is what he sent back:

0 */2 * * * python /path/to/file.py >> /dev/null 2>&1

I get that the first bit is saying everyone hour evenly divisible by two, the second part is using python to execute my file, and the rest, I don't really know.

The support guy sent me an email back saying

That means that stdout and stderr will be redirected nowhere to keep you clean of garbled messages, and command outputs if any (useful and common in cron).

To test script functionality, use the same without redirection.

Which makes sense, because I remember >> being used in the command prompt to write output to files. I still don't get two things though. First, what does 2>&1 do? And second, by redirection, is he talking about sending the output to /dev/null? If it didn't go there, and I did want to confirm it was working, where would it go?

Upvotes: 0

Views: 220

Answers (4)

dcp
dcp

Reputation: 55458

2 represents the stderr stream, and it's saying to redirect it to same place that stream 1 (stdout) was directed, which is /dev/null (sometimes referred to as the "bit bucket").

If you didn't want the output to go to /dev/null, you could put, for example, a filename there, and the output of stderr and stdout would go there.

Ex:

0 */2 * * * python /path/to/file.py >> your_filename 2>&1

Finally, the >> (as opposed to >) means append, so in the case of a filename, the output would be appended instead of overwriting the file. With /dev/null, it doesn't matter though since you are throwing away the output anyway.

Upvotes: 2

jimw
jimw

Reputation: 2598

  • >> is unnecessary there - /dev/null isn't a real file, it doesn't matter whether you use > or >>
  • 2>&1 means send STDERR to the same place as STDOUT, i.e. /dev/null
  • The man page for cron explains what it does if you don't have the redirect; in general, it emails the admin with the output.
  • If you wanted to check it was working, you'd replace '/dev/null' with an actual file' say '/tmp/log', and check that file. This is why there's a >> in the command: when logging to a real file, you want to append each time rather than overwriting it.

Upvotes: 1

Jonathan Leffler
Jonathan Leffler

Reputation: 754530

The >> appends standard output to /dev/null; the 2>&1 sends file descriptor 2 (standard error) to the same place that file descriptor 1 (standard output) is going.

The append is unusual but not actually harmful; you'd normally just write >. If you were dealing with real files instead of /dev/null, the append is probably better, so this reduces the chances of running into problems when you change from /dev/null to /tmp/cron/job.log or whatever.

Throwing away errors is not necessarily a good idea, but if the command is 'chatty', that output will typically end up in an email to the user whose cron job it is.

Upvotes: 0

erikxiv
erikxiv

Reputation: 4075

2>&1 redirects all error output to the same stream as the standard output (e.g. in your case to /dev/null = nowhere)

If you run python /path/to/file.py in a console window (e.g. removing the output redirection starting with >>) the output will be printed on your console (so you can read it visually)

Note: By default the output of cron jobs will be sent as an e-mail to the user owning the job. For that reason it is very common to always direct standard and error output to /dev/null.

Upvotes: 2

Related Questions