PDA

View Full Version : Delayed script action




haravikk
Jul 8, 2012, 11:11 AM
Okay, so I have a simple shell script which I use as an application launcher that mounts some disk images that I use when running the application.

Once the application is finished the disk images are left mounted, so that if I want to relaunch the application the script can skip the mounting stage.


What I'd like to do however is built in a delay so that the disks will be unmounted say… an hour after the application is closed.

I'm wondering what would be the best way to do this? Specifically, as the launcher script closes (because the app has been closed) I'd like to setup a delayed background action to trigger a second 'cleanup' script after a certain amount of time. I'd also like to be able to cancel that event from my launcher script (to prevent it from occurring while I'm running the application).

I was considering launchd, but it seems a bit of an awkward way to accomplish this since there's no good way to start a launch agent to execute after an hour or anything like that, which would possibly mean fiddling with the calendar interval via script or such.

Anyway, I basically just want the disks to unmount automatically X minutes after I finish using them, so I'd love to hear suggestions on how best to do this!



CyBeRino
Jul 8, 2012, 05:09 PM
use at(1)?


Hmm, looking into it more, it appears Lion at least doesn't actually run those jobs by default. You need to enable the atrun launchdaemon for it (launchctl load -w /System/Library/LaunchDaemons/com.apple.atrun.plist).

haravikk
Jul 9, 2012, 03:26 AM
Hmm, looks like a possibility, though if it's disabled by default in Lion then I wonder if there's a preferred alternative?

It still looks like it requires a specific time though; while probably a bit easier than a temporary launch agent, it would mean calculating the execution time myself, what's the best way to just grab the current time and add say… an hour?

Also, it looks like you can only cancel a job using its job number; but I'm not sure how I'd go about doing that without having to store it somewhere for the launcher script to lookup later? Is there an easier way to handle this, for example with a unique identifier?