0
0
Linux CLIscripting~5 mins

at command for one-time jobs in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want your computer to do a task just once at a specific time later. The 'at' command lets you schedule these one-time jobs easily without needing to keep the terminal open.
When you want to run a backup script at 2 AM tonight without staying logged in.
When you need to send an email reminder automatically at a future time.
When you want to shut down your computer after finishing a long download.
When you want to run a cleanup script once after office hours.
When you want to test a command later without running it immediately.
Commands
This schedules a simple command to run at 2 AM today. It writes 'Backup started' into a file named backup.log. We use echo and pipe it to 'at' to schedule the job.
Terminal
echo "echo 'Backup started' > backup.log" | at 02:00
Expected OutputExpected
job 1 at Thu Apr 27 02:00:00 2024
This command lists all pending jobs scheduled with 'at'. It helps you check what jobs are waiting to run.
Terminal
atq
Expected OutputExpected
1 Thu Apr 27 02:00:00 2024 a user
This removes the scheduled job with ID 1. Use this if you want to cancel a job before it runs.
Terminal
atrm 1
Expected OutputExpected
No output (command runs silently)
This starts an interactive prompt to schedule commands at 2:30 PM. You type the commands you want to run, then press Ctrl+D to save and exit.
Terminal
at 14:30
Expected OutputExpected
warning: commands will be executed using /bin/sh at> echo 'Meeting reminder' > reminder.txt at> <Ctrl+D> job 2 at Thu Apr 27 14:30:00 2024
Key Concept

If you remember nothing else from this pattern, remember: the 'at' command schedules one-time tasks to run later without needing a running terminal.

Common Mistakes
Typing the time in a wrong format like '25:00' or '2pm' without spaces.
The 'at' command expects valid time formats; wrong formats cause errors or unexpected scheduling.
Use 24-hour format like '14:00' or natural language like '2 pm' with a space.
Not pressing Ctrl+D after typing commands in the interactive 'at' prompt.
Without Ctrl+D, the job is not saved and scheduled, so the commands never run.
Always press Ctrl+D to signal the end of input and save the job.
Trying to schedule a job without having the 'at' daemon running or installed.
The 'at' command depends on a background service; if it's missing, scheduling fails silently or with errors.
Ensure 'atd' service is installed and running before scheduling jobs.
Summary
Use 'echo "command" | at time' to schedule a one-time job quickly.
Check scheduled jobs with 'atq' and remove them with 'atrm job_id'.
Use 'at time' to enter interactive mode for multiple commands, ending with Ctrl+D.