I'm trying to do a little script that, as a function, will take a file and date as input and delete that file on the specified date. So I only need it to schedule to run once, and that's it.

I'm on OSX and have been looking at at, but not only have I been having problems scripting it, it's my understanding (I've been researching all day) that services like at and cron will be deprecated on OSX in favor of launchd.

From what I've seen, not only is launchd overly complicated for this, I can't seem to figure out how to use it to do a one time action at a specific time and date (which is really all I need).

I'd like to get some help on this, please. I'd rather not use at, and ideally it's a solution that will not require the user to install extra stuff. It also needs to be scriptable (so, command line). If I have to use launchd, so be it, but it really needs to be able to use a specific time and date.

  • How far in the future are you planning to run this command that you are afraid cron or at will be removed? :) Why would you not use at? By default it exist in all osx installs, so there's nothing extra for the user to install. – Mxx May 31 '12 at 19:56
  • It's not about how far in the future the command will run, it's about for how long users can continue to use the script. I've also read that at has some more implications, like using the disk more often, as it uses cron that checks for tasks every minute, and that it can (for example) stop the system from entering sleep mode. As I said, I've been researching all day, and would ideally not have to use at. – user137369 May 31 '12 at 20:05
  • If this is a one time command, why are you worried about how long they'll be able to continue use it?(not arguing, just trying to fully understand your situation) – Mxx May 31 '12 at 22:27
  • The idea is that I'll be able to use it multiple times. Explaining better, today I'll give it a file that it will delete in 4months, tomorrow I'll give it a different file that it will delete in 7days (I pick the file and date each time). Each of this file deletions is a one time thing, but I expect to be able to use this script for some time to come, and possibly even give it to some friends. – user137369 Jun 1 '12 at 1:58

Sure cron and at are deprecated, but they're probably not going anywhere (even if they did, it shouldn't be hard to reinstall them).

If you really wanted to do that with launchd, you'd have to save and load a property list in the first script and then unload and remove it in the second script.



plist="<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN
<plist version=\"1.0\">

echo "$plist" > $agent
launchctl unload $agent 2> /dev/null
launchctl load $agent

The second script could look like:

launchctl unload $plist
rm $plist
rm ~/Desktop/test

If you used at, you'd first have to enable it with sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.atrun.plist. Here's some examples copied from my website:

echo 'say test' | at now+5 # +5 and +5 seconds don't work
echo 'say test' | at +1 minute # +1 minutes results in an error
echo 'say test' | at +2 hours
echo say test > test.txt; at -f test.txt now+5
atq # at -l; list at queue
atrm 14 # at -d 14
atrm {1..9999} # remove all jobs
| improve this answer | |
  • Thank you very much, I really needed this example, and I think that using it as a base I'll be able to work out the rest of how to do it. I hope to be able to do some tests on the coming days. Your approach also got me a lot of ideas for how to better previous scripts I've written in a more “clean” way. – user137369 Jun 1 '12 at 23:40
  • Regarding the at aspect, you're all right on it's simplicity, but as I said I have some concerns, like it touching the disk a lot of times and possibly preventing the laptop from going to sleep on its own. I'd also like to eventually share the script with more people, and since at is disabled by default on OSX, having to ask the user for the password (since launchd has to activate it once as root) or to install extra stuff (in case at is removed) are steps I believe will be more difficult for some users to understand the necessity for or trust the script. – user137369 Jun 1 '12 at 23:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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