Linux Automation [at, cron]

March 17, 2021

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 the at command 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 the crontab command.

Let’s learn more about the use of the at command. To use the at command, the syntax is as follows:

$ at time date

The following are examples of the at command:

  • The Ctrl + D command will save the at job. The task will be executed at 11.15 A.M. This command will log messages to the log.txt file 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 following command will make the task run on May 20 at 11 A.M.:
$ at 11 am may 20
  • All the jobs that are scheduled by the at command can be listed using the following command:
$ atq
  • To remove a specific job listed by the atq command, 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 following diagram is 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 the crontab command:

  • 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 to test@example.com:
0 22 * *  1-5   sendmail test@example.com  < ~/work/email.txt
  • The sendmail utility 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 the echo command:
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 * * * *

Related Articles

Calculating and reducing the runtime of a script

In this article, we are going to learn how to calculate and reduce the script’s runtime. A simple time command will help in calculating the execution time.PrerequisitesBesides having a terminal open, make sure you have the necessary scripts present in your...

read more

Lorem ipsum dolor sit amet consectetur

0 Comments

Submit a Comment

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

seventeen + 20 =