Starting with Linux Crontab

Linux Crontab is a time-based job scheduler which you can run various programs and scripts in different time and scheduling. Matter of fact, Crontab is quite powerful and allows you to do schedule your task in any flexible way as you wish.

However, for novice [including me], Crontab might be little bit confusing. In my case I wanted to run a script every five minutes and this script is kind of critical. Therefore, I had to make sure the crontab was working properly.

The very first thing I did was to create a dummy script just to test the Cron to see whether the schedule is correct or not.

Following is a dummy script that creates a file which starts with abc_timeStamp.

#!/bin/bash
test=`date +%s`;
touch /home/kasra/Desktop/abc_$test

After creating the script, I made the script runnable with this command,

$ chmod a+x MyScript.sh

The next step is to add the script to Crontab like this,

$ crontab -e

Then your editor window should be appeared and you can add the following line into it

*/5 * * * * /home/kasra/MyScript.sh

If you got message like “crontab doesn’t exist for user [userName]”, that means your editor has not set or set to wrong one, easiest solution is to change your editor with following command,

$ sudo select-editor

My recommended editor is either nano or vim.

After you have done everything just you need to wait for 5 minutes to see whether the Cron has run or not.

In addition, you can check Cron log file with this command to make sure it has run,

$ grep CRON /var/log/syslog

And lastly if everything is working fine, just replace the dummy script with the real working/production script.

Send your idea and information to kasra@madadipouya.com

Leave a Reply