Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Running scripts only once per day Apps

I'm a cron (Cronnix) user, but because of my erratic work schedule, I occasionally need to run a cron job manually before its scheduled time. The problem is that it runs again via cron when its time rolls around. The following script uses AppleScript properties to avoid delivering the same payload twice in any one (calendar) day.

property last_run : ""

set today to date string of (current date)
if last_run is not today then
  set last_run to today
  display dialog "hi" -- your payload here
end if

The value of a script property is automatically stored whenever the script runs, so this script essentially remembers when it was run last, and behaves accordingly. The initial value of last_run is an empty string so the payload does get delivered the very first time the script runs. Using such scripts in cron is easier for me than than touching / checking / deleting a tmp file....

If you want to get fancier, it's not hard to give a warning instead of silently aborting.


property last_run : ""

set today to date string of (current date)
if last_run is today then
  display dialog "You already did this today." buttons {"Cancel", "Do it again"}
end if
-- if you're still here, do your thing
set last_run to today
display dialog "hi" -- your payload here

[robg adds: I haven't tested this one myself...]

    •    
  • Currently 2.20 / 5
  You rated: 3 / 5 (5 votes cast)
 
[6,752 views]  

Running scripts only once per day | 4 comments | Create New Account
Click here to return to the 'Running scripts only once per day' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Have you looked at anacron?
Authored by: porkchop_d_clown on Feb 25, '03 11:31:23AM

Anacron uses time stamps to check how long it's been since something has been run; thus, if you force anacron to run a job early, it will wait the correct interval before running it again.

It's available thru fink.

---
--
knock knock

[ Reply to This | # ]

Running scripts at all
Authored by: Swany on Feb 25, '03 04:02:34PM

I can't get cron to run an Applescript at night: "User interaction not allowed." or "Applescript timed out."



[ Reply to This | # ]
Running scripts at all
Authored by: ClarkGoble on Feb 25, '03 07:28:11PM

You have to be logged in for the Applescript to work. This is always a problem calling Applescripts from Apache or cron. So if you have a shared machine where people are logging in or out the above hint won't work.

There are work arounds. Ideally just store the current date in a file somewhere and then check it in your cron script. In that way you just have a sh script rather than an Applescript.



[ Reply to This | # ]
Running scripts only once per day
Authored by: DanFrakes on Feb 27, '03 08:13:57PM

If your crontab entry runs as root -- which is preferred for administrative tasks -- it can call an AppleScript from anywhere on any volume.



[ Reply to This | # ]