Schedule one-off jobs with 'at'
AnthonyEnglish 270000RKFN Comments (2) Visits (27201)
After reworking the script a few times, looking up the crontab command which tells you that the week starts with 0 for Sunday, and minutes come before hours, and not putting in an extra asterisk which will run all files in the home directory for the user (which may be root), you end up with a very cluttered crontab file which hopefully doesn't have a surprise test job running every morning for evermore.
There is a much simpler way of running a one-off job: the at command. It's surprisingly easy to use and yet even administrators with long years of experience often say they have never used it. Here are some simple examples adapted from the man page for the at command.
Run command at now
In its simplest form, you could run your test script right now, using:
The time we're setting the command to run is "now". You may be more comfortable with setting the time before you specify the script to run:
Give me 5
The at command gives a lot of flexibility as to how you specify the time to start. You could start a job in exactly five minutes from now:
or maybe at a fixed time on a certain day. Here are five ways of setting a script to run at a given date and time:
Be careful with that last one. If you ran that at command some time on Friday, but after 3pm had passed, it wouldn't run until Friday of next week. It will run the next time Friday 3pm comes around.
crontab or at
crontab is for regular job schedules. The at command is for one-off jobs.
You could set a job to run again 5 minutes from the end of the current run by adding this into the script:
The cron doesn't give you that flexibility, as it only allows you to separate the start time of jobs, which means a second job may start off while the first one is still running.
Listing, cancelling and restricting at jobs
You can list jobs that are scheduled to run using at -l and you can remove them using at -r. The at man page explains how to control who can run the at command using /var