How to list/remove at jobs → atq, atrm
How to List and Remove AT Jobs Using atq and atrm Commands
Table of Contents
1. [Introduction](#introduction)
2. [Prerequisites](#prerequisites)
3. [Understanding AT Jobs](#understanding-at-jobs)
4. [Listing AT Jobs with atq](#listing-at-jobs-with-atq)
5. [Removing AT Jobs with atrm](#removing-at-jobs-with-atrm)
6. [Practical Examples and Use Cases](#practical-examples-and-use-cases)
7. [Advanced Usage and Options](#advanced-usage-and-options)
8. [Common Issues and Troubleshooting](#common-issues-and-troubleshooting)
9. [Best Practices and Tips](#best-practices-and-tips)
10. [Security Considerations](#security-considerations)
11. [Conclusion](#conclusion)
Introduction
The AT command suite in Linux provides a powerful mechanism for scheduling one-time tasks to run at specific times in the future. While creating AT jobs is straightforward using the `at` command, managing these scheduled jobs requires understanding two essential utilities: `atq` (AT queue) for listing pending jobs and `atrm` (AT remove) for deleting scheduled tasks.
This comprehensive guide will teach you everything you need to know about listing and removing AT jobs, from basic operations to advanced management techniques. Whether you're a system administrator managing multiple scheduled tasks or a developer automating deployment processes, mastering these commands is crucial for effective job scheduling and system maintenance.
By the end of this article, you'll understand how to efficiently monitor your scheduled AT jobs, remove unnecessary tasks, and implement best practices for AT job management in production environments.
Prerequisites
Before diving into AT job management, ensure you have the following prerequisites:
System Requirements
- A Linux-based operating system (Ubuntu, CentOS, RHEL, Debian, etc.)
- The `at` package installed on your system
- Basic familiarity with Linux command-line interface
- Understanding of file permissions and user accounts
Installation Verification
To verify that the AT daemon is installed and running, execute:
```bash
Check if at package is installed
which at atq atrm
Verify atd daemon is running
systemctl status atd
or for older systems
service atd status
```
User Permissions
- Standard users can manage their own AT jobs
- Root access required to view and manage other users' jobs
- Understanding of `/etc/at.allow` and `/etc/at.deny` files
Basic AT Command Knowledge
While this guide focuses on listing and removing jobs, basic familiarity with creating AT jobs using the `at` command will enhance your understanding of the examples provided.
Understanding AT Jobs
What Are AT Jobs?
AT jobs are one-time scheduled tasks that execute commands or scripts at specified future times. Unlike cron jobs that run repeatedly, AT jobs execute once and are automatically removed from the queue after completion.
AT Job Lifecycle
1. Creation: Jobs are created using the `at` command
2. Queuing: Jobs wait in the AT queue until their scheduled time
3. Execution: The AT daemon (`atd`) executes jobs at the specified time
4. Completion: Jobs are automatically removed after successful execution
5. Manual Removal: Jobs can be manually removed before execution using `atrm`
AT Job Components
Each AT job consists of:
- Job ID: Unique identifier assigned when the job is created
- Execution Time: When the job is scheduled to run
- User: The user who created the job
- Queue: The queue letter (default is 'a')
- Commands: The actual commands or script to execute
Listing AT Jobs with atq
Basic atq Usage
The `atq` command displays all pending AT jobs in the queue. The basic syntax is:
```bash
atq [options]
```
Understanding atq Output
When you run `atq`, the output format is:
```
Job_ID Date Time Queue User
```
Example output:
```bash
$ atq
5 Tue Dec 12 15:30:00 2023 a john
7 Wed Dec 13 09:00:00 2023 a john
12 Thu Dec 14 18:45:00 2023 b mary
```
Column Explanations
- Job ID: Unique numerical identifier for the job
- Date/Time: When the job is scheduled to execute
- Queue: The queue letter (a-z, with 'a' being default)
- User: The username who created the job
atq Command Options
Viewing All Jobs (Root Only)
```bash
View all users' AT jobs (requires root privileges)
sudo atq
```
Filtering by User
```bash
View jobs for a specific user (root only)
sudo atq -u username
```
Verbose Output
Some systems support verbose output:
```bash
Display additional job information
atq -v
```
Practical atq Examples
Example 1: Basic Job Listing
```bash
Create a test job
echo "echo 'Hello World'" | at now + 1 hour
List all your jobs
atq
```
Output:
```
15 Tue Dec 12 16:45:00 2023 a currentuser
```
Example 2: Multiple Jobs in Different Queues
```bash
Create jobs in different queues
echo "backup_database.sh" | at -q a tomorrow
echo "system_maintenance.sh" | at -q b "next monday"
echo "report_generation.py" | at -q c "Dec 25 09:00"
View all jobs
atq
```
Output:
```
16 Wed Dec 13 16:45:00 2023 a admin
17 Mon Dec 18 16:45:00 2023 b admin
18 Mon Dec 25 09:00:00 2023 c admin
```
Example 3: System Administrator View
```bash
As root, view all users' jobs
sudo atq
```
Output:
```
15 Tue Dec 12 16:45:00 2023 a john
16 Wed Dec 13 10:30:00 2023 a mary
17 Thu Dec 14 14:00:00 2023 b bob
18 Fri Dec 15 08:00:00 2023 a alice
```
Removing AT Jobs with atrm
Basic atrm Usage
The `atrm` command removes one or more pending AT jobs from the queue. The basic syntax is:
```bash
atrm job_id [job_id2 job_id3 ...]
```
Single Job Removal
To remove a specific job, use its job ID:
```bash
Remove job with ID 15
atrm 15
```
Multiple Job Removal
Remove multiple jobs by specifying multiple job IDs:
```bash
Remove jobs with IDs 15, 17, and 20
atrm 15 17 20
```
Verification of Removal
Always verify that jobs were successfully removed:
```bash
Remove job and verify
atrm 15
atq # Check if job 15 is gone
```
Practical atrm Examples
Example 1: Basic Job Removal
```bash
List current jobs
atq
Output: 25 Wed Dec 13 10:00:00 2023 a john
Remove the job
atrm 25
Verify removal
atq
Output: (empty - job removed)
```
Example 2: Bulk Job Removal
```bash
List multiple jobs
atq
Output:
30 Thu Dec 14 09:00:00 2023 a john
31 Thu Dec 14 10:00:00 2023 a john
32 Thu Dec 14 11:00:00 2023 a john
Remove all three jobs
atrm 30 31 32
Verify removal
atq
Output: (empty - all jobs removed)
```
Example 3: Error Handling
```bash
Attempt to remove non-existent job
atrm 999
Output: atrm: 999: No such job
Attempt to remove another user's job (without privileges)
atrm 45
Output: atrm: 45: Permission denied
```
Practical Examples and Use Cases
Use Case 1: Automated Backup Management
```bash
Create backup jobs
echo "/home/scripts/daily_backup.sh" | at 02:00 tomorrow
echo "/home/scripts/weekly_backup.sh" | at 03:00 sunday
echo "/home/scripts/monthly_backup.sh" | at 04:00 "first day of next month"
List all backup jobs
atq
Output:
40 Wed Dec 13 02:00:00 2023 a admin
41 Sun Dec 17 03:00:00 2023 a admin
42 Mon Jan 1 04:00:00 2024 a admin
Cancel weekly backup due to maintenance
atrm 41
Verify cancellation
atq
Output:
40 Wed Dec 13 02:00:00 2023 a admin
42 Mon Jan 1 04:00:00 2024 a admin
```
Use Case 2: Deployment Pipeline Management
```bash
Schedule deployment stages
echo "/opt/deploy/stage1_build.sh" | at "Dec 15 20:00"
echo "/opt/deploy/stage2_test.sh" | at "Dec 15 21:00"
echo "/opt/deploy/stage3_deploy.sh" | at "Dec 15 22:00"
List deployment jobs
atq
Output:
50 Fri Dec 15 20:00:00 2023 a devops
51 Fri Dec 15 21:00:00 2023 a devops
52 Fri Dec 15 22:00:00 2023 a devops
Emergency: Cancel deployment due to critical bug
atrm 50 51 52
Verify all stages cancelled
atq
Output: (empty)
```
Use Case 3: System Maintenance Scheduling
```bash
Schedule various maintenance tasks
echo "systemctl restart apache2" | at 01:00 sunday
echo "/usr/bin/updatedb" | at 02:00 sunday
echo "apt update && apt upgrade -y" | at 03:00 sunday
echo "/home/scripts/log_cleanup.sh" | at 04:00 sunday
List maintenance jobs
atq
Output:
60 Sun Dec 17 01:00:00 2023 a root
61 Sun Dec 17 02:00:00 2023 a root
62 Sun Dec 17 03:00:00 2023 a root
63 Sun Dec 17 04:00:00 2023 a root
Cancel Apache restart (not needed this week)
atrm 60
Verify specific job removal
atq
Output:
61 Sun Dec 17 02:00:00 2023 a root
62 Sun Dec 17 03:00:00 2023 a root
63 Sun Dec 17 04:00:00 2023 a root
```
Advanced Usage and Options
Working with Different Queues
AT supports multiple queues (a-z) with different priority levels:
```bash
Create jobs in different priority queues
echo "high_priority_task.sh" | at -q a now + 1 hour # Highest priority
echo "medium_priority_task.sh" | at -q m now + 1 hour # Medium priority
echo "low_priority_task.sh" | at -q z now + 1 hour # Lowest priority
List all jobs (shows queue information)
atq
Output:
70 Tue Dec 12 17:45:00 2023 a user
71 Tue Dec 12 17:45:00 2023 m user
72 Tue Dec 12 17:45:00 2023 z user
```
Batch Command Alternative
The `batch` command is equivalent to `at -q b` and runs jobs when system load permits:
```bash
Create batch job
echo "intensive_process.sh" | batch
List jobs (will show in queue 'b')
atq
Output:
75 Tue Dec 12 16:45:00 2023 b user
Remove batch job
atrm 75
```
Scripting AT Job Management
Script to Clean Old Jobs
```bash
#!/bin/bash
cleanup_old_at_jobs.sh
Get current timestamp
current_time=$(date +%s)
Get all AT jobs and process them
atq | while read job_id date time year queue user; do
# Convert job time to timestamp
job_time=$(date -d "$date $time $year" +%s 2>/dev/null)
# If job is older than 7 days from now, remove it
if [ $? -eq 0 ] && [ $((job_time - current_time)) -lt -604800 ]; then
echo "Removing old job: $job_id scheduled for $date $time $year"
atrm $job_id
fi
done
```
Script to List Jobs by User
```bash
#!/bin/bash
list_user_jobs.sh
if [ "$1" = "" ]; then
echo "Usage: $0 "
exit 1
fi
username=$1
echo "AT jobs for user: $username"
echo "================================"
Must run as root to see other users' jobs
if [ $(id -u) -ne 0 ]; then
echo "This script must be run as root to view other users' jobs"
exit 1
fi
atq | grep "$username$" | while read job_id date time year queue user; do
echo "Job ID: $job_id"
echo "Scheduled: $date $time $year"
echo "Queue: $queue"
echo "---"
done
```
Common Issues and Troubleshooting
Issue 1: Permission Denied Errors
Problem: Users cannot remove jobs or see certain jobs.
Symptoms:
```bash
$ atrm 25
atrm: 25: Permission denied
```
Solutions:
```bash
Check job ownership
atq # Only shows your jobs
As root, check all jobs
sudo atq
Users can only remove their own jobs
Root can remove any job
sudo atrm 25
```
Issue 2: AT Daemon Not Running
Problem: Jobs are not executing or commands don't work.
Symptoms:
```bash
$ atq
atq: can't open /var/spool/at: No such file or directory
```
Solutions:
```bash
Check if atd is running
systemctl status atd
Start atd if not running
sudo systemctl start atd
Enable atd to start on boot
sudo systemctl enable atd
For older systems
sudo service atd start
sudo chkconfig atd on
```
Issue 3: Jobs Not Appearing in Queue
Problem: Created jobs don't show up when running `atq`.
Debugging Steps:
```bash
Check if job was actually created
echo "test command" | at now + 1 minute
Note the job number returned
Immediately check queue
atq
Check AT spool directory permissions
ls -la /var/spool/at/
Check system logs for AT daemon messages
sudo journalctl -u atd
or
sudo tail -f /var/log/messages | grep atd
```
Issue 4: Invalid Job ID Errors
Problem: Trying to remove jobs that don't exist.
Symptoms:
```bash
$ atrm 999
atrm: 999: No such job
```
Solutions:
```bash
Always list jobs first to get correct IDs
atq
Verify job ID exists before removal
if atq | grep -q "^999 "; then
atrm 999
else
echo "Job 999 does not exist"
fi
```
Issue 5: Access Control Problems
Problem: Users blocked from using AT commands.
Investigation:
```bash
Check AT access control files
sudo cat /etc/at.allow # If exists, only listed users can use AT
sudo cat /etc/at.deny # If exists, listed users cannot use AT
Add user to allow file
echo "username" | sudo tee -a /etc/at.allow
Remove user from deny file
sudo sed -i '/^username$/d' /etc/at.deny
```
Issue 6: Time Zone and Scheduling Issues
Problem: Jobs scheduled for wrong times due to timezone issues.
Debugging:
```bash
Check system timezone
timedatectl
Verify AT daemon timezone
sudo systemctl show atd | grep Environment
Create job with explicit timezone
TZ=UTC echo "test" | at "Dec 25 12:00"
```
Best Practices and Tips
1. Regular Queue Monitoring
Implement regular monitoring of your AT job queue:
```bash
Create a daily report of pending jobs
#!/bin/bash
daily_at_report.sh
echo "Daily AT Job Report - $(date)"
echo "================================"
echo "Pending Jobs:"
atq | wc -l
echo ""
echo "Job Details:"
atq
echo ""
echo "Jobs by Queue:"
atq | awk '{print $5}' | sort | uniq -c
```
2. Implement Job Naming Conventions
Use descriptive commands that help identify job purposes:
```bash
Good: Descriptive job creation
echo "# Daily backup job - $(date)" > /tmp/backup_job.sh
echo "/opt/scripts/daily_backup.sh" >> /tmp/backup_job.sh
at -f /tmp/backup_job.sh tomorrow
Instead of: Unclear job
echo "/opt/scripts/db.sh" | at tomorrow
```
3. Use Logging for Job Management
Create logs when removing jobs:
```bash
#!/bin/bash
safe_atrm.sh - Remove AT job with logging
if [ $# -eq 0 ]; then
echo "Usage: $0 [job_id2 ...]"
exit 1
fi
log_file="/var/log/at_management.log"
for job_id in "$@"; do
# Get job details before removal
job_details=$(atq | grep "^$job_id ")
if [ -n "$job_details" ]; then
echo "$(date): Removing job $job_id: $job_details" >> "$log_file"
atrm "$job_id"
echo "Job $job_id removed successfully"
else
echo "$(date): Attempted to remove non-existent job $job_id" >> "$log_file"
echo "Job $job_id not found"
fi
done
```
4. Implement Queue Management Strategies
Organize jobs using different queues based on priority and type:
```bash
System maintenance (low priority)
echo "system_cleanup.sh" | at -q z 02:00 sunday
Business critical (high priority)
echo "critical_backup.sh" | at -q a 01:00 daily
User requests (medium priority)
echo "user_report.py" | at -q m 09:00 tomorrow
```
5. Create Wrapper Scripts for Common Operations
```bash
#!/bin/bash
at_manager.sh - Comprehensive AT job management
case "$1" in
"list")
echo "Current AT Jobs:"
atq | sort -k2,3
;;
"remove")
shift
for job in "$@"; do
echo "Removing job $job..."
atrm "$job"
done
;;
"clean")
echo "Removing all your AT jobs..."
atq | awk '{print $1}' | xargs -r atrm
;;
"summary")
total=$(atq | wc -l)
echo "Total pending jobs: $total"
echo "Jobs by queue:"
atq | awk '{print $5}' | sort | uniq -c
;;
*)
echo "Usage: $0 {list|remove |clean|summary}"
exit 1
;;
esac
```
6. Security Best Practices
- Regularly audit AT job queues for unauthorized jobs
- Monitor AT access control files (`/etc/at.allow`, `/etc/at.deny`)
- Log all AT job management activities
- Use principle of least privilege for AT job access
7. Backup and Recovery
```bash
Backup AT jobs (job details, not the jobs themselves)
atq > /backup/at_jobs_$(date +%Y%m%d).txt
Create restoration documentation
echo "# AT Jobs Backup - $(date)" > /backup/at_restore_notes.txt
echo "# Use this information to recreate jobs if needed" >> /backup/at_restore_notes.txt
atq >> /backup/at_restore_notes.txt
```
Security Considerations
Access Control
Understanding and properly configuring AT access control is crucial for system security:
AT Access Files
- `/etc/at.allow`: If this file exists, only users listed can use AT commands
- `/etc/at.deny`: If this file exists, users listed cannot use AT commands
- If neither file exists, only root can use AT commands (on some systems)
Best Practices for Access Control
```bash
Create restrictive allow file
sudo touch /etc/at.allow
sudo chmod 640 /etc/at.allow
Add trusted users
echo "admin" | sudo tee -a /etc/at.allow
echo "backup_user" | sudo tee -a /etc/at.allow
Verify configuration
sudo cat /etc/at.allow
```
Monitoring and Auditing
Implement monitoring for AT job activities:
```bash
#!/bin/bash
at_audit.sh - Monitor AT job activities
Check for suspicious jobs
suspicious_patterns=("rm -rf" "curl.|.sh" "wget.|.sh" "/tmp/.*\.sh")
echo "AT Job Security Audit - $(date)"
echo "================================="
atq | while read job_id date time year queue user; do
# Get job content (requires root)
job_file="/var/spool/at/a$(printf "%05d" $job_id)"
if [ -f "$job_file" ]; then
for pattern in "${suspicious_patterns[@]}"; do
if grep -q "$pattern" "$job_file" 2>/dev/null; then
echo "WARNING: Suspicious job $job_id by $user contains: $pattern"
fi
done
fi
done
```
Preventing Abuse
Implement safeguards against AT job abuse:
```bash
Limit number of jobs per user
#!/bin/bash
check_job_limits.sh
max_jobs_per_user=10
atq | awk '{print $6}' | sort | uniq -c | while read count user; do
if [ "$count" -gt "$max_jobs_per_user" ]; then
echo "WARNING: User $user has $count jobs (limit: $max_jobs_per_user)"
# Optionally remove excess jobs or send alert
fi
done
```
Conclusion
Mastering the `atq` and `atrm` commands is essential for effective AT job management in Linux environments. These tools provide the foundation for monitoring, controlling, and maintaining scheduled one-time tasks across your systems.
Key Takeaways
1. atq Command: Use `atq` to list all pending AT jobs, showing job IDs, scheduled times, queues, and users
2. atrm Command: Use `atrm ` to remove specific jobs from the queue before execution
3. Permission Model: Users can only manage their own jobs; root can manage all jobs
4. Queue Management: Utilize different queues (a-z) for organizing jobs by priority or type
5. Monitoring Importance: Regular monitoring prevents queue buildup and identifies potential issues
Best Practices Summary
- Always verify job removal with `atq` after using `atrm`
- Implement proper access controls using `/etc/at.allow` and `/etc/at.deny`
- Create descriptive job commands for easier identification
- Use logging for audit trails of job management activities
- Regularly monitor job queues for security and performance
- Implement automated cleanup for old or unnecessary jobs
Next Steps
To further enhance your AT job management skills:
1. Explore Cron Integration: Learn how AT jobs complement cron for comprehensive scheduling
2. Advanced Scripting: Develop custom scripts for automated AT job management
3. System Integration: Integrate AT job monitoring into your system monitoring infrastructure
4. Security Hardening: Implement comprehensive security policies for scheduled job management
Final Recommendations
The `atq` and `atrm` commands are powerful tools that, when used properly, provide excellent control over scheduled tasks. Regular practice with these commands, combined with proper security measures and monitoring practices, will ensure efficient and secure AT job management in your Linux environment.
Remember that effective job scheduling is not just about creating tasks—it's equally important to maintain, monitor, and clean up your scheduled jobs to keep your system running optimally. The techniques and best practices outlined in this guide will help you achieve professional-level AT job management capabilities.