Main       Comic       Forums       Anime       Tools       Fun       
Categories
Recent Articles
Recent Posts
Buy DSG Items
The DeadGod Anime Manager
helps you stay up-to-date
on your favorite anime series.
Recent Posts       Popular Articles       Popular Threads       [ View All Recent Posts ]
1. New Server:  Rules Discussion by DranoK -- 4H 56M ago.
2. Unknown by noggin -- 5H 8M ago.
3. New Server:  Rules Discussion by <LCC> -- 9H 59M ago.
4. DeadGod Survival Game - Cycle 11 by DSG -- 13H 23M ago.
 
[ Login | Register ]
 
Viewing 'Scheduling jobs with at instead of cron'
Aug
20
Monday, August 20th, 2007 (22728 Views)
Intermediate
DranoK
Most Linux admins are well-versed with crontab. All too often, however, I see people using crontab to schedule a job that's only intended to run once. This not only clutters your cron file but can potentially be dangerous if you forget to un-schedule your job before it runs again.

Luckily there's an alternative: at

Prerequisites:
o Familiarity with the bash shell


The at command simply runs a job once at some specified point in the future. Unlike crontabs, which will only use your user environment if you explicitly tell it to, at will use your environment by default.

The basic invocation patterns of at are:

Listing 1: Basic invocation:
1) at time
2) at time < /file/with/commands
3) echo "some command" |at time

In the first example after pressing enter you will be dropped into the at> shell. Here type the commands, one per line, you would like to be executed.

The second example is non-interactive. Instead of typing your commands into the at> shell, put them into the /file/with/commands file instead. Note that you do not put a shabang (#!/bin/sh) at the top of your script! If you have a script you want to run, do not specify "at time < /path/to/script"--instead create a new text file with one line, "/path/to/script" in it, and use that file as your source to at (at time < /path/to/newfile).

The third example is useful if you want to run a single command at a specified time. For example, "echo /path/to/script |at time"

There are a number of ways you can specify the time an at job runs at. Below are the most common specifications (all these examples use the first formatting of listing 1 and will drop you into the at> shell):

Listing 2: Time specifications:
at 2pm (execute the job at 2pm today, or tomorrow if 2pm has already passed)
at +5 hours (execute the job exactly 5 hours from now)
at monday (execute the job on the coming Monday, next Monday if it is currently Monday)
at next month (execute the job exactly 1 month from now)
at 7/7/17 (execute the job on July 7th, 2017)
at 2:21pm september 21 (execute the job at exactly 2:21pm on September 21st)
at +1000 days (execute the job exactly 1000 days from now)

To see a listing of scheduled at jobs, use the atq command.

Listing 3: The atq command
dranok@neptune:~> atq
17      2017-07-07 12:38 a dranok

To view the contents of an at job, use the at -c jobid command. Your full environment will be included in this output, however I have snipped this from the listing below to remove the clutter.

Listing 4: View the contents of an at job
dranok@neptune:~> at -c 17
#!/bin/sh
# atrun uid=1000 gid=100
# mail    dranok 0
umask 22

ls -l

To remove an at job, use the atrm jobid command.

Listing 5: Delete an at job
dranok@neptune:~> atrm 17

dranok@neptune:~>atq

dranok@neptune:~>

At commands will persist through reboots. They are stored in a spool directory on the filesystem, generally /var/spool/atjobs. You can cat these jobs directly instead of using at -c jobid. You can also remove them with rm instead of atrm jobid.

Listing 5: Delete an at job
dranok@neptune:~> sudo ls -l /var/spool/atjobs
total 8
-rwx------ 1 dranok users 4966 Jul 24 12:43 a00012012d7160

dranok@neptune:~> sudo cat /var/spool/atjobs/a00012012d7160
#!/bin/sh
# atrun uid=1000 gid=100
# mail    dranok 0
umask 22
Environment snipped to save space
ls -l

dranok@neptune:~> atq
18      2007-07-24 17:00 a dranok

dranok@neptune:~> sudo rm /var/spool/atjobs/a00012012d7160

dranok@neptune:~> atq

Thanks for reading, and welcome to the wonderful world of at ;)

See Also: Relevant section of the at man page regarding time specifications
       At  allows fairly complex time specifications, extending the POSIX.2 stan‐
       dard.  It accepts times of the form HH:MM to run a job at a specific  time
       of day.  (If that time is already past, the next day is assumed.)  You may
       also specify midnight, noon, or teatime (4pm) and you can have a  time-of-
       day suffixed with AM or PM for running in the morning or the evening.  You
       can also say what day the job will be run, by giving a date  in  the  form
       month-name  day with an optional year, or giving a date of the form MMDDYY
       or MM/DD/YY or DD.MM.YY.  The specification of  a  date  must  follow  the
       specification  of  the  time  of  day.  You can also give times like now +
       count time-units, where the time-units can be  minutes,  hours,  days,  or
       weeks  and you can tell at to run the job today by suffixing the time with
       today and to run the job tomorrow by suffixing the time with tomorrow.

       For example, to run a job at 4pm three days from now, you would do at  4pm
       +  3 days, to run a job at 10:00am on July 31, you would do at 10am Jul 31
       and to run a job at 1am tomorrow, you would do at 1am tomorrow.


Permalink - Forum Thread - 9 Comments - Subscribe!Social Bookmark Button



9 Comments -- Post a Comment


262 Days 13 Hours ago, noggin said...

Now that's what I call, BREAKING THE ICE!

262 Days 2 Hours ago, LiQUiD_X said...

You're insane noggin =P

261 Days 15 Hours ago, noggin said...

Hahahhahahha

261 Days 13 Hours ago, noggin said...

I'm so fucking awesome.

261 Days 3 Hours ago, DranoK said...

Give me all your WoW gold. And items.

260 Days 8 Hours ago, noggin said...

Already sold them

260 Days 8 Hours ago, LiQUiD_X said...

HE LIES! He's holding out on us. How did this end up in a linux article by the way?

259 Days 7 Hours ago, noggin said...

Someone broke the ice and thus conversation took place.


FUCKING IDIOT.


No I really did quit and sold my shit for IRL DOLLARS!

259 Days 5 Hours ago, LiQUiD_X said...

You're mean =(


Post a Comment
Name (Required)
Register an account to gain access to all the great features of DeadGod.Net! It's fast, and free. Register now!

You may use BBCode in your post.


Older Articles
 
 
DeadGod.Net - Cute Evil Atheists
Encouraging critical thought and general enjoyment since 2001.
Want to join our community? Have suggestions or comments?
Contact Us or Visit our forums.