1748246 Members
3630 Online
108760 Solutions
New Discussion юеВ

Re: Help with Crontab

 
Karthik_sg
Frequent Advisor

Help with Crontab

hi all i am using the follwing code in my crontab
* * * * * /root/a.sh &> /dev/null
Exactly wht is this &> used for......Pls explain
6 REPLIES 6
Jeeshan
Honored Contributor

Re: Help with Crontab

It running in background. and store the temporary result in /dev/null
a warrior never quits
Ralph Grothe
Honored Contributor

Re: Help with Crontab

I rather assume this is a typo for what probably should read something like

* * * * * /root/a.sh >/dev/null 2>&1

Why would you want to run a cronjob in the background?
Besides, should this job really run every minute?

Madness, thy name is system administration
Ralph Grothe
Honored Contributor

Re: Help with Crontab

Forgot to mention,
Linux distros are using the extended Vixie crontab format.
There you can place environments in a crontab,
and especially setting MAILTO here to an empty string should suppress the sending of the cronjobs output as mail to the owner of the cronjob; something one usually tries to achieve by redirecting stdout and stderr to /dev/null
(with conventional implementations of cron)
So you could place this line in the top of your crontab:

MAILTO=""
Madness, thy name is system administration
Karthik_sg
Frequent Advisor

Re: Help with Crontab

we are monitoring our devices ,so it is to run every minute.but none of you explain the significance of &> .I do know that it does not issue mails.But is it running the crontab in background or something...thnks for the replies...
Ralph Grothe
Honored Contributor

Re: Help with Crontab

The appended ampersand to a statement
means that the flow of following commands
(n.b. shell scripts basically aren't much more than a batch of commands/program calls)
won't be blocked until the execution of the current command has finished or is interrupted.
It can be used as a kind of forking to achieve asynchronous execution behavior.
I guess this was the intention here
as the by minute cronjob interval might be too narrow for the called script to have finished before the next cronjob is due, since no other command immediately follows the call.
With interactive shells that support job control the ampersand is used to regain the shell prompt to continue work while a probably long-running command is being executed in the background.
Madness, thy name is system administration
Jason Sibre
New Member

Re: Help with Crontab

I know I'm resurrecting a long dead thread, but I stumbled across this while searching for something related. As none of the above answers are correct, I figured I'd answer for the benefit of future searchers.

Ralph's comments are nearly correct, but it isn't a typo, and it isn't backgrounding the process, either.

Usually an "&" appended to the end of a command will cause that command to run in the background, but in this particular case, the "&> [file]" is a shorthand notation for the " >[file] 2>&1" notation (Ralph was close). Not a typo. It does NOT cause it to run in the background. This shorthand is recognized by bash, and probably other shells, also.

What these two variations do is redirect both stdout and stderr to the file specified. In this case, /dev/null, aka the bit bucket.

For more information, see the man page for bash, or whatever shell you found this being used in.