Tutoriel Linux

Program a one-time command in Linux without creating a permanent cron job

Débutant3 min de lecture

Do you need to run a command just once tonight, tomorrow morning, or after a maintenance window? at avoids creating a cron job that you’ll forget about later. However, it has one constraint: the atd service must be running, and the command will be executed by /bin/sh, not necessarily by your interactive shell.

The method below allows you to schedule, check, and then remove a job before it executes. Use an absolute path for scripts and log files. An at job does not benefit from your terminal or any of your Bash settings.

Tux near a server, a clock, and a calendar to schedule a unique command on Linux
The atd service executes a unique command at the scheduled time.

Install at and verify the service

On Debian, Ubuntu, and their derivatives, install the package and then enable the daemon. On Fedora, RHEL, or Rocky Linux, replace apt with dnf.

sudo apt update
sudo apt install at
sudo systemctl enable --now atd
systemctl status atd --no-pager

The status should indicate active (running). If your account is denied, check /etc/at.allow and /etc/at.deny. These files determine which users have the right to submit tasks.

Schedule a command without leaving a cron

To test without affecting the system, create a small folder in your home directory in five minutes. Type the command, then end the input with Ctrl + D.

at now + 5 minutes
mkdir -p "$HOME/at-test"
date > "$HOME/at-test/executed-at.txt"
# then press Ctrl+D

For a real intervention, avoid relative paths. The following job restarts a service tomorrow at 02:30 and keeps stdout and stderr in a known log:

at 02:30 tomorrow
/usr/bin/systemctl restart my-service.service >> /var/log/my-service-at.log 2>&1
# then press Ctrl+D

Do not schedule a restart or remote change without manually verifying the command. If you are working over SSH, keep a second session open before making a change that might disconnect the network or the service.

Review the job and check the machine’s time

atq displays the pending jobs with their identifier. at -c allows you to see the actual registered script. This reading is useful when a variable, current directory, or a redirection matters for your command.

atq
at -c 7
timedatectl status

Replace 7 with the number displayed by atq. Also check the time zone with timedatectl status. The date entered in at is interpreted by the machine executing the daemon. A server set to UTC will not necessarily run the job at the time displayed on your workstation.

Cancel a job before its execution

The deletion is immediate. Take the identifier, remove the job, then relaunch atq to confirm it has disappeared.

atrm 7
atq

Use at for a one-time action: a purge after migration, a single restart, or a delayed test. For a repeated task, it is better to use cron and crontab or a systemd timer. This way, you will mainly avoid turning a one-time command into forgotten automation.

sudo apt update && sudo apt upgrade