All Linux HowTo's

How to run a command at a given time

Sometimes we want to run a single command at a later time. We could use Cron but that’s more for running commands every day, week, month, etc. Instead we can use the “at” command.

The “at” command works in 24 hour time. So to run something at “12:15” means running it just after mid-day. We can have “at” send us an email once done using the “-m” option.

Let’s touch a file at 12:15 pm and have the output emailed to us.

[me@www ~]$ at 12:15 -m 
at> touch /tmp/delme
at> <EOT>
job 7 at Wed Dec 21 12:15:00 2016

The above starts with the “at” command setting the time and the “-m” option. We then enter the command that we want to run “touch /tmp/delme”. And then press “Control+D” to end. We get a message saying “job 7 at Wed Dec 21 12:15:00 2016” confirming our schedule.

You can check your “at” job queue by issuing the “atq” command. It will output the following:

[me@www ~]$ atq
7	Wed Dec 21 12:15:00 2016 a me

We can remove that job by issuing the command:

atrm 7

Where 7 is the job number included in the output of the “atq” command (above).

You can’t see the actual command that you’ve scheduled using any “at” command but you can look in “/var/spool/at/” for pending scheduled commands. Only root can see them.

The files in “/var/spool/at/” are lengthy with plenty of information such as paths, etc. If you want to see the actual command, look towards the end of the job file.

Similar Posts:

Leave a Reply

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