1834391 Members
1722 Online
110066 Solutions
New Discussion

Re: How to use crontab

 
SOLVED
Go to solution
Henry Chua
Super Advisor

How to use crontab

Hi,

Can you guys help me with this.. I would like to know what does the "2>&1" does in this line of cron job.
"
5 0 * * * $HOME/bin/daily.job >> HOME/tmp/out 2>&1 "

In addition, if I would like to run a program for every 2mins on my UX10.20b how do I do it?

should I used crontab -e to add the line? do I need to do anything thereafter?

thank u!!



7 REPLIES 7
Robert-Jan Goossens
Honored Contributor

Re: How to use crontab

Hi,

2>&1 to capture all output, then the redirection it to the appropriate destination.

58,02,06,10,14,18,22,26,30,34,38,42,46,50,54 * * * * /usr/local/www/bin/web_chechk >> /dev/null 2>&1

example above is used every 4 minutes.

Yes, use the crontab -e to edit the cron.

Regards,
Robert-Jan
Jean-Luc Oudart
Honored Contributor
Solution

Re: How to use crontab

2>&1 redirects stderr into stdout

stdout is HOME/tmp/out
all yuor error messages will go in same file

man sh-posix

Regards
Jean-Luc

fiat lux
Sridhar Bhaskarla
Honored Contributor

Re: How to use crontab

Hi Henry,

1 is standard out
2 is standard error

>> $HOME/tmp/out redirects all the 'standard out' messages into /tmp/out file. 2>&1 tells the system to redirect all the error messages into 1 which is standard out which is inturn redirected to /tmp/out file.

If you want to run it every 2 mins, then it's slightly painful. Add the program like

0,2,4,6,8,10,12 (upto) 58 * * * * $HOME/bin/daily.job >> $HOME/tmp/out 2>&1

"crontab -e" is correct. It will open a 'vi' session. Once you make the changes, just save it and the changes will be automatically put into effect.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Bharat Katkar
Honored Contributor

Re: How to use crontab

Hi,

1. Errors will be redirected to Standard output that is your console.
2. 0,2,4,6,8,......,58 * * * *

Hope that helps.
Regards,
You need to know a lot to actually know how little you know
Stephen Keane
Honored Contributor

Re: How to use crontab

2>&1 is redirecting stderr to stdout. In other words anything that the program (daily.job) writes to file descriptor 2 (stderr) will instead go to file descriptor 1 (stdout). (File descripter 0 is stdin, in case you were wondering).

To run a job every 2 minutes (assuming you start at minute zero

0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58 * * * * your_job



Fred Ruffet
Honored Contributor

Re: How to use crontab

2>filename redirects errors output (stderr) to filename.
1>filename (or >filename without 1) redirects standard output (stdout).
&2 means stderr. &1 means stdout. So 2>&1 redirects stderr to the same file than stdout (output will be mixed).

To run a job every two minutes, you need something like that :
0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58 * * * * myjob
(which may be a little annoying. More recent crons such as Linux one, implements something like 0/2 that means every 2 minutes starting at 0. But we have to wait for HP to implement this feature)
for more explenations use "man crontab"

crontab -e is enough. No need to do extra things. Once saved, new crontab replaces old one.

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
Gerhard Roets
Esteemed Contributor

Re: How to use crontab

Hi Henry

Just so you know for reference
> implies redirection to a file

x>&y means take the CURRENT place y is pointing and redirect x to it. So this implies the following ...
command >> HOME/tmp/out 2>&1
and
command 2>&1 >> HOME/tmp/out
would yield different results.

Unix standard files is the following
0 = stdin

1 = stdout ( if none is specified this is the default with redirection). Hence the following.
command >> HOME/tmp/out 2>&1
and
command 1>> HOME/tmp/out 2>&1
is equivalent.

2 = stderr

HTH
Gerhard