Bird
Raised Fist0
Linux CLIscripting~10 mins

at command for one-time jobs in Linux CLI - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - at command for one-time jobs
User types 'at' with time
↓
System waits for commands
↓
User enters commands
↓
User signals end (Ctrl+D)
↓
System schedules job
↓
Job runs at scheduled time
↓
Job output sent to user (optional)
The 'at' command schedules commands to run once at a specified future time. User inputs commands, ends input, then system runs them later.
Execution Sample
Linux CLI
echo "echo Hello at 5 seconds" | at now + 5 seconds
Schedules a one-time job to print 'Hello at 5 seconds' after 5 seconds.
Execution Table
StepActionInput/CommandSystem ResponseOutput
1User runs at commandat now + 5 secondsat waits for commands
2User enters commandecho Hello at 5 secondsCommand accepted
3User signals endCtrl+DJob scheduledjob 1 at Thu Apr 27 12:00:05 2024
4Time passes 5 secondsJob runsHello at 5 seconds
5Job completesJob removed from queue
💡 Job runs once at scheduled time and then is removed from queue
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
Job Queueemptyjob 1 addedjob 1 scheduledjob 1 runningempty
Outputnonenonenone"Hello at 5 seconds"none
Key Moments - 3 Insights
Why does the 'at' command wait for input after I run it?
Because 'at' expects you to type the commands to run later. See execution_table step 1 and 2 where system waits and accepts commands.
How do I tell 'at' I finished entering commands?
You press Ctrl+D to signal end of input. This is shown in execution_table step 3.
What happens if I schedule a job but the time never comes?
The job stays in the queue until the scheduled time. It runs once then is removed, as shown in variable_tracker for Job Queue.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what command does the user schedule at step 2?
Aecho Hello at 5 seconds
Bat now + 5 seconds
CCtrl+D
Djob 1 at Thu Apr 27 12:00:05 2024
💡 Hint
Check the 'Input/Command' column at step 2 in the execution_table.
At which step does the job actually run and produce output?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the 'System Response' and 'Output' columns in execution_table step 4.
If the user forgets to press Ctrl+D, what happens?
AJob is scheduled immediately
Bat command keeps waiting for input
CJob runs twice
DJob is canceled automatically
💡 Hint
See key_moments about how 'at' waits for input until Ctrl+D is pressed.
Concept Snapshot
at command schedules one-time jobs
Syntax: at [time]
Type commands, end with Ctrl+D
Job runs once at scheduled time
Output sent by email or terminal
Use 'atq' to list jobs
Full Transcript
The 'at' command lets you schedule commands to run once at a future time. You run 'at' with a time, then type the commands you want to run later. When done, press Ctrl+D to finish input. The system schedules the job and runs it at the specified time. After running, the job is removed from the queue. For example, 'echo "echo Hello at 5 seconds" | at now + 5 seconds' schedules a job to print a message after 5 seconds. The job queue holds scheduled jobs until they run. If you forget to press Ctrl+D, 'at' keeps waiting for your commands. You can check scheduled jobs with 'atq'.

Practice

(1/5)
1. What is the primary purpose of the at command in Linux?
easy
A. To list all running processes
B. To schedule a one-time job to run at a specific time
C. To monitor system resource usage continuously
D. To create a recurring job every day

Solution

  1. Step 1: Understand the at command function

    The at command is designed to schedule commands or scripts to run once at a specified future time.
  2. Step 2: Compare with other options

    Options A, C, and D describe other commands or functionalities unrelated to at. For example, listing processes is done by ps, monitoring resources by top, and recurring jobs by cron.
  3. Final Answer:

    To schedule a one-time job to run at a specific time -> Option B
  4. Quick Check:

    at schedules one-time jobs = B [OK]
Hint: Remember: at is for one-time future tasks [OK]
Common Mistakes:
  • Confusing at with cron for recurring jobs
  • Thinking at lists running processes
  • Assuming at monitors system resources
2. Which of the following is the correct syntax to schedule a job at 3:30 PM using the at command?
easy
A. at 3-30
B. at 3.30pm
C. at 15:30pm
D. at 3:30pm

Solution

  1. Step 1: Identify valid time format for at

    The at command accepts time in formats like HH:MMam or HH:MMpm. So 3:30pm is valid.
  2. Step 2: Check other options for syntax errors

    B: at 3.30pm uses dot instead of colon, invalid. C: at 15:30pm mixes 24-hour format with pm, invalid. D: at 3-30 uses dash instead of colon, invalid.
  3. Final Answer:

    at 3:30pm -> Option D
  4. Quick Check:

    Correct at time format = A [OK]
Hint: Use colon and am/pm for time with at [OK]
Common Mistakes:
  • Using dot or dash instead of colon in time
  • Omitting am/pm in 12-hour format
  • Confusing 24-hour and 12-hour formats
3. What will be the output of the following commands sequence?
echo "echo Hello World" | at now + 1 minute
sleep 70
atq
medium
A. No jobs listed because the job already ran
B. Shows an error because at cannot read from pipe
C. Lists the scheduled job with its job number
D. Prints 'Hello World' immediately

Solution

  1. Step 1: Understand the job scheduling and timing

    The command schedules a job to run 1 minute from now that prints 'Hello World'. Then the script waits 70 seconds (more than 1 minute), so the job runs during sleep.
  2. Step 2: Check the atq output after sleep

    Since the job already executed, atq will show no pending jobs.
  3. Final Answer:

    No jobs listed because the job already ran -> Option A
  4. Quick Check:

    Job runs after 1 min, sleep 70s, then atq empty = C [OK]
Hint: Wait past scheduled time, then atq shows no jobs [OK]
Common Mistakes:
  • Expecting atq to list completed jobs
  • Thinking at cannot read from pipe
  • Assuming immediate output from scheduled job
4. You tried to schedule a job with at 25:00 but got an error. What is the likely cause and how to fix it?
medium
A. You must run at as root user
B. Missing command after at; add a command
C. Invalid hour; use a valid 24-hour time like 23:00
D. The at daemon is not installed

Solution

  1. Step 1: Identify the error cause

    The hour 25 is invalid because valid 24-hour times range from 00 to 23.
  2. Step 2: Correct the time format

    Use a valid time like 23:00 or a 12-hour format with am/pm.
  3. Final Answer:

    Invalid hour; use a valid 24-hour time like 23:00 -> Option C
  4. Quick Check:

    Hour must be 0-23 in at time = A [OK]
Hint: Hours must be 0-23 in 24-hour format for at [OK]
Common Mistakes:
  • Using invalid hour values above 23
  • Assuming at needs root for scheduling
  • Confusing missing command error with time format error
5. You want to schedule a script /home/user/backup.sh to run once at 11:45 PM today and then remove it from the queue after scheduling. Which sequence of commands achieves this correctly?
hard
A. echo "/home/user/backup.sh" | at 11:45pm && atrm $(atq | awk '{print $1}')
B. at 11:45pm < /home/user/backup.sh && atrm $(atq | awk '{print $1}')
C. at 11:45pm <<< "/home/user/backup.sh" && atrm $(atq | awk '{print $1}')
D. echo "/home/user/backup.sh" | at 11:45pm && atq && atrm $(atq | awk '{print $1}')

Solution

  1. Step 1: Schedule the script using echo piped to at

    This schedules the command to run at 11:45pm by piping the script path as a command.
  2. Step 2: Remove the job from the queue immediately after scheduling

    Use atrm with the job number obtained from atq. echo "/home/user/backup.sh" | at 11:45pm && atrm $(atq | awk '{print $1}') correctly schedules and then removes the job.
  3. Final Answer:

    echo "/home/user/backup.sh" | at 11:45pm && atrm $(atq | awk '{print $1}') -> Option A
  4. Quick Check:

    Pipe command to at, then remove job = D [OK]
Hint: Pipe command to at, then remove job with atrm [OK]
Common Mistakes:
  • Using input redirection with script file instead of command
  • Removing job before it is scheduled
  • Confusing atq output format for job ID
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard