Other Software

ID #105

Can I have a crontab?

Applies to: Grid System

Yes, just login to your shell using ssh and type:

crontab -e

That will put you in the Vi editor editing your crontab. The Vi keystrokes you need to edit your crontab are: "i" for insert, then type out your cron line following the format below for each line, then press the "escape" key on your keyboard to get out of insert mode, then ":" to get a prompt, then "wq" and then "enter" and it is saved.

The form of a crontab file is:

minute hour day month weekday command

To view your crontab file, type:

crontab -l

To remove your crontab, type:

crontab -r

A perfectly good crontab might look like this:

MAILTO=yourname@yourdomain.com
41 04 * * 1 /save/whales -v

The above crontab will execute the script "whales -v" from your "/save" directory every Monday morning at 4:41AM.

The "whales" script should be executable (permissions like 700 or -rwx------). The "whales" script should also specify on the first line which interpreter you want to use like:

#!/usr/bin/perl

or

#!/usr/bin/python

If you do not specify an interpreter, then it will assume you want the script interpreted by the shell "sh". Any output from the script will be emailed to the person listed as MAILTO. The MAILTO line is optional. To preserve shared resources, we discourage setting up a crontab which runs every minute. Modwest administrators will routinely reset scheduled tasks to run every 5-15 minutes if they are set to run more frequently.

Please note that changes to your crontab may take up to 15 minutes to synchronize with the main cron server. Please be sure to configure your scripts to execute at least 15 minutes in the future if you are testing your crontab.


Last update: 2010-09-15 17:40
Author: FAQ Admin
Revision: 1.3

Digg it! Share on Facebook Print this record Send FAQ to a friend Show this as PDF file
Please rate this FAQ:

Average rating: 3.5 (10 Votes)

completely useless 1 2 3 4 5 most valuable

You can comment this FAQ

Comment of Anonymous:
Beware of the line endings in DOS vs unix. I edited the crontab and script files on DOS (with CR/LF endings) and unix expects just the LF. The symptom was the error:

/bin/sh: /ping/pingMonitor.py: bad interpreter: No such file or directory

You can find utilities dos2unix and unix2dos to do the conversion.

Cheers,
Roger Burnham
Added at: 2002-06-19 08:16

Comment of Anonymous:
Any output from a cron script (including stderr messages) is emailed to your hosting account mailbox. If you do not want the mails, or the disk space they use up, then have your cron script produce no output:

/path/to/script &> /dev/null
Added at: 2003-01-30 11:34

Comment of Anonymous:
Here's a crontab that executes at 4:41AM on the 1st of each month and deletes files in the logs directory that are more than 32 days old:

41 04 1 * * find /logs -ctime +32 -type f -exec rm -f \{\} \;
Added at: 2003-11-18 18:49

Comment of Anonymous:
When trying to execute "lynx" from a cron, the cron was not executing but was instead giving errors about "no such directory /www/vhosts/m/myaccount.com" so I created such a path, making it lead back to my home directory with a symlink like this:

cd /
mkdir www
mkdir www/vhosts
mkdir www/vhosts/m
cd www/vhosts/m
ln -s ../../../ myaccount.com

where "myaccount.com" is the domain name hosted here and "m" is the 1st letter of that domain name
Added at: 2003-04-09 13:42

Comment of Anonymous:
You CAN use a backtick operator in the command argument place in a crontab.

However, percent signs (%) in the command must be escaped with a backslash or else they will be interpreted as a newline.

From 'man crontab':
The ??sixth?? field (the rest of the line) specifies the command to be run. The entire command portion of the line, up to a newline or % character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the crontab file.
Percent-signs (%) in the command, unless escaped with backslash (\), will be changed into newline characters, and all data after the first % will be sent to the command as standard input. There is no way to split a single command line onto multiple lines, like the shell?s trailing "\".

so the following is valid:

15 3 * * 0,2-6 echo ´date +\%a´

and

15 3 * * 0,2-6 echo $(date +\%a)

Failure to escape the backslash will result in a cron error:

/bin/sh: -c: line 1: unexpected EOF while looking for matching ´´'
/bin/sh: -c: line 2: syntax error: unexpected end of file

Added at: 2007-08-30 23:49

Comment of Anonymous:
If you're finding the crontab exercise to be a bit challenging, or you'd like to offload things a bit from your host, you can always use an online cron service like http://cronless.com.

Reliable online cron jobs for your website.
Added at: 2010-02-02 14:39

Comment of :
+1 thanks to the escaping "%".

Driving me nuts.

It should be noted that this information is in section 5 of the crontab man page.

Referenced as follows:

man -S 5 crontab

Using the capital "S" option.

More on man page sections:
http://en.wikipedia.org/wiki/Man_page#Manual_sections

Added at: 2011-03-01 15:18

Comment of Anonymouse:
Many thanks to the person who pointed out that % signs are interpreted differently by cron. I've been trying to fix that "unexpected EOF" error for over a week now, and this is the first mention I've seen of this.
Added at: 2010-09-15 10:50

Comment of :
^^Agreed^^ thank you for the EOF error tip!
Added at: 2011-02-10 07:17