Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Linux Commands»At Command in Linux for One-Time Jobs Scheduling

    At Command in Linux for One-Time Jobs Scheduling

    By RahulMarch 23, 20233 Mins Read

    Scheduling tasks in a Linux environment is a common requirement for system administrators and developers. While the cron command is often used for recurring tasks, the “at” command is a powerful tool for scheduling one-time jobs in Linux. This article will provide an in-depth look at the “at” command, its syntax, usage examples, and best practices for managing one-time jobs.

    Advertisement

    Understanding the ‘at’ Command

    The “at” command allows users to schedule a command or script to be executed at a specified time in the future. It is particularly useful for running one-time jobs, such as maintenance tasks, backups, or system updates, without requiring manual intervention. The “at” command reads the commands to be executed from standard input or from a file and schedules them accordingly.

    Installing the ‘at’ Command

    Most Linux distributions come with the “at” command pre-installed. However, if it is not present on your system, you can install it using the package manager for your distribution.

    • For Debian-based distributions, use the following command:
      sudo apt-get install at 
      
    • For Red Hat-based distributions, use this command:
      sudo yum install at 
      

    Syntax and Options

    The basic syntax of the “at” command is as follows:

    1
    at [OPTIONS] TIME

    • -f: Specifies a file containing the commands to be executed.
    • -t: Specifies the time at which to run the commands using a Unix timestamp.
    • -m: Sends an email to the user when the job has completed.
    • -q: Specifies a queue in which to place the job.

    Scheduling a One-Time Job

    To schedule a one-time job, simply provide the desired time for execution. The “at” command supports various time formats, such as:

    • Relative time: “now + 1 hour” or “now + 30 minutes”
    • Absolute time: “2:30 PM” or “15:30”
    • Date and time: “10:00 AM tomorrow” or “2023-04-01 18:00”

    Example:

    echo "echo 'Hello, World!' > /tmp/hello_world.txt" | at now + 1 hour 
    

    This example schedules a one-time job to create a file containing “Hello, World!” in the /tmp directory after one hour.

    You can also schedule the command as below:

    at now + 1 hour 
    echo 'Hello, World!' > /tmp/hello_world.txt 
    

    Press CTRL + d to exit from at command terminal.

    Listing and Managing Scheduled Jobs

    To list all scheduled jobs for the current user, use the “atq” command:

    atq 
    

    To remove a scheduled job, use the “atrm” command followed by the job ID:

    atrm <job_id> 
    

    Best Practices

    • Always verify that the “at” command is installed and enabled on your system.
    • Use descriptive comments in your “at” jobs to make it easier to understand their purpose.
    • Test your commands or scripts before scheduling them with the “at” command.
    • Remember that the “at” command is designed for one-time jobs. Use the cron command for recurring tasks.

    At Command Examples

    1. Schedule task at coming 10:00 AM.
      at 10:00 AM 
      
    2. Schedule task at 10:00 AM on coming Sunday.
      at 10:00 AM Sun 
      
    3. Schedule task at 10:00 AM on coming 25’th July.
      at 10:00 AM July 25 
      
    4. Schedule task at 10:00 AM on coming 22’nd June 2015.
      at 10:00 AM 6/22/2015 
      at 10:00 AM 6.22.2015 
      
    5. Schedule task at 10:00 AM on the same date as next month.
      at 10:00 AM next month 
      
    6. Schedule task at 10:00 AM tomorrow.
      at 10:00 AM tomorrow 
      
    7. Schedule task at 10:00 AM tomorrow.
      at 10:00 AM tomorrow 
      
    8. Schedule task to execute just after 1 hour.
      at now + 1 hour 
      
    9. Schedule task to execute just after 30 minutes.
      at now + 30 minutes 
      
    10. Schedule tasks to execute just after 1 and 2 weeks.
      at now + 1 week 
      at now + 2 weeks 
      
    11. Schedule tasks to execute just after 1 and 2 years.
      at now + 1 year 
      <at now + 2 years 
      
    12. Schedule tasks to execute at midnight.
      at midnight 
      

      The above job will execute at 12:00 AM

    Conclusion

    The “at” command is an essential tool for Linux users who need to schedule one-time jobs. By understanding its syntax and usage, you can effectively automate tasks and improve the efficiency of your workflow. Remember to use best practices when scheduling jobs to ensure that your system runs smoothly and your tasks are completed on time.

    at scheduling task
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    cp Command in Linux (Copy Files Like a Pro)

    dd Command in Linux (Syntax, Options and Use Cases)

    Top 10 JQ Commands Every Linux Developer Should Know

    View 29 Comments

    29 Comments

    1. Khay on December 14, 2020 11:20 pm

      EXACTLY WHAT I NEEDED!!!!!
      BTW the recaptcha input is underneath the Submit button in the comments form. Hard to click.

      Reply
      • Rahul on December 15, 2020 1:46 am

        Thanks Khay, I hope recaptcha positioned correctly now.

        Reply
    2. Hrishi on December 4, 2019 12:23 pm

      Hi,
      does it generate log after execution like cron ?

      Reply
    3. leandro on November 22, 2019 9:59 pm

      How can we schedule a gtk “graphical” job, for example a simple yad message?
      yad –title “Warning” –text “Alarm now, attention” –on-top –borders=25

      In my tests, no display is shown at a specified time.

      Reply
    4. Alexey on October 2, 2019 9:59 pm

      How is it better to schedule installation of upgrades?
      For some reason tasks don’t execute. I tried something like: sudo apt-get upgrade -y | at 21:00

      Reply
      • Magnus Smedberg on December 10, 2020 7:41 pm

        It should be
        echo “apt-get upgrade -y” | sudo at 21:00

        Reply
    5. tejender singh on September 17, 2019 12:10 pm

      how can we Schedule one job at two different timing with single command

      Reply
      • Brandon on September 26, 2019 12:53 pm

        | at | at

        ex.

        touch helloworld.txt | at now + 1 minute | at now + 2 hours

        Reply
    6. Lekshmi on July 30, 2019 4:48 pm

      HI

      Where do I see the output ? How will I know if the job has been executed or not ?

      Reply
    7. Tharun on July 5, 2019 1:47 pm

      please tell me that i want to run a corntab command in every last day of the month.
      i am thinking but in some its 30 days and 31 in some and 28 and 29 like that.

      Reply
      • Rahul on July 9, 2019 9:17 am

        Hi Tharun,

        You can try cron like this:

        59 23 28-31 * * [ "$(date +%d -d tomorrow)" = "01" ] && /root/script.sh
        

        here [ “$(date +%d -d tomorrow)” = “01” ] will retrun true if tomorrow is the first day of next month.

        Reply
    8. bartek on April 19, 2019 8:49 pm

      I’m using at to run mplayer and stream some radio station, when i want to stop mplayer and use atrm command it doesn’t work. i got only ” Warning: deleting running job” . job vanishes from the atq list, but it still streams music.

      Reply
    9. NISHANT SACHDEVA on January 17, 2019 9:57 am

      HI, I am experiencing an issue. When i am using the format:
      command | at time

      It is not executing the command that I am providing.
      Please tell how to get past it.

      Reply
      • Magnus Smedberg on December 10, 2020 7:42 pm

        echo “command” | at time

        Reply
    10. Sergey on August 7, 2017 3:03 am

      Great article Rahul!
      I just wanted to extend it a bit with some useful additions.
      1) You can use -f option to point “at” to the script you need to run:
      at -f /path/to/the/script time_spec
      2) One can use “at” to start a process in background without nohup, etc. As easy as
      at -f /a/command now
      or
      echo “/a/command” | at now
      3) You can use “at” to run a command repeatedly, but unlike cron you can use “at” to run commands with some period between runs, for example after 3 minutes after previous run was completed. This allow you to avoid various checks preventing next run to start before previous is finished.
      Moreover you can define this period as random value. Examples:
      The script (lets name it /home/user1/at_run.sh):
      ————————————-
      #!/bin/bash
      /the/command/you/need
      # fixed period between runs
      period=3
      # or random period. RANDOM is a bash’s random number from 0 to 32767
      period=$[ ($RANDOM % 20) + 15 ]
      at -f /home/user1/at_run.sh now + $period minutes
      ————————————-
      run /home/user1/at_run.sh and all next runs will be scheduled automatically, so your /the/command/you/need will run repeatedly forever. Sure, you can break the next run with atq/atrm.

      Reply
      • Francois Scheurer on June 1, 2018 8:51 am

        Thx Rahul and Sergey.

        @Sergey, in your last point you are basically using a wrapper with a random pause and re-scheduling of the at job.

        I think this is also achievable with cron, without the “various checks preventing next run to start before previous is finished”.
        You can just replace the last line of your script with this:

        sleep $(($period * 60)) && exec /home/user1/at_run.sh

        Then set a crontab for /home/user1/at_run.sh .
        Note: the ‘exec’ bash builtin will prevent calling bash recursively (nested bash’s) and spare memory.
        You will need to kill the process to end it.

        If the job is not supposed to stay permanently (boot safe), I would prefer to use an interactive bash inside a ‘screen’ command and just do a loop:

        while : ; do
        /the/command/you/need
        sleep 60
        done

        So you can follow in “live” the output of the script.

        Reply
    11. archie on May 7, 2017 2:15 am

      hello!
      i am running centos. when i submit an at nothing happens. i can call it up using “atq #”, however, it doesn’t execute?
      thanks for the help!!!

      Reply
    12. Nabil on April 26, 2017 1:33 am

      What are the differences between at-command and chrontab?

      Reply
      • Rahul K. on April 26, 2017 4:11 am

        at command is used for one-time task scheduling. crontab is used for repeated tasks.

        Reply
    13. shitanshu on December 1, 2015 10:04 am

      Hi,

      I need to run a script at particular time-stamp which internally calls one more script.
      But I am getting an error :

      at `./radConnect.sh` 09:00
      sh: netstat: command not found
      sh: /sbin/ifconfig: No such file or directory
      Radskman rc:[0] [Request successfully completed]
      syntax error. Last token seen: h
      Garbled time

      Reply
      • Rahul on December 1, 2015 1:13 pm

        Hi Shitanshu,

        Schedule job like following

        $ at 09:00 AM
        
        at> sh radConnect.sh
        at> ^d
        
        job 2 at Wed Dec  2 09:00:00 2015
        
        Reply
        • shitanshu on December 2, 2015 5:54 am

          Hi Rahul,
          Thanks for the quick response, the use case which I am trying is a bit different.
          I have created some containers using a script and a script internally calls another one which needs to run at a particular timestamp. So the command prompt for giving the script won’t be visible. So is there any alternative to it. Or can we do it using crontab?

          Reply
          • Rahul on December 2, 2015 6:49 am

            Hi Shitanshu,

            Try following command.

            echo "sh radConnect.sh" | at 09:00 AM
            
            Reply
            • Anno on June 21, 2017 10:31 am

              What would be the command for future date? midnight on 30 October 2017

              echo “sh radConnect.sh” | at ?

              Reply
              • Nabil on June 22, 2017 4:13 pm

                i think it should be
                echo “sh radConnect.sh” | at 0 0 30 10 * *

                this would repeat every year on 30th October.

              • Nabil on June 22, 2017 4:21 pm

                I think i’ve mixed up with crontab function command.

                Correction of the previous command:
                echo “sh radConnect.sh” | at 12:00 AM 30.10.17

              • Rahul K. on June 23, 2017 3:32 am

                Hi Nabil.. This will work

    14. maillot psg 2013 on June 3, 2013 4:34 pm

      Wow that was unusual. I just wrote an incredibly long comment but after I clicked submit my comment didn’t appear. Grrrr… well I’m not writing all that over again. Anyway, just wanted to say fantastic blog!

      Reply
    15. newcastle on April 18, 2013 1:46 am

      We are interested as well as enthusiastic about what you really are writing about here.

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Implementing a Linux Server Security Audit: Best Practices and Tools
    • cp Command in Linux (Copy Files Like a Pro)
    • 15 Practical Examples of dd Command in Linux
    • dd Command in Linux (Syntax, Options and Use Cases)
    • Iptables: Common Firewall Rules and Commands
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.