How to schedule a job/task using crontab

It’s been a long time since my last post. I’ve been quite busy, but i still want to run these pages so here am i.

First thing first, you don’t want to run a job as root (vi /etc/crontab , unless you do exactly what you are doing of course). So all the following instruction are done under my user.

Let’s see what’s on the definition file  “/etc/crontab”

[lolo@dellxps ~]$ cat /etc/crontab

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

See, it already tell you everything you need to know. Let’s take a look to an example. i have created a script(~/scripts/ that will display a notification on my Gnome desktop on Fedora:

#test bash to be launch with crontab or/and anacrontab
#don't forget chmod u+x
notify-send "Hello World" -t 3000

Let now schedule a “funny” reminder every minute ! to edit cron table use the following syntax (to edit another user crontab use -u switch):

[lolo@dellxps ~]$ crontab -e

then add the following line

*/1 * * * * /home/lolo/bin/

Now let set it every working day at 13h05 (1:05 pm) from January to June each year:

5 13 * 1,2,3,4,5,6 mon-fri /home/lolo/bin/

For each value of the time table minute, hour, day of month, month, day of week you can use special character:

  • use * (star) to specify all possible values of the range. ex: use * for day of month position to schedule the task each day of the month.
  • use , (comma) to specify a list of value. ex: to schedule a job every 2 months, at month position set jan,mar,may,jul,sep,nov or 1,3,5,7,9
  • use – (dash) to specify a range of value. ex: to schedule a task from monday to friday set day of week value as 1-5 or mon-fri
  • use / [slash] to specify a recurrence within the unit of time. ex: use * */1 * * * to schedule an event every hour from 00:00 to 24:00

To review the list of tasks scheduled for your current user:

[lolo@dellxps ~]$ crontab -l

To remove/delete all entries:

[lolo@dellxps ~]$ crontab -r

You may also need to use the shortcuts or aliases instead of this long and coding stuff…for instance:

  • @reboot: Run after each time the machine is started.
  • @yearly or @annually: Scheduled once a year, same as “0 0 1 1 *”.
  • @monthly: Run once a month, same as “0 0 1 * *”.
  • @weekly: Scheduled once a week, same as “0 0 * * 0”.
  • @daily or @midnight: Scheduled once a day, same as “0 0 * * *”.
  • @hourly: Scheduled every hour, same as “0 * * * *”.


You can check the cron tab log using the command:

[lolo@dellxps ~]$ tail /var/log/cron

!!! WARNING !!! crontab is used to launch task on a machine that run 24/7. If the the machine is shutdown at the time you specify the task to be executed, NOTHING WILL BE LAUNCH.

If you want to run  a task on a laptop, or desktop that is regulary shutdown use anacrontab instead. It’s similar to crontab but will run the task at {starting time daemon anacron + delay}

[lolo@dellxps ~]$ cat /etc/anacrontab
# /etc/anacrontab: configuration file for anacron

# See anacron(8) and anacrontab(5) for details.

# the maximal random delay added to the base delay of the jobs
# the jobs will be started during the following hours only

#period in days   delay in minutes   job-identifier   command
1    5    cron.daily        nice run-parts /etc/cron.daily
7    25    cron.weekly        nice run-parts /etc/cron.weekly
@monthly 45    cron.monthly        nice run-parts /etc/cron.monthly

As usual for more detail don’t forget man pages for more help

  • man crontab
  • man anacrontab

Stay tune…


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: