macOS – How to Use at Command to Schedule a Script

atcommandosx

I've written a Python script that I need to execute at a certain time after another script runs. I found the at command, and after a bit of digging, I tried the following (broken into multiple lines for readability):

/private/var/folders/w9/6q0rjl6n4yv859fpxbg4123w0000gn/T/S10/fm-git.py
--filename chiv-lib --repository /Users/chuck/Projects/chivalry/chiv-lib/
--path Chivalry/ --comment "test" | at 2:52pm

This ran the script immediately. So I tried

at 2:53pm
> /private/var/folders/w9/6q0rjl6n4yv859fpxbg4123w0000gn/T/S10/fm-git.py
> --filename chiv-lib --repository /Users/chuck/Projects/chivalry/chiv-lib/
> --path Chivalry/ --comment "test"
> ^D

But the time came and went and the script didn't execute. After the above, at -l outputs the following:

13  Mon Nov 27 02:53:00 2017
12  Mon Nov 27 14:52:00 2017

This is on macOS High Sierra. Is at the right utility for this? If so, how can I get it to do what I want here?

Best Answer

Let's see, on Mac OS X (as this hardware is too old to run macOS) plus with myat because I can never remember the date format at wants...

$ date
domingo, 26 de noviembre de 2017, 16:34:12 PST
$ myat 16:36
touch $HOME/nananananananananananananananana-atran
job 2 at Sun Nov 26 16:36:00 2017
$ atq
1       Wed Mar  9 08:00:00 2016
2       Sun Nov 26 16:36:00 2017
$ 

... why is there a job from 2016 hanging around??

$ date
domingo, 26 de noviembre de 2017, 16:36:33 PST
$ atq
1       Wed Mar  9 08:00:00 2016
2       Sun Nov 26 16:36:00 2017
$ 

Uhhhh...maybe the man page for at will help?

IMPLEMENTATION NOTES
     Note that at is implemented through the launchd(8) daemon periodically
     invoking atrun(8), which is disabled by default.  See atrun(8) for infor-
     mation about enabling atrun.

Meanwhile over in atrun(8) we find...

 Execute the following command as root to enable atrun:
       launchctl load -w
       /System/Library/LaunchDaemons/com.apple.atrun.plist

Gosh. Let's try that...

$ sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.atrun.plist

And then we wait like a minute or two...

$ atq
$ ls *atran
nananananananananananananananana-atran
$ 

Looks good once you turn it on (warning may drain battery or precious cpu slices...)

Related Question