Hints on linux cron jobs - A quick tutorial

If you really do not know what a cron job is, then please have a look at the Wikipedia's article first.

Hint 1: Keep in mind that cron does not know where your home directory is OR does not see your directory structure. Be careful and specify the full path of the things you call (like /usr/java/bin/java etc.). This is not enough; in several occasions you have to add an extra line to your crontab file in order to define your HOME path. It might be something like that:

HOME=/
You have to write this inside the crontab file.


Hint 2
: How do I edit my cron jobs? You have to open a file called the "crontab" and simply edit it. To do that just type:

>> crontab -e
Beautiful vi editor will open; now you can edit your cron jobs.


Hint 3
: I hate vi. How do I open the crontab with an other editor?
You have to type three commands this time:

>> EDITOR=gedit (I pick gedit - you may pick another editor)
>> export EDITOR
>> alias editor=$EDITOR
Now run crontab -e again and the beautiful gedit will open (civilisation!).


Hint 4
: Hmm, what else should I write inside the crontab file? What about the actual syntax of the cron jobs? OK, it is nothing special. You just have to type a command to be executed. Right BEFORE the command you have to specify WHEN do you want to execute this command (or how often). You will use 5 space separated text fields for that. This is an example of a line in a crontab file:

* * * * * /usr/java1.6_02/bin/java -jar /home/programs/tetris/dist/tetris.jar
This creates a cron job that will open a tetris java based game. The 5 stars mean that this thing will be executed once per minute (and after a while your computer will have a problem because this .jar is not a very light or smart application). Actually the first star controls minutes, the second hours, the third days, the fourth months and the fifth years. It is really simple. Is it? How do I set a cron job to run every two minutes? This is how:
*/2 * * * * <your_command>
You just have to type a slash 2 right next to the first star!

 

How do I set a cron job to run every day at 21.00?

0 21 * * * <your_command>
That's it... -:)


Hint 5
: How do I avoid receiving this huge email with the cron job's output? Sometimes cron jobs produce massive output (especially when you are executing programs that print a lot to the standard output). Here is the way to skip these emails by redirecting them to nowhere. You just have to add >/dev/null 2>&1 at the end of your command.

0 21 * * * <your_command> >/dev/null 2>&1


Hint 6
: How does a crontab file look in general? '#' is used for writing comments and a subset of my crontab looks like this:

HOME=/
SHELL=/bin/sh

# START MYSQL (this is a comment) 0 0 1 1 * /space/lampos/mysql/bin/mysqld_safe &
# Get statistics (I want to receive the notification email on this one!) 0 6 * * * /usr/java/latest/bin/java -jar -Xms256m -Xmx512m
/space/lampos/weathertalk/weatherTalkStat/dist/weatherTalkStat.jar -getLocStats
# BACKUP MYSQL odd days 10 7 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31 * *
/bin/tar -cvzf /space/lampos/BACKUP /mysql_odd.tar.gz /space/lampos/mysql
# MAIN FEED 0,5,10,15,20,25,30,35,40,45,50,55
0,1,2,3,4,5,6,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 * * *
/usr/java/latest/bin/java -jar
/space/lampos/weathertalk/weatherTalk/dist/weatherTalk.jar >/dev/null 2>&1

 

Use the form below, for any questions or comments! Your input is welcome.

Thanks for taking the time to

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful and beneficial to your readers. Oes Tsetnoc Internet Gratis Kerja Keras Adalah Energi Kita

Thanks for your comment! I

bill's picture

Thanks for your comment! I would love to write everything I learn on my blog, but unfortunately there is not always time for that. When I'll tackle again a fundamental concept or task which does not have many online references, and given that I'll have time to write about it properly, then I'll post again! Sorry for the long and bad structured answer of mine ... :-)

Great blog, this could be the

Great blog, this could be the best blog I ever visited this month. Never stop to write something useful dude! Oes Tsetnoc Oes Tsetnoc Kerja Keras Adalah Energi Kita Health Habit Computer Blog

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <p>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.