Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Frequently within my scripts I schedule auxiliary tasks for later in the form:

at now + 8 hours <<< "rm -f workfile.$$ >> $worklog 2>&1 "

or

at 11pm + $(date +%M) minutes <<< "./postMarketJob.sh $orderID &> $orderID.log"

Trouble is, sometimes the laptops or PCs I use as servers are off when the scheduled time comes, so the at job never happens.

Same issue is true of crontab entries.

I'm trying to make sense of anacron's man page for Ubuntu.

Is there a tool to schedule a job such that it will run when requested, or at the earliest possibility whenever the server is back online?

It doesn't have to be with anacron, and if it's something only root can use, then I'll just wrap my invocations with sudo

CLARIFICATION

The tasks are not some fixed scripts (if at all) but very variable, and are conditional, meaning that depending on the unforeseen state of things within programs, they may or must not be scheduled in the first place.

It sounds like I might have to develop my own wrapper on top of atd/anacron that gives my programs a similar interface to at, but I was hoping something already exists. Been checking if fcron fits the bill and if available for Ubuntu in some repo.

share|improve this question

3 Answers 3

Anacron is great for what it is... a system to ensure jobs are executed every N number of days, after rebooting--not much control there. Furthermore, it's not a resident daemon and therefore can't replace crond or atd; it starts up at boot time (or when manually invoked), waits for the appropriate intervals before checking some conditions, and then once everything has been checked or run, it quits.

That said, it could be helpful in your situation, as a backup for atd. You could do something like:
echo '@monthly 0 custom1timejob script_or_cmd_here' >> /etc/anacrontab

Then schedule your at job, making sure to include something in the script that either lets anacron know the job was run (by doing date +%Y%m%d > /var/spool/anacron/custom1timejob), or even simpler: just remove the entry from anacrontab (sed -i /custom1timejob/d /etc/anacrontab).

Then, at worst, the job should be run the next time the system boots up.
(Make sure the RANDOM_DELAY and START_HOURS_RANGE variables in /etc/anacrontab [see man anacrontab], aren't gonna mess with your expectations though.)

share|improve this answer
    
That helped me understand how the stuff in /etc/anacrontab works, thanks. I considered hacking my own interface on top of anacron, till I noticed its built-in scheduling isn't more granular than a full day (contrasted with 1min in cron). –  Marcos Mar 6 '12 at 10:23
    
Yeah. Due to cron's ability to read the @reboot time nickname, I haven't really had an occasion yet where I chose anacron over cron. –  ryran Mar 6 '12 at 13:29

For jobs that are executed once, at already does exactly what you require. At any time, atd will execute all scheduled jobs that are past their scheduled dates. In particular, at boot time, starting atd triggers the execution of any job whose date became due during the downtime.

For periodic jobs, you could combine a fixed-schedule crontab entry and an @reboot crontab entry. In the @reboot entry, only perform the task if it hasn't been done in the last 24 hours (or whatever interval you prefer).

42 5 * * * /usr/local/bin/myjob && touch /var/myjob/done
@reboot if [ -z "$(find /var/myjob.done -mtime 1)" ]; then /usr/local/bin/myjob && touch /var/myjob/done; fi
share|improve this answer
    
Sounds like could be part of the solution; how would my programs go about scheduling (or avoid scheduling) arbitrary jobs+future parameters like they do now with at? To run only once at a future date/hour/minute, or next uptime following that appointment. –  Marcos Mar 6 '12 at 10:29
    
@Marcos That's already what happens with at: when atd starts, it executes all jobs whose due date is in the past. –  Gilles Mar 6 '12 at 11:02
    
If so, thanks--I'll have to re-check my observations on that though. For sure crontab doesn't act like that, so based on your comment, I may need to convert some of my mission-critical crontab lines to at jobs launched by anacron.daily to run at their specific crontab times –  Marcos Mar 6 '12 at 11:27
    
Just realized that too won't work if server comes back up eventually, anacron runs, and those at schedules are already in the past--would be interpreted as tomorrow –  Marcos Mar 6 '12 at 11:29
1  
@Marcos At jobs are recorded at a specific date. at 4pm schedules a job for 4pm today. If you turn off the computer between 3pm and 5pm, the job will be executed at 5pm today, since it was due at 4pm today and 5pm today is the first time there was an at daemon capable of launching the job. –  Gilles Mar 6 '12 at 12:24

This may be a situation where the batch option of at could help.

From my sandbox RHEL5 machine, I was able to schedule a batch job two minutes into the future, then stop atd. I let five minutes pass, and then started atd again. When atd started, the batch job kicked off immediately.

invocation: batch "NOW+2 minutes" or any acceptable at time input.

I did run into issues trying this on Fedora 16. Even though the man pages that say it supports time input, the command reports that it accepts no arguments.

share|improve this answer
1  
From the man page: executes commands when system load levels permit; in other words, when the load average drops below 1.5, or the value specified in the invocation of atd. I wouldn't want it limited by system load though –  Marcos Mar 5 '12 at 14:52
1  
The man pages do say that batch is not suitable when users are competing for resources. If you wanted to override the load-avg issue and had no intentions of using batch for other processes, you could modify the atd invocation and adjust -l upwards. In RHEL this is located in /etc/sysconfig/atd –  JodieC Mar 5 '12 at 15:13
    
batch "now + 1 minutes" <<< "uptime" gives batch accepts no parameters like you said. Guess that cements that possibility for Ubuntu, anyway. Ability to schedule is essential. –  Marcos Mar 5 '12 at 17:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.