Skip to content

The Linux at Command: Schedule One-Off Tasks the Right Way

Run a command once at a set time with at, batch, atq and atrm: time syntax, the environment trap, lost output, access control, and the systemd-run alternative.

●By 10 min read

Cron is for things that repeat. A surprising amount of admin work does not: restart a service after the change window opens, roll back a firewall change if you lock yourself out, run a heavy report once the nightly backup is done. People handle these with a crontab entry they promise to delete, or a sleep 3600 && … in a tmux pane. Both are worse than the tool built for the job.

at queues a command to run once at a given time, writes it to disk so it survives a reboot, and captures your environment so the job runs the way your shell would. This guide covers the time syntax, the queue tools, the two traps that make at jobs fail silently, and when a transient systemd timer is the better choice. For recurring schedules, see our cron jobs guide.

Install atd and Queue a First Job

The at package provides the client commands and the atd daemon that runs jobs. It is often missing from minimal and cloud images.

# Debian / Ubuntu (the service is enabled on install)

$ sudo apt install at

# RHEL / Fedora / Rocky / Alma

$ sudo dnf install at

$ sudo systemctl enable --now atd

$ systemctl is-active atd

active

# Commands come from stdin: type them, then Ctrl-D

$ at now + 10 minutes

warning: commands will be executed using /bin/sh

at> /usr/local/bin/rotate-reports.sh

at> <EOT>

job 7 at Fri Oct 2 15:10:00 2026

# Non-interactive forms

$ echo 'systemctl restart app' | sudo at 02:00

$ at -f /srv/jobs/reindex.sh 23:30 tomorrow

$ at now + 1 hour <<'EOF'

cd /srv/app && ./export.sh > /var/tmp/export.log 2>&1

EOF

Keep job bodies simple β€” the job file is a /bin/sh wrapper that replays your environment and then runs your commands. Rather than depend on which shell interprets them, put real logic in a script with an explicit #!/bin/bash line and have the at job call it by absolute path.

Quote the heredoc delimiter β€” <<'EOF' stops your current shell from expanding variables and $(…) at submission time, so they are evaluated when the job runs.

Jobs survive reboots β€” at jobs are files in a spool directory. If the machine is down at the scheduled time, atd runs the overdue job when it starts again.

Time Specifications That Actually Parse

The at time parser accepts a small natural-language grammar: a time, an optional date, and an optional increment. If you give only a time that has already passed today, the job runs at that time tomorrow.

SpecMeaning
now + 30 minutesRelative; units are minutes, hours, days, weeks
16:00 or 4pmToday at 16:00, or tomorrow if that has passed
noon / midnight / teatime12:00, 00:00 and 16:00
02:00 tomorrowTomorrow at 02:00
9am next weekSeven days from now at 09:00
4pm + 3 daysThree days from today at 16:00
10:30 Oct 15Month name and day; year optional
10:30 2026-10-15ISO date ([CC]YY-MM-DD)
10:30 10/15/2026MM/DD/[CC]YY (US order)
10:30 15.10.2026DD.MM.[CC]YY (European order)
-t 202610151030POSIX form [[CC]YY]MMDDhhmm[.ss], as used by touch -t

# Check how a spec parses before trusting it: -v prints the time first

$ echo true | at -v 10:30 10/15/2026

Thu Oct 15 10:30:00 2026

warning: commands will be executed using /bin/sh

job 8 at Thu Oct 15 10:30:00 2026

# Then remove the test job

$ atrm 8

Prefer ISO or -t in scripts β€” 10/15 versus 15.10 is decided by the separator, which is easy to get wrong. 2026-10-15 and -t are unambiguous.

Times are local β€” at uses the system time zone. A server on UTC runs at 09:00 at 09:00 UTC, whatever your laptop says. Check with timedatectl.

Granularity is one minute β€” atd checks the queue once a minute, so a job can start up to a minute after its time. For second-level precision use a systemd timer with AccuracySec set.

Managing the Queue: atq, at -c, atrm

$ atq

7 Fri Oct 2 15:10:00 2026 a deploy

9 Sat Oct 3 02:00:00 2026 a deploy

11 Fri Oct 2 15:04:00 2026 = deploy # "=" means running right now

# Root sees every user's jobs; a normal user sees only their own

$ sudo atq

# Show exactly what job 9 will run: a header, the captured environment,

# a cd into the submission directory, and your commands at the very end

$ at -c 9 | head -6

#!/bin/sh

# atrun uid=1001 gid=1001

# mail deploy 0

umask 22

MAIL=/var/mail/deploy; export MAIL

USER=deploy; export USER

# Remove one or more jobs

$ atrm 7 9

# Clear every job you own

$ atq | awk '{print $1}' | xargs -r atrm

CommandEquivalentDoes
atqat -lList pending and running jobs
atrm Nat -r N / at -d NDelete job N
at -c NPrint the full job script to stdout
batchat -bQueue for when load is low (see below)
at -q c TIMEUse queue c; later letters run with a higher nice value
at -m TIMESend mail even if the job prints nothing
at -M TIMENever send mail

Queue letters run from a to z and A to Z. at uses a by default and batch uses b. A job submitted to an uppercase queue is treated as a batch job once its time arrives. The job files themselves live in /var/spool/cron/atjobs on Debian and Ubuntu and in /var/spool/at on RHEL and Fedora.

sponsored

The Two Traps: Environment and Output

at is the opposite of cron in one important way. Cron starts jobs with an almost empty environment. at copies yours: the working directory, the umask, and every exported variable except a few such as TERM, DISPLAY and _. That is convenient, and it causes two kinds of failure.

The directory is gone β€” the job begins with cd into the directory you submitted from. Queue a job from a build directory that a cleanup removes, and the job exits with "Execution directory inaccessible" before your command runs. cd / or into a stable path before submitting, or cd explicitly in the job.

The environment was not what you thought β€” a job submitted with sudo at gets sudo's reset environment, not your shell's. A job submitted from a shell with a Python virtualenv or a custom PATH active depends on it still existing later. Use absolute paths for anything important.

Secrets end up on disk β€” exported variables such as AWS_SECRET_ACCESS_KEY or GITHUB_TOKEN are written into the job file in the spool directory and stay there until the job runs. Unset them, or submit from a clean shell with env -i.

The second trap is output. Anything the job writes to stdout or stderr is sent as mail to the submitting user through /usr/sbin/sendmail. Most servers today have no mail transport agent, so that output goes nowhere you will ever see. Redirect it yourself:

# Bad: if this fails, the error is mailed to a mailbox nobody reads

$ echo '/srv/app/migrate.sh' | at 02:00

# Good: log to a file, and to the journal with a searchable tag

$ at 02:00 <<'EOF'

/srv/app/migrate.sh >> /var/log/app-migrate.log 2>&1

echo "exit=$?" | systemd-cat -t app-migrate

EOF

# Next morning

$ journalctl -t app-migrate --since today

Oct 03 02:00:41 web1 app-migrate[48211]: exit=0

batch, Load Limits and Access Control

batch takes the same input as at but no time. The job waits until the load average drops below 1.5, then starts. atd also waits at least 60 seconds between starting batch jobs. Both values are set on the atd command line with -l and -b.

$ echo 'nice -n 19 /usr/local/bin/rebuild-search-index' | batch

warning: commands will be executed using /bin/sh

job 14 at Fri Oct 2 15:20:00 2026

# 1.5 is far too low for a 16-core box. Raise it.

# RHEL / Fedora: options come from /etc/sysconfig/atd

OPTS="-l 12 -b 120"

# Debian / Ubuntu: override the unit's command line

$ sudo systemctl edit atd

[Service]

ExecStart=

ExecStart=/usr/sbin/atd -f -l 12 -b 120

$ sudo systemctl restart atd

Who may use at is controlled by two files. If /etc/at.allow exists, only the users listed in it may submit jobs. Otherwise /etc/at.deny is checked and everyone not listed in it is allowed; an empty at.deny allows everyone. If neither file exists, only root can use at. Debian ships an at.deny listing system accounts; RHEL ships an empty one.

Lock it down on shared hosts β€” an allow-list is easier to reason about: echo deploy | sudo tee /etc/at.allow. The same model applies to cron with cron.allow and cron.deny, and both belong in your server hardening checklist.

Audit what is queued β€” sudo atq is a quick persistence check during incident response. A job queued by an attacker looks exactly like a legitimate one, so read it with at -c.

Patterns Worth Keeping, and When to Use systemd-run

The best use of at is the dead man's switch: schedule the rollback first, make the risky change, and cancel the rollback only once you have confirmed you can still log in.

# 1. Save the known-good ruleset (it must start with "flush ruleset")

$ sudo nft list ruleset | sudo tee /root/nft-good.conf >/dev/null

$ sudo sed -i '1i flush ruleset' /root/nft-good.conf

# 2. Schedule the rollback and remember the job number

$ job=$(echo 'nft -f /root/nft-good.conf' | sudo at now + 5 minutes 2>&1 | awk '/^job/ {print $2}')

# 3. Apply the risky change

$ sudo nft -f /etc/nftables.conf

# 4. Open a NEW ssh session. If it works, cancel the rollback

$ sudo atrm "$job"

The same trick protects sshd_config edits and network changes. Our firewall rules generator builds the ruleset; at makes applying it safe.

On systemd hosts, systemd-run can create a transient timer that does the same thing, with logging in the journal and resource limits from unit properties:

$ sudo systemd-run --on-active=30min --unit=fw-rollback \

/usr/sbin/nft -f /root/nft-good.conf

Running timer as unit: fw-rollback.timer

Will run service as unit: fw-rollback.service

$ sudo systemd-run --on-calendar='2026-10-15 02:00' --unit=reindex \

-p Nice=19 /usr/local/bin/rebuild-search-index

$ systemctl list-timers fw-rollback.timer

$ sudo systemctl stop fw-rollback.timer # cancel

$ journalctl -u reindex.service # output, no mail needed

atsystemd-run timercron
RunsOnceOnce (or repeating with OnCalendar)Repeatedly
Survives rebootYes, spooled to diskNo, transient units are lostYes
EnvironmentCopied from your shellClean unit environmentMinimal
OutputMail via sendmailJournalMail via sendmail
Cancelatrm Nsystemctl stop NAME.timerEdit the crontab
Load-awareYes, with batchNoNo

Pick by lifetime β€” if the job must run even if the server reboots first, use at. If it is a short-fuse safety net within the current boot, systemd-run gives you better logs.

Need it permanent? β€” a job you schedule with at every week should be a real timer unit. Our systemd generator writes the .service and .timer pair.

Reboots have their own tool β€” shutdown -r 02:00 schedules a reboot and warns logged-in users; shutdown -c cancels it.

Frequently Asked Questions

What is the difference between at and cron?

at runs a command once at a specific time and then forgets it; cron runs commands repeatedly on a schedule. at also copies your current environment and working directory into the job, while cron starts jobs with a minimal environment. Use at for one-off tasks and cron or systemd timers for anything recurring.

Why did my at job not run or produce no output?

Check that atd is running with systemctl status atd, and that the job appears in atq. If it ran, its output was mailed via sendmail, which goes nowhere on most servers. Redirect output to a log file inside the job. Also check whether the submission directory still exists, since the job starts by changing into it.

How do I see the commands in a scheduled at job?

Run at -c followed by the job number from atq. It prints the whole job script, including the exported environment variables, the umask and the cd into the submission directory, followed by your commands at the end.

Do at jobs survive a reboot?

Yes. Jobs are stored as files in /var/spool/cron/atjobs on Debian and Ubuntu or /var/spool/at on RHEL and Fedora. If the scheduled time passes while the machine is down, atd runs the job when it starts again. Transient timers created with systemd-run, by contrast, do not survive a reboot.

How does the batch command decide when to run?

batch queues a job without a time. atd starts it when the load average drops below 1.5, and waits at least 60 seconds between batch job starts. Both limits are set with the -l and -b options to atd, which is worth doing on multi-core servers where 1.5 is far too low.

sponsored

Related Tools

See also