[HELP] what does "2>&1" do in crontab?

Hi Experts,

I would like to know, what does "2>&1" do in crontab?

example:

          • /export/user/home/test.sh >> /export/user/home/logtest.log 2>&1

My colleague told me the commabd 2>&1 mean to prevent crontab sending an email (sendmail on /var/mail/root) when the script failed. So, if error occur when the crontab script failed, the email will dump to logtest.log not on /var/mail/root.
Is it true?

Why system always sending email if crontab script failed? and with adding command 2>&1 can solve the problem?

Your colleague is correct. The 2>&1 just redirects Channel 2 (Standard Error) and Channel 1 (Standard Output) to the same place which in this context is Channel 1 (Standard Output), and thence your log file.
If all output from a cron is redirected to a file, it will not generate an email of the output to Stdout or Stderr.
Hope this helps.

Hi methyl,

Thanks for your reply.

I have tested, if the output error rc=1, it will not generate an email of the output, but redirected to a file.

If the output warning (rc=4) can be redirected to a file?
Could you give me the simple scenario for output warning, please?

By adding "2>&1" is it the best choice for redirect output cron to a file?

Thanks
Edy

If you want to do the redirection on the crontab line this the best solution based on the information supplied.

It would really help if you posted what Operating System and version you are running (okay we know that is is a Solaris) and what Shell you are using when in crontab. Under Solaris the Shell under cron can be an ancient Bourne Shell.

Personally I would redirect the two output chanels to different files.

2>error_log.txt 1>normal_log.txt

Really depends on the application.

1 Like

Yes, i used Solaris 10. The scripts just moving text file to another directory with specific pattern.

If i modify the script became :
2>error_log.txt 1>normal_log.txtWhen the warning info occur, will be redirected to error_log.txt or normal_log.txt ?

Just basic question, how to include time stamp? But when i have tested, time stamp cannot include.

Thanks
Edy