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.

I need to run a command once, but only once, per day, until it succeeds.

Continuous uptime cannot be expected, and program success cannot be guaranteed.

This program requires network access, but not every time I start my computer with network access.

My program will exit with, e.g -1 unless it succeed ( which returns 0 ).

share|improve this question
    
So basically, what you want is for the program to try something once a day until it succeeds? –  Shadur Jun 14 '12 at 13:38
    
@Shadur yes , but can this be determinted by cron ? I know I could use a state file on hard drive –  warl0ck Jun 14 '12 at 14:04
    
Yeah. Check my answer for a possible solution. –  Shadur Jun 14 '12 at 14:17
add comment

2 Answers

up vote 2 down vote accepted

Use a shell to provide this. For example, create a script with something like the following:

#!/bin/sh
# Check to see if this is already running from some other day
mkdir /tmp/lock || exit 1
while ! command-to-execute-until-succeed; do
    # Wait 30 seconds between successive runs of the command
    sleep 30
done
rmdir /tmp/lock

After that, point cron to the script.

share|improve this answer
    
Wouldn't this also be vulnerable to TOCTTOU? Also, I admit that the OP isn't entirely clear whether he wants the program to try once per day until it succeeds, or to succeed once per day. If the latter, your answer's better. –  Shadur Jun 14 '12 at 13:58
    
@Shadur - No, mkdir is atomic. –  Chris Down Jun 14 '12 at 19:40
add comment

I think the easiest way to do this would be for your program to check for a file's existence before making the attempt, and to create that file once it executes successfully.

If you can't modify the program to do so, use the cronjob itself. Something along the lines of:

test -e /path/to/tempfile || ( /path/to/program && touch -a /path/to/tempfile )

Because of the way unix evaluates condition checks, once the left side of the || condition evaluates to true, the entire expression is true and the right side is never evaluated -- IE, the program isn't called.

Then inside the parens, && requires both sides to evaluate to true, so if /path/to/program exits with a nonzero return code, there's no point in evaluating the statement on the right -- the expression already evaluates to false -- and /path/to/tempfile is never created.

share|improve this answer
    
This is vulnerable to TOCTTOU. –  Chris Down Jun 14 '12 at 13:47
    
TOCTTOU being--? Sorry, not familiar with the acronym. –  Shadur Jun 14 '12 at 13:48
    
    
Ah, point taken -- but in this case, seeing as how we're not actually doing anything with the file except checking for its existence and creating it if it doesn't already exist there's an extreme limit to any possible shenanigans you could get up to with the exploit. I'll amended the answer to add -a to the touch command to limit it even further. –  Shadur Jun 14 '12 at 13:54
    
Whilst that may be true, it's easily fixed by using an atomic check/set. –  Chris Down Jun 14 '12 at 13:56
add comment

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.