Any Linux user, especially security professionals and system administrators, wants to increase productivity by automating routine tasks such as backup of system, initiate specific programs during startup, rotate log files, run customize security scanners, delete temporary files etc.

The main reason of automating tasks is to increase efficiency and also provide adequate resources to processes so jobs run automatically when no other process is running. This blog helps you to use cron daemon to schedule mundane tasks automatically.

Basics

cron - check cron table to run specific task at schedule time

crond - runs in background

crontab - file contains list of tasks to schedule

How to schedule job in Linux

Cron table located at /etc/crontab. Location of cron may be varies depending on Linux distribution. User need to specify schedule timer, user and the command.

Format for scheduling command

minute hour day month day of week user command
0-59 0-23 1-31 1-12 0-7 root sh testscript.sh

Examples:

45 20 * * * * /home/testscript.sh means that run of testscript.sh from /home/testscript.sh every day at 8:45 PM.

0 9 20 * * /bin/sh /home/backup.sh means that run of backup.sh from /home/backup.sh on 20 of every month at 9:00 AM.

Special Characters

While defining task, certain special characters may be used to automate tasks. These characters gives enormous flexibility to users to schedule tasks effectively.

Special Character Remarks
asterisk (*) It specifies all values with respect to particular field.
An asterisk in the minute field is equivalent to "every minute." Similarly, An asterisk in the day field is equivalent to "every day."
dash (-) It denotes the range of values.
1-5 in the day field specifies a date of 1 to 5.
L (last) Only allowed for the day and day of week fields.
Use of L in the day field simply means "the last day of the month,".
Use of L in the day of week field means Saturday.
You can use it in the day of week field after specific value. e.g. 4L means "the last Wednesday of the month."
question mark (?) Only allowed in day and day of week field.
You can mention ? in day field and 7 in day of week field, it specifies to run a task on Saturday but you don't care about whatever the date may be.
slash (/) It provides an option to skip given number in field.
*/4 in the day field is equivalent to 0,4,8,12,16,20,24 and 28. The asterisk ( * ) denotes "every day," but the /4 means only the first, fifth, ninth etc..
comma (,) Provide an options of putting multiple values in single field.
e.g. 30,31 in the day field specifies a date of of 30 and 31.
hash (#) Only allowed for day of week field.
e.g. The value of 2#4 in the day of week field means the fourth Monday of the month (day 6 denotes Friday and #4 denotes the 4th one in the month).
W Only allowed for day field. It denotes the weekday i.e. monday to friday nearest the provided date.
If you use 18W in day field, if 18 is saturday, the script runs on friday. But if 18 is sunday, the script runs on monday.

Edit crontab file

crontab file specifies the content that need to be updated to schedule tasks automatically. You can use below commands to schedule tasks:

crontab -e
nano /etc/crontab

Shortcuts of crontab

@reboot @daily @weekly @monthly @annually @yearly @noon @midnight

You can use shortcut in crontab file as mentioned below.

@daily root /usr/share/backup.sh

How to check logs of cron

Every system defines logs of cron in different ways and also store at different location. On Ubuntu, Debian and other similar distributions, you can find logs of cron jobs in /var/syslog. While, On CentOS, Redhat and Amazon Linux cron logs are found in /var/log/cron.

crontab Commands

To display content of crontab file

crontab -l

To remove current crontab file of user

crontab -r

To remove current crontab file of user, with prompt of yes/no

crontab -i

Conclusion

Linux provides an ample options to schedule tasks more effectively and increase efficiency. I hope this tutorial helped you to write your first script to automate your task.