How to schedule a task in linux with cron

The cron is a very useful job scheduler in the Linux system. It runs based on commands specified in a cron table (crontab). Each user, including root, can have a cron file. These files don’t exist by default but can be created in the /var/spool/cron directory. Crontab files do not allow you to use any text editor for creating or editing it. To do that, you have to use commands as below

crontab -e: create or edit the crontab file 
crontab -l: Display the crontab file
crontab -r : Remove the crontab file 

The structure of a crontab file

# * * * * * command to execute
# - - - - -
# | | | | |
# | | | | |---------- day of week (0-7)
# | | | |
# | | | |--------------- month (1-12)
# | | |
# | | |--------------------- day of month (`1-31)
# | |
# | |---------------------------- hour (0-23)
# |
# |----------------------------------- minute (0-59)

How to add your schedule task to the crontab file
For example, if you have a bash script which will reminder you to do a list of thing in a day starting from 6 am. By following these steps to add it into the crontab file:

$ crontab -e 

then insert the line below to the bottom of the crontab file:

0 6 * * * /path/to/

Note that you have to set the execution permission for by using chmod + x command.
By default cron service will send the email to user who is own of that script. To disable it, you should do:

0 6 * * * /path/to/ >/dev/null 2>&1

Starting the cron service

$ service cron start
$ service cron status

Disable a schedule task in the crontab file
To disable a schedule task in the crontab file, you should use crontab -e then comment the task you want to ignore by putting # starting of line.

Hopefully this post help you easy to make a schedule task with cron service in Linux.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s