AT command
Updated on 03 Oct 2020
The at
command is similar to a cron except that at
is typically used for one off scheduling.
Installing
at
may not be installed, so you can do that with the following commands.
sudo apt-get update
sudo apt-get install at
Starting the Daemon
Installing at
doesn’t automatically start the daemon. We need to do that manually.
sudo systemctl enable --now atd.service
Tip: We use
enable
so that it will be automatically launced at boot.
Scheduling a task (now + increment)
Scheduling a task is pretty straight forward.
at now +2 minutes -f test_1.sh
- now run now
- +2 minutes add 2 minutes on to now
- -f executable file to run
Available increments
The increments that we can use for the at command are listed below.
| minute | minutes |
| hour | hours |
| day | days |
| week | weeks |
| month | months |
| year | years |
I.e.
at 10 AM +2 weeks
Scheduling a task (time + increment)
We can also schedule a task to run at a specific time
at 10 AM -f script.sh
We can also specify a day
at 10 AM Sun -f script.sh
Remember, these tasks are one off!
Scheduling a task (date)
We can specify a time, day and/or date with our at
command.
at 10:00 AM July 25 -f script.sh
/etc/at.deny for Yii
Before you even contemplate using at
for Yii, you’ll need to remove the www-data user from the /etc/at.deny
file. Edit this file and remove www-data from this file so that Yii can use the at
command.
heredoc, PHP and Yii
at
supports the use of heredoc which will allow us to use PHP or Yii commands.
at 10:00 AM July 25 <<heredoc
/usr/bin/php test_1.php
heredoc
A Yii version would be
at now +1 minute <<heredoc
/var/www/demoapp/yii controller/action parameters
heredoc
Notice that the heredoc
statements must go on their own line. This is a requirement of heredoc
, and failure to do so will result in errors.
And this can all be incorporated into Yii or a PHP script.
- 1> stdout file
- 2> stderr file
Note when running under Yii, the user is www-data, and it doesn’t have access to your paths etc. Therefore you must supply the full path to codecept, i.e. vendor/bin/codecept
.
$processId = 43;
$cmd = 'at now +1 minute <<EOF
cd /var/www/demoapp
vendor/bin/codecept run acceptance demoappManualUpdateCept.php 1>/var/www/demoapp/runtime/logs/out1.txt 2>/var/www/demoapp/runtime/logs/error1.txt
EOF';
exec($cmd);
Viewing the schedule (atq)
You can view what is in the at
queue with the atq
command. This is equivalent to using at -l
.
We can also use this in PHP and capture the output using shell_exec
function. This can be very useful if you want to check that a process is not already in the queue before adding it again.
$cmd = 'atq';
//shell_exec returns the output as a string!
$out = shell_exec($cmd);
echo "\nphp: $out\n";
Removing a job (atrm)
You can remove a job from the queue using atrm
and passing the job_id. The job_id is the first number that shows up when you list the queue; with either:
atq
at -l