- basic cron information
- How do I setup a cron job?
- How do I setup a cron job? (long version)
- How do I know it worked?
- My cron job does not run, what's wrong?
- What user does the cron job run under?
- Turn off cron job email notifications
basic cron information
Cron jobs allow you to run programs on a recurring schedule.
How do I setup a cron job?
Here is the short version, for those in a hurry...
-
Upload an executable file to the server.
-
Give the file execute permissions for your user account.
-
Log into the cornerhost control panel.
-
Click on "edit crontab" on the left menu.
-
Add a line to your crontab text box that indicates when the job should run, and which file should be executed.
How do I setup a cron job? (long version)
-
Upload a file to the server.
There are several ways to upload files to the server, including FTP, and scp. Check out other FAQ on these topics for more information. If you have a shell account, you can edit a file on the server using vi or emacs.
-
Give the file execute permissions for your user account.
You have to give the file execute permissions for your account. A "700" permission is sufficient which gives you rights to read/write/execute, but no one else can read/write/execute your file. Some FTP programs allow you to set the permissions on the file, or if you have shell access, the "chmod" command (run "man chmod" for more information) allows you to change the permissions of a file.
-
Log into the cornerhost control panel.
You can login to the cornerhost control panel from the www.cornerhost.com home page.
-
Click on "edit crontab" on the left menu.
-
Add a line to your crontab text box that indicates when the job should run, and which file should be executed.
Adding crontab lines are complicated, and better documented elsewhere. For beginners you may want to use the following template:
15 4 * * * nice $HOME/run_every_day_at_four_fifteen_am.py
The "15 4 * * *" is the time of day to run the program everyday (i.e. 4:15, military time). Other times are:
-
"00 20 * * *" is 8:00 PM everyday
-
"59 23 * * *" is 11:59 PM everyday
In general, the program you run should be: "nice $HOME/programname". Replace "programname" with the file you want to run. The "nice" command lets the server know that the running program is not time sensitive and should yield to time sensitive processes, such as your website. The "$HOME" is a special keyword that is the path to your home directory. The following is the equivalent command without the $HOME keyword, "15 4 * * * nice /home/clay/run_every_day_at_four_fifteen_am.py".
You can also run programs within a directory in your home directory:
15 4 * * * nice $HOME/myscripts/run_every_day_at_four_fifteen_am.py
A word of warning: The last line must have a <return> at the end, otherwise your cron job will be ignored.
How do I know it worked?
You will receive an email as soon as the file is finished running.
My cron job does not run, what's wrong?
Double check that the last line has a return at the end.
What user does the cron job run under?
No, it does not run under the root account. It runs under your account, which means your account must have execute permissions on the file, and the program or script can only do what your account is allowed to do.
Turn off cron job email notifications
Redirect the output of the command to /dev/null. If there's no output, cron won't send you an email.
Example:
00 * * * * $HOME/hourly.py > /dev/null
For complete silence:
00 * * * * $HOME/hourly.py > /dev/null 2>&1
