Linux Automation [at, Cron]

Posted on the 17 March 2021 by Satish Kumar @satish_kumar86

Understanding “at”

Many a time, we need to schedule a task for a future time, say in the evening at 8 p.m. on a specific day. We can use theatcommand in such a situation.

Sometimes, we need to repeat the same task at a specific time, periodically, every day, or every month. In such situations, we can use thecrontabcommand.

Let’s learn more about theuseof theatcommand. To use theatcommand, the syntax is as follows:

$ at time date

The following are examples of the at command:

  • TheCtrl+Dcommand will save theatjob. The task will be executed at 11.15 A.M. This command will log messages to thelog.txtfile at 11.15 a.m.:
$ at 11.15 AMat >  echo "Hello World" > $HOME/log.txtat >  Control + D
  • The following command will send an email on March 31, 2015, at 10 A.M.:
$ at 10am mar 31 2015at> echo "taxes due" | mail jonat> ^D
  • The followingcommandwill make the task run on May 20 at 11 A.M.:
$ at 11 am may 20
  • All the jobs that are scheduled by theatcommand can be listed using the following command:
$ atq
  • To remove a specific job listed by theatqcommand, we can use the following command:
$ atrm  job-id

Understanding crontab

If we need to run a specific task repetitively, then the solution is to use crontab. The syntax of the command is as follows:

$ crontab -e

This will open a new editor. The followingdiagramis the syntax to add tasks. The fields to use for repeating tasks at a particular time are explained here:

Finally, to save the jobs, use the following:

Press Esc then type :wq

The preceding operations will save the job and quit crontab.

The following are a few examples of thecrontabcommand:

  • Use the following command to run a script every hour at the fifth minute, every day:
5 * * * *      $HOME/bin/daily.job >> $HOME/tmp/out  2>&1
  • Use the following command to run 5 minutes after midnight every day:
5 0 * * *      $HOME/bin/daily.job >> $HOME/tmp/out  2>&1
  • Use the following command to run at 2.15 p.m. on the first of every month–the output is mailed to Paul:
15 14 1 * * *     $HOME/bin/monthly
  • Use the following command to run at 10 P.M. on weekdays, and send the email totest@example.com:
0 22 * *  1-5   sendmail test@example.com  < ~/work/email.txt
  • Thesendmailutility is used for sending emails. We can also use the mail utility as follows:
sendmail user@example.com  < /tmp/email.txt
  • The following commands are self-explanatory from the text of theechocommand:
23 0-23/2  *  *  *  echo "run 23 minutes after midn, 2 am, 4 am, everyday"5  4  *  *  sun    echo "run at 5 minutes after 4 am every Sunday"

The following are a few more crontab command examples:

Min

Hour

Day / month

Month

Day / week

Execution time

45

0

5

1,6,12

*

00:45 hrs on the fifth day of January, June, and December.

0

18

*

10

1-5

00 P.M. every weekday (Monday-Friday), only in October.

0

0

1,10,15

*

*

Midnight on the first, tenth, and fifteenth days of the month.

5,10

0

10

*

1

At 12.05 and 12.10 every Monday, and on the tenth day of every month.

We can add macros in the crontab file. Use the following to restart my_program after each reboot:

@reboot  /bin/my_program@reboot echo `hostname` was rebooted at `date` | mail -s "Reboot notification" admin@example.com

The following is a summary of a few more macros:

Entry

Description

Equivalent To

@reboot

Run once at start-up

None

@weekly

Run once a week

0 0 * * 0

@daily

Run once a day

0 0 * * *

@midnight

(same as @daily)

0 0 * * *

@hourly

Run once an hour

0 * * * *