Day 9 and 10 - Task Scheduling in Linux with cron and at command

Day 9 and 10 - Task Scheduling in Linux with cron and at command

AT -> The at command is the terminal method of allowing you to schedule on-time jobs for a specific time and date.It executes commands at a particular time and accepts times of the form HH:MM to run a job at a specific time of day. The following expression like noon, midnight, teatime, tomorrow, next week, next Monday, etc. could be used with at command to schedule a job.

Note -> While cron is used to schedule recurring tasks, the at command is used to schedule a one-time task at a specific time.

Syntax: at [option] runtime

Installation of at command :

$sudo apt-get install at

Command to list the user’s pending jobs:

$at -l

or

$atq

Schedule a job to run one minutes from now:

Schedule a job to run four hour from now:

$at now +2 days

$at now +3 weeks

$at now +3 years

Schedule a job to run at 5pm 6 days from now:

Schedule a job to run at noon today:

$at noon tomorrow

$at next monday

at -r or atrm command is used to deletes job , here used to deletes job 7:

crontab -> The crontab is a list of commands that you want to run on a regular schedule, and also the name of the command used to manage that list. Crontab stands for “cron table, ” because it uses the job scheduler cron to execute tasks.cron is the system process which will automatically perform tasks for you according to a set schedule.

Linux Crontab Format:

MIN HOUR DOM MON DOW CMD

Linux Crontab Syntax:

Check if cron package is installed:

If cron is not installed, install the cron package on Ubuntu:

Verify if cron service is running:

schedule job:

When you run crontab -e in the future, it will bring up your crontab in this text editor automatically. Once in the editor, you can input your schedule with each job on a new line. Otherwise, you can save and close the crontab for now (CTRL + X, Y, then ENTER if you selected nano).

crontab -l : List the all your cron jobs.

crontab -r : Delete the current cron jobs.

Here are some more examples of how to use cron’s scheduling component:

  • * * * * * - Run the command every minute.

  • 12 * * * * - Run the command 12 minutes after every hour.

  • 0,15,30,45 * * * * - Run the command every 15 minutes.

  • */15 * * * * - Run the command every 15 minutes.

  • 0 4 * * * - Run the command every day at 4:00 AM.

  • 0 4 * * 2-4 - Run the command every Tuesday, Wednesday, and Thursday at 4:00 AM.

  • 20,40 */8 * 7-12 * - Run the command on the 20th and 40th minute of every 8th hour every day of the last 6 months of the year.

Conclusion:

Cron is a flexible and powerful utility that can reduce the burden of many tasks associated with system administration. When combined with shell scripts, you can automate tasks that are normally tedious or complicated.


Thanks for reading to the end; I hope you gained some knowledge.❤️🙌

PARAMVEER SINGH