At and cron command in linux

At Command in Linux for One-Time Jobs Scheduling

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.

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:

Syntax and Options

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

  • -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”
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:

To remove a scheduled job, use the “atrm” command followed by the 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

at 10:00 AM 6/22/2015 at 10:00 AM 6.22.2015 
at now + 1 week at now + 2 weeks 
at now + 1 year at now + 2 years 

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.

Читайте также:  Linux настроить ассоциации файлов

How to Ignore SSL Certificate Check with Wget

How to Ignore SSL Certificate Check with Curl

The `ls` Command in Linux with Practical Examples

29 Comments

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

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.

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

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.

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.

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.

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.

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.

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.

Читайте также:  How to turn off linux

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.

Источник

Using at for single-use cron jobs in Linux

Scheduling a single job is easy with the at command and can be done without going cron crazy. Find out how.

@ (at sign) on keyboard

Cron is one of the most recognizable UNIXisms of the computer world. Even IT people who don’t administer Linux servers have heard of that mysterious beast, the «cron job.» And Linux admins know that cron jobs are endlessly useful. In fact, it’s arguable that the cron system was an early progenitor of the automation craze. If there was something you knew a human could forget to do, cron was the answer.

Great Linux resources

However, where cron excels is repetition, and sometimes you don’t need a job to run every hour or every day or every week. You just need a job to be executed on schedule, and setting an alarm to remind yourself just doesn’t seem like the UNIX way. What you need is the at command, a small utility you can use to create a queue of jobs scheduled to run at a specific time in the future.

Setup

Before using the at command, you must have a shell script you want to launch at some time. As usual, your script should have a «magic cookie» or «shebang» line at the top of the file to set which shell to use for its execution.
For this example, create a simple script that creates a file in /tmp :

#!/bin/sh DATE=`date --utc +%s` echo "hello world $DATE" | tee /tmp/at.log exit 0

Make the script executable and then give it a test run:

View the contents of the /tmp/at.log file the script created:

$ cat /tmp/at.log hello world 1588482185

You can schedule a job using a timestamp or with natural language. If you use natural language or simple time formats, then no option is required before specifying the time.
For instance, to schedule test.sh to run immediately, you can just use the keyword now :

$ at now -f test.sh warning: commands will be executed using /bin/sh job 1 at Mon Feb 24 01:23:00 2020 $ cat /tmp/at.log hello world 1588482620

You can set an offset from now using minutes , hours , days , weeks , or years .

$ at now + 1 day -f test.sh warning: commands will be executed using /bin/sh job 2 at Tue Feb 25 01:27:00 2020

The years keyword is undocumented, and seconds is not supported. You can specify common times, too, such as midnight , noon , and teatime (that’s 16:00).

$ at 21:12 -f test.sh warning: commands will be executed using /bin/sh job 3 at Mon Feb 24 21:12:00 2020

To schedule a calendar date at a specific time, you must use the -t option and any POSIX-compliant time format. For example, using the YYYYMMDDHHMM format:

$ at -t 202003141509 -f test.sh warning: commands will be executed using /bin/sh job 4 at Sat Mar 14 15:09:00 2020

[ Looking for more advanced system automation? Get started with The Automated Enterprise, a free book from Red Hat. ]

You can send commands to at through a UNIX pipe:

$ echo "hello world" > /tmp/at.log | at now warning: commands will be executed using /bin/sh job 5 at Mon Feb 24 01:28:00 2020 $ cat /tmp/at.log hello world

The batch command (or at -b ) executes a command as early as system resources allow. If your system load is high, you can use batch to enqueue your job to be run when there are CPU cycles to spare.

$ echo "Cycles to spare" > /tmp/at.log | batch warning: commands will be executed using /bin/sh job 5 at Mon Feb 24 01:31:00 2020

The atq command displays your at queue. This gives you the job ID, the time each job is scheduled to run, the queue each job is grouped into ( a for the at queue or b for the batch queue), and the username of the queue owner. The queue owner is usually yourself, unless you’re running atq as root, in which case you see all user’s at queues.

$ atq 2 Tue Feb 25 01:27:00 2020 a seth 3 Mon Feb 24 21:12:00 2020 a seth 4 Sat Mar 14 15:09:00 2020 a seth 5 Mon Feb 24 01:31:00 2020 b seth

You can create and name your own queues using any single character c-z or A-Z. Both a and b are reserved designations for at and batch , and any queue with an uppercase letter is treated as a batch job. Queues with names that alphabetically follow a and b are run with increasing niceness.

Читайте также:  Softmaker office 2021 linux

When you send a command or script to at , your current working directory, environment (excluding BASH_VERSINFO , DISPLAY , EUID , GROUPS , SHELLOPTS , TERM , UID , and _ ), and the umask are retained. If your command expects specific environment settings, then you should set those while passing the command to at or override them in your script.

To see how your command is going to run, use the -c option along with the job number:

$ at -c 4 #!/bin/sh # atrun uid=1006 gid=1006 # mail seth 0 umask 22 CPLUS_INCLUDE_PATH=/usr/lib64/qt/include; export CPLUS_INCLUDE_PATH MANPATH=/usr/local/man:/usr/man:/usr/lib64/adoptopenjdk12.0.2/man; export MANPATH KDE_MULTIHEAD=false; export KDE_MULTIHEAD [. ]

You can remove pending jobs from your at queue using the atrm command and the job ID. If you don’t know the job ID, use atq to view your queue first.

$ atq 6 Fri Jan 01 00:00:00 2038 a seth $ atrm 6 $ atq

The at command is a little like a lesser cron system. It’s useful for one-time command execution, and it’s easy to use. If you’ve been using cron or sleep to offset the execution time of a command, take a look at at and batch .

[ Need to learn more about Linux system administration? Consider taking a Red Hat system administration course. ]

Источник

Оцените статью
Adblock
detector