Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux at Command

Linux at Command

Linux Command Reference Linux Command Reference


at is a powerful task-scheduling tool in Linux that lets users run one-time tasks at a specified time, rather than periodically like cron that way.

The difference between cron Compared toat Better suited to the following scenarios:


Installation and Basic Usage

Check the installation

Most Linux distributions have it installed by default at, which you can check with the following command:

which at

If it is not installed, you can install it with a package manager:

Example

Debian/Ubuntu

sudo

apt-get install

at

CentOS/RHEL

sudo

yum install

at

Start the at Service

After installation, you need to start atd the daemon:

Example

sudo

systemctl start atd

sudo

systemctl

enable

atd  

Enable startup on boot


Command Syntax and Parameters

Basic Syntax

at [选项] 时间

Common options

Option Description
-f 文件 Read commands from a specified file instead of standard input
-m Send an email to the user after the task completes
-l List pending tasks (same as atq)
-d 任务ID Delete a specified task (same as atrm)
-v Show the time when a task will be executed

Time Formats

at Multiple time formats are supported:

  1. Absolute time

  2. HH:MM (such as 14:30)
  3. YYYY-MM-DD (such as 2023-12-25)
  4. Combined format HH:MM YYYY-MM-DD
  5. - `HH:MM` (e.g., 14:30) - `YYYY-MM-DD` (e.g., 2023-12-25) - combined format `HH:MM YYYY-MM-DD`
    1. Relative time

    2. now + 数量 单位 (such as now + 2 hours)
    3. Units can be: minutes, hours, days, weeks
    4. - `now + amount unit` (e.g., `now + 2 hours`) - units can be: minutes, hours, days, weeks
      1. Special Keywords

      2. noon (noon, 12 o'clock)
      3. midnight (midnight)
      4. teatime (4 p.m.)
      5. tomorrow (same time tomorrow)
      6. - `noon` (12 o'clock at noon) - `midnight` (midnight) - `teatime` (4 p.m.) - `tomorrow` (same time tomorrow)

        Usage examples

        Example 1: Basic Usage

        Example

        $ at

        15

        :

        30

        warning: commands will be executed using

        /

        bin

        /

        sh

        at

        echo

        "Hello at command"

        ~

        /

        at_test.txt

        at

         

        Press Ctrl+D to end the input

        job

        1

        at Tue Jul

        18

        15

        :

        30

        :00

        2023

        Example 2: Reading Commands from a File

        Create a script file myscript.sh:

        Example

        #!/bin/bash

        echo

        "This is a test"

        /

        tmp

        /

        at_log

        date

        /

        tmp

        /

        at_log

        Then use:

        at -f myscript.sh now + 5 minutes
        
        

        Example 3: Viewing and Deleting Tasks

        Example

        $ atq  

        or at -l

        1

              Tue Jul

        18

        15

        :

        30

        :00

        2023

        a username

        $ atrm

        1

         

        or at -d 1


        Advanced usage

        Set environment variables

        Example

        $ at

        16

        :00

        at

        export

        MY_VAR

        =

        "test"

        at

        echo

        $MY_VAR

        ~

        /

        env_test.txt

        at

        Execute complex commands

        Example

        $ at

        17

        :00 today

        at

        cd

        /

        var

        /

        log

        &&

        grep

        "error"

        syslog

        ~

        /

        error_log.txt

        at

        Use a here document

        Example

        at

        18

        :00

        << 'EOF'
        #!/bin/bash
        echo "Starting backup..."
        tar -czf ~/backup-$(date +%F).tar.gz ~/Documents
        EOF


        Notes

        1. Permission Control

        2. /etc/at.allow and /etc/at.deny Files Control User Access Permissions
        3. If both the at.allow exists, only the listed users can use it at
        4. If both the at.allow does not exist, check at.deny, in which the listed users are denied
        5. - The `/etc/at.allow` and `/etc/at.deny` files control user access - If `at.allow` exists, only the listed users can use `at` - If `at.allow` does not exist, check `at.deny`, where listed users are denied
          1. Output handling

          2. By default, the command's output is sent to the user by email
          3. Use -m option forces sending an email even if there is no output
          4. - By default, the command's output is sent to the user by email - Use the `-m` option to force sending an email even if there is no output
            1. Environment Differences

            2. at The environment at execution time may differ from the interactive shell
            3. Important environment variables should be set explicitly in the command
            4. - The environment when `at` executes may differ from the interactive shell - Important environment variables should be set explicitly in the command
              1. Error handling

              2. Checks atd Check whether the service is running:systemctl status atd
              3. Check the system logs for error messages:journalctl -u atd
              4. - Check whether the `atd` service is running: `systemctl status atd` - Check the system logs for error messages: `journalctl -u atd`

                Practical Exercise

                1. Create a task that appends the current date and time after 5 minutes to ~/time_log.txt file
                2. Write a script to back up the /etc directory, and use at to schedule it for 2:00 AM tomorrow
                3. List all currently pending at jobs, then delete one of them

                Summary

                at The command is a powerful tool for managing one-time scheduled tasks in Linux. Through this article, you should have mastered:

                For tasks that need to run periodically, you can consider using cron, but for one-time tasks,at is undoubtedly the best choice.


                Linux Command Reference Linux Command Reference