Linux › Section G · System configuration › Topic G.6

One-time tasks with at and user timer units

Not every job repeats. Sometimes you want a file removed at midnight, a reminder in two hours or a script run when the machine is quiet. The at command, transient systemd timers and per-user timer units cover these cases without editing a crontab.

Run it once, later

The recurring tools in the previous topic fit work that repeats. For a single future run there are three common choices. at takes a command and a time and runs it once through the atd daemon. systemd-run can create a temporary, self-deleting timer for a command. And a user timer unit, a normal timer kept in your own home directory, lets you schedule personal jobs without root. All three leave a record you can list and cancel, which a background sleep 7200 followed by a command does not: that approach disappears when the terminal closes and cannot be inspected.

The at command

at TIME starts an interactive prompt, at>, where you type the commands to run, one per line, and finish with Ctrl-D. Alternatives: echo 'rm /tmp/big.iso' | at 23:00, or at -f cleanup.sh 23:00 to read the commands from a file. at prints a job number and the planned time. The job runs later with the working directory, environment variables and umask that were in effect when you queued it. It needs the atd service to be running, and on some distributions the at package is not installed by default, so you may need to install it first. By default output is mailed to you, so redirect output in the command if no mail system is configured.

Time specifications

at understands clock times such as 17:30 or 5:30pm, the words noon, midnight and teatime (4 p.m.), and relative forms such as now + 90 minutes, with units of minutes, hours, days or weeks. Add today or tomorrow after a time to choose the day, or give a date: Nov 20, 11/20/26 or 2026-11-20. If the time you name has already passed today, at assumes tomorrow. The -t option takes the format [[CC]YY]MMDDhhmm, the same as touch -t. Because the rules are forgiving, always read back the time that at prints before you walk away.

Worked example: resolving at times

Assume it is Friday 6 November 2026, 15:40. 1. at 17:30 is later today: Friday 17:30. 2. at 15:00 has already passed, so the job is set for tomorrow: Saturday 7 November, 15:00. 3. at now + 90 minutes adds 1 h 30 min to 15:40: Friday 17:10. 4. at teatime means 16:00, so Friday 16:00. 5. at 09:00 tomorrow is Saturday 7 November, 09:00. Check example 3: 15:40 + 1:30 = 17:10. The surprise is example 2, which is the case where a wrong assumption would silently run the job a day late.

Managing the queue

atq lists your pending jobs with the job number, time, queue letter and owner; root sees everyone's. atrm 7 deletes job 7. at -c 7 prints the job's full script, including the environment it will run in, which is the best way to check what will happen. You can also use at -l and at -d as aliases for atq and atrm. Access is controlled by two files: if /etc/at.allow exists only the users in it may use at; otherwise users not listed in /etc/at.deny may; if neither exists, only root may. Jobs submitted with at -m send a mail message when they finish, even without output.

batch: wait for a quiet machine

batch is like at, but with no clock time. The job starts when the system load average drops below a threshold set by atd, so heavy work such as a large compression yields to interactive use. Type batch, enter the commands, and finish with Ctrl-D. Jobs go into a separate queue (b by default, while at uses a), and you see them in atq as usual. batch suits work that must happen sometime soon but not at a fixed moment. If the load never drops, the job waits, so it is not suitable for anything with a deadline.

Transient timers with systemd-run

systemd-run --on-active=2h --unit=tidy /usr/local/bin/tidy.sh creates a timer that fires two hours after it is created and then runs the command as a service. --on-calendar='2026-11-07 09:00' sets a wall-clock moment instead. The unit name is generated unless you pass --unit. Creating the timer prints lines such as Running as unit: tidy.timer and Will run service as unit: tidy.service (illustrative). systemctl list-timers shows it, journalctl -u tidy.service shows its output, and systemctl stop tidy.timer cancels it. --collect unloads the units after the job. Add --user to schedule in your own manager, without root.

Worked example: at versus systemd-run

The same moment, 15:40 on Friday 6 November 2026. Task one: run tidy.sh in two hours. 1. With at: echo '/usr/local/bin/tidy.sh' | at now + 2 hours. 2. With systemd-run: systemd-run --user --on-active=2h --unit=tidy /usr/local/bin/tidy.sh. 3. Both run at 17:40. 4. Cancel with atrm and the job number, or systemctl --user stop tidy.timer. Task two: run it on Saturday 7 November at 09:00. 5. at -f tidy.sh 09:00 tomorrow, or systemd-run --user --on-calendar='2026-11-07 09:00:00' --unit=tidy2 /usr/local/bin/tidy.sh. Check: 15:40 + 2 h = 17:40. The systemd route gives journal logging and one place to list timers.

User timer units

A per-user systemd manager reads units from ~/.config/systemd/user/ (it takes precedence over /etc/systemd/user and /usr/lib/systemd/user). Put a notes-sync.service and a notes-sync.timer there, then run systemctl --user daemon-reload and systemctl --user enable --now notes-sync.timer. List with systemctl --user list-timers and read logs with journalctl --user-unit notes-sync.service. A user manager normally runs only while you are logged in. To let timers fire when you are not, run loginctl enable-linger USERNAME (policy may require an administrator), which starts your manager at boot and keeps it after logout. WantedBy=timers.target works in user mode as well.

Common mistakes to avoid

First, assuming at 15:00 means today when 15:00 has already passed. Second, relying on mail for output on a machine without a mail system; redirect it to a file. Third, forgetting that the job's environment was captured at queue time. Fourth, using sleep in the background for delays that must survive logout. Fifth, enabling user timers and expecting them to run while logged out without lingering. Sixth, forgetting that atd must be running, or that the at package may not be installed. Seventh, thinking batch has a deadline.

Key takeaways

at runs commands once from an interactive prompt, a pipe or -f file; atq lists, atrm removes, at -c shows the script. Times may be clock, named (noon, teatime) or relative (now + 90 minutes), and a passed time rolls to tomorrow. /etc/at.allow and /etc/at.deny control access. batch waits for low load. systemd-run --on-active or --on-calendar creates transient timers; user timers live in ~/.config/systemd/user and need systemctl --user, plus lingering to run while logged out.

Flashcards

Flashcard

at TIME

Schedules commands to run once at a given time

Type commands, then Ctrl-D.

Test your understanding

Question 1 of 5

It is Friday 15:40. You type at 15:00 and enter a command. When will it run?

Question 2 of 5

Which command lists your pending at jobs?

Question 3 of 5

It is 15:40 on Friday. What time does systemd-run --on-active=2h fire?

Question 4 of 5

A user timer should keep firing at 03:00 although the user has logged out. What is needed?

Question 5 of 5

How does batch differ from at?

← Previous topicNext topic →