How to Use the at Command in Bash for Scheduling Tasks
Use the
at command in bash to schedule a command or script to run once at a specified future time. You type at [time], enter the commands, then press Ctrl+D to save and schedule the job.Syntax
The basic syntax of the at command is:
at [time]: Schedule commands to run at the specified time.- After running this, you enter the commands you want to run later.
- Press
Ctrl+Dto finish and save the job.
bash
at 14:30 # type commands here # press Ctrl+D to save
Example
This example schedules a message to be printed at 1 minute from now. It shows how to enter the command and the output of listing scheduled jobs.
bash
echo "echo 'Hello from at command!'" | at now + 1 minute atq # Wait 1 minute to see the message printed in the terminal or check the output file if redirected
Output
1 Thu Jun 1 14:31:00 2024 a user
Common Pitfalls
- Not pressing
Ctrl+Dafter typing commands will not save the job. - Using incorrect time formats can cause errors; use formats like
HH:MM,now + 5 minutes, ortomorrow. atjobs run with your user permissions, so environment variables or paths may differ.- Some systems may require
atdservice to be running foratto work.
bash
echo "date" | at 25:00 # Wrong time format, will cause error # Correct way: echo "date" | at 23:00
Quick Reference
| Command | Description |
|---|---|
| at [time] | Schedule commands to run once at the specified time |
| atq | List pending at jobs |
| atrm [job_number] | Remove a scheduled at job |
| Ctrl+D | End input and save the at job |
Key Takeaways
Use
at [time] to schedule one-time commands in bash.Type your commands after running
at and press Ctrl+D to save.Check scheduled jobs with
atq and remove them with atrm.Ensure the
atd service is running for at to work properly.Use correct time formats like
HH:MM, now + 5 minutes, or tomorrow.