Quick Tutorial: How to Automate Tasks in Linux Using Cron

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

The main reason for 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 the specific task at the scheduled time

crond - runs in the background

crontab - file contains a list of tasks to schedule

How to schedule a job in Linux

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

Format for scheduling command

minutehourdaymonthday of weekusercommand
0-590-231-311-120-7rootsh 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 tasks, certain special characters may be used to automate tasks. These characters give enormous flexibility to users to schedule tasks effectively.

Special CharacterRemarks
asterisk (*)It specifies all values with respect to a 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 the week field means Saturday.
You can use it in the day of the week field after a 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 a 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 a given number in the 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 option of putting multiple values in a single field.
e.g. 30,31 in the day field specifies a date of 30 and 31.
hash (#)Only allowed for a 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 the 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 needs to be updated to schedule tasks automatically. You can use the below commands to schedule tasks:

crontab -e
nano /etc/crontab

Shortcuts of crontab

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

You can use shortcuts in the 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 them 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 the content of the crontab file

crontab -l

To remove the current crontab file of the user

crontab -r

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

crontab -i

Conclusion

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

Subscribe us to receive more such articles updates in your email.

If you have any questions, feel free to ask in the comments section below. Nothing gives me greater joy than helping my readers!

Disclaimer: This tutorial is for educational purpose only. Individual is solely responsible for any illegal act.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

10 Blockchain Security Vulnerabilities OWASP API Top 10 - 2023 7 Facts You Should Know About WormGPT OWASP Top 10 for Large Language Models (LLMs) Applications Top 10 Blockchain Security Issues