1080*80 ad

Linux: Task Scheduling with the `at` Command

Mastering Linux Task Scheduling with the at Command

In the world of Linux system administration, automation is key to efficiency and reliability. While the cron daemon is famous for handling recurring tasks, there’s another powerful, yet often overlooked, utility designed for a different purpose: scheduling one-time tasks. This tool is the at command, and mastering it can significantly streamline your workflow.

Whether you need to run a resource-intensive backup script after hours, reboot a server at a specific time, or simply send yourself a reminder, the at command provides a simple and flexible solution.

What is the at Command?

The at command is a command-line utility that allows you to schedule commands or scripts to be executed once at a specified future time. Unlike cron, which operates on a recurring schedule (e.g., every Tuesday at 2 AM), at is designed for jobs that you only need to run a single time.

The command works by reading a series of commands from standard input or a file and bundling them into an at job. This job is then spooled, and the atd daemon checks periodically to execute any jobs whose scheduled time has arrived.

Getting Started: Installation and Setup

On many modern Linux distributions, the at package may not be installed by default. You can easily install it using your system’s package manager.

For Debian/Ubuntu systems:

sudo apt update
sudo apt install at

For RHEL/CentOS/Fedora systems:

sudo dnf install at

After installation, you need to ensure the atd service is enabled and running. This daemon is responsible for executing the scheduled jobs.

sudo systemctl enable --now atd

You can verify its status with sudo systemctl status atd.

How to Schedule a Job with at

There are several ways to schedule a job, catering to different needs from simple commands to complex scripts.

1. The Interactive Method

The most straightforward way to use at is interactively. You simply type at followed by the desired time. The command prompt will then change, allowing you to enter the commands you want to run.

For example, to schedule a system update for 11:00 PM tonight:

at 11:00 PM

The prompt will change to at>. Now, enter your commands:

at> sudo apt update && sudo apt upgrade -y
at>

To save the job and exit the prompt, press Ctrl+D. You will receive a confirmation message like job 1 at Tue Nov 21 23:00:00 2023.

2. Piping Commands

For simple, one-line commands, you can pipe the command directly to at using echo. This is a quick and efficient method.

To create a temporary file in 10 minutes:

echo "touch /tmp/testfile.txt" | at now + 10 minutes
3. Scheduling from a File

For any complex or multi-line tasks, the best practice is to place your commands in a shell script and schedule the script for execution. This method is cleaner, more reliable, and easier to debug.

First, create your script (e.g., backup.sh):

#!/bin/bash
# A simple backup script
echo "Starting backup at $(date)" > /var/log/backup.log
tar -czf /opt/backups/home_$(date +%F).tar.gz /home/user
echo "Backup finished at $(date)" >> /var/log/backup.log

Make sure the script is executable:

chmod +x backup.sh

Now, schedule it to run at 2:30 AM tomorrow using the -f flag:

at 02:30 tomorrow -f /path/to/backup.sh

Understanding Time and Date Formats

The at command accepts a wide variety of human-readable time formats, making it incredibly intuitive.

  • Absolute Time: HH:MM (e.g., at 16:30), HH:MM AM/PM (e.g., at 4:30 PM).
  • Specific Date and Time: Month Day (e.g., at 10:00 AM Jan 31), MMDDYY, MM/DD/YY, etc.
  • Relative Time: Use now + followed by a count and unit (e.g., at now + 1 hour, at now + 30 minutes, at now + 2 days).
  • Keywords: You can also use convenient keywords like noon, midnight, teatime (4 PM), and tomorrow.

Managing Your Scheduled Jobs

Once you have jobs scheduled, you need to know how to manage them.

  • List Pending Jobs: To see a queue of all your pending jobs, use the atq command (or at -l). The output shows the job ID, the scheduled date and time, and the user who scheduled it.

    atq
    # Output:
    # 2   Wed Nov 22 02:30:00 2023 a root
    # 1   Tue Nov 21 23:00:00 2023 a user
    
  • View a Job’s Content: To inspect the commands within a scheduled job, use at -c followed by the job ID. This is extremely useful for verification.

    at -c 2
    
  • Delete a Scheduled Job: If you need to cancel a job, use the atrm command (or at -d) followed by the job ID.
    bash
    atrm 2

Security Considerations

By default, most systems allow any user to schedule at jobs. For security, you can restrict access using two files: /etc/at.allow and /etc/at.deny.

  • /etc/at.allow: If this file exists, only users listed in it are permitted to use the at command. All other users are denied.
  • /etc/at.deny: If at.allow does not exist, this file is checked. Any user listed in at.deny is blocked from using at.

Security Tip: For a more secure system, it is recommended to create an /etc/at.allow file and explicitly list only the users who require at privileges. This follows the principle of least privilege, ensuring that only authorized accounts can schedule future tasks.

Source: https://kifarunix.com/scheduling-tasks-using-at-command-in-linux/

900*80 ad

      1080*80 ad