linux cron jobs

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?

Syndicate content