In this article, you’ll learn how to schedule tasks in Linux using Cron. Cron allows you to automate commands by specifying the date, time, and frequency for execution. Once configured, the cron daemon runs the task without human intervention, making your system maintenance efficient and reliable. Consider a practical example: Michael needs to run the commandDocumentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
uptime and redirect its output to the file /tmp/system-report.txt every day at 9 p.m. Running the command manually can be tedious, so a Cron job automates this process. The command to append the output is:
Scheduling a Cron Job
To schedule this task, log in as Michael and edit Michael’s crontab:- Minute (0): The task runs at the 0th minute.
- Hour (21): The task runs at 9 p.m. (21:00 in 24-hour format).
- Day of Month (*): Every day of the month.
- Month (*): Every month.
- Day of Week (*): Every day of the week.
Avoid using
sudo with the crontab command if you intend the task to run as Michael; using sudo would schedule the job for the root user.Understanding Cron Syntax
Here are some examples to help you better understand the scheduling syntax:-
Run a job on February 19th at 8:10:
-
Run a job on February 19th at 8:10 only if it’s a Monday (where 1 represents Monday):
-
Run a job every day at 8:10:
-
Run a job every minute of every hour on all days:
-
Run a job every two minutes using step values:
Listing and Verifying Cron Jobs
To list all current Cron jobs for Michael, run:/var/log/syslog). Look for an entry similar to:
uptime command executed as scheduled.
Now it’s your turn! Try creating scheduled tasks using Cron jobs in a practical lab exercise to reinforce your learning.