0

I run two scripts. Both are essentially the same code, but they need to run 5 minutes apart to prevent collision. First script: #!/bin/sh

# First run
line="Bonus check"
        stamp=$(date +%Y%m%d-%H%M)
      # sleep 90s
        echo "$stamp\t $line" >> /home/rdanner3/Desktop/CardParty.log
        {
export DISPLAY=:0.0
google-chrome [URL masked]
        }
sleep 45s
# google-chrome
pkill -f chrome

# Second run to seventh run identical to above; stripped for brevity.

Second script (notice it's essentially the same code; both really need to be loops for sanity!)

#!/bin/sh

# 1st run
line="Bonus checked."
stamp=$(date +%Y%m%d-%H%M)
# sleep 90s
echo "$stamp\t $line" >> /home/rdanner3/Desktop/Slots.log
{
export DISPLAY=:0.0
google-chrome [URL masked]
}
sleep 45s
# google-chrome
pkill -f chrome

# 2nd run to 7th run identical to above code...stripped for brevity

What happens when the scripts reach the second run? They run within seconds of one another, despite the cron jobs that initially launch them being 5 minutes apart. It's pretty obvious (from the log files the two scripts write) that they're launching pretty much seconds apart, not in the proper order. Would switching the second script to (say) ash or bash do any good to avoid the two scripts' sleep commands from confusing each other? Or am I making some other mistake (for example, needing to uncomment the second run of google-chrome to allow the pkill to clear the air)

Noticed the problem yesterday, decided I needed a little help. So I came here for it.

1 Answer 1

2

The mistake is probably relying on sleep for absolute timing instead of relative timing. The stuff prior to the sleep may be taking a varying amount of time.

After removing the repeats of code, I'd maybe use at in a loop or, more likely, use more sophisticated use of cron.

If exact start times were unimportant, I'd call two functions sequentially in a simple loop. Combining both scripts.

3
  • Part of the problem is that cron simply cannot do an interval of 183 minutes. The interval is relatively important to the functioning of the one line that does the heavy lifting, and for some odd reason, Windows has zero problem doing this sort of interval. Have tried an epoch-based timing scheme, but it simply didn't work.Oddly, with the second invocation of the browser not commented out, the two scripts work essentially flawlessly. Still scratching my head over that one, since the second run of the browser is an ugly kludge. Feb 16, 2018 at 6:29
  • On a cleaning-up note (in reference to the script code) what type of loop would you recommend? I'll admit I'm pretty new at scripting for Linux, although I've done loops in scripts for both DOS (all the way back to Apple's ProDOS, believe it or not...followed by both PC-DOS and MS-DOS) and Windows. Feb 16, 2018 at 6:35
  • @Raymond. I'm not sure I can recommend specific code, but if you have a quick read of a resource such as bash loop intro you should be able to find a construct that matches your needs. Maybe something like for url in "http://a.com" "http://b.com"; do google-chrome $url; sleep 45s; done Feb 16, 2018 at 13:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .