Computers are great at doing repetitive things a lot, so why deprive them of doing what they do best by manually re-running the same code every night? Here we create a simple bash script to execute an R script and define a *.plist so that launchd, under OSX, can run it periodically. The *.plist code given here is configured to run a shell script every day at 8:00 PM.
setwd('~/') xBar <- mean(c(1,2,1,21,2,3,2)) write.csv(xBar,'rOutput.csv')
Save code as rCode.R to the
Bash shell script
Save code as rShellScript.sh file to the
chmod +x rShellScript.sh in the terminal to make this file runnable.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>com.rTask</string> <key>ProgramArguments</key> <array> <string>/Path/to/shell/script/rShellScript.sh</string> </array> <key>StartCalendarInterval</key> <dict> <key>Hour</key> <integer>20</integer> <key>Minute</key> <integer>00</integer> </dict> </dict> </plist>
Save code as com.rTask.plist file under path
Run the command
launchctl load ~/Library/LaunchAgents/com.rTask.plist.
In this way, we should now be able to periodically run an R script automatically. Fun, huh? For a more lengthy description of how to do this and how it all works see the Creating Launch Daemons and Agents section of the Daemons and Services Programming Guide under the Mac Developer Library at the developer.apple.com website.
13 thoughts on “Periodically Run an R Script as a Background Process using launchd under OSX”
what is meant by ~/ directory ? My R scripts are in a workspace which is stored somewhere /users/…/…/../..
The symbols “~/” are a shortcut used in place of the full path to your home directory. For instance, “~/” is, for me, equivalent to “/Users/willchernoff”.
I managed to set everything up as described here! Thanks a lot! But how can I see the status of the script? It must be running somewhere in the background but I’d like to see it .. Typing “ps” into Terminal does not give me anything..
launchctl listshows what plist are running. Plist with a status of 0 were launched successful (1 otherwise).
Thanks man, really helpful. I had some problems with letting it run properly, but I figured out I forgot the she-bang in my R-script, which gave 78 exits in launcherd. However, it’s working fine now, really awesome stuff.
hey! I have tried this exactly as you described – and it says
launchctl: no plist was returned for: /Users/fd/Library/LaunchAgents/com.rTask.plist
After creating and before loading the *.plist file change the file permission. The file permission should match the account under which the file is going to run. In your case, the command
chown fd /Users/fd/Library/LaunchAgents/com.rTask.plist` should do the trick. Let me know how it goes!
Thanks…this was very helpful! I’m new at using R and programming in general and used LaunchControl to run my script. I initially had some errors, but I tweaked it according to your instructions and somehow it worked.
Glad the tweaks worked!
I upgraded my OS X to El Capitan recently, and I can’t get my shell script to work. When I try to run it, I get the error message: line 2: /usr/bin/Rscript: No such file or directory
Any idea what could be causing this? I uninstalled and reinstalled R on my computer, but I still had the same problem. Thanks!
Thanks for highlighting this! I recently, as in a couple days ago, upgraded too and had the same issue. OS X no longer stores Rscript under the location /usr/bin. (For details, see bug 16469). After reinstalling R, Rscript is now located under /usr/local/bin. All instances of “/usr/bin/Rscript” in this post should be replaced with “/usr/local/bin/Rscript.”
Thanks for replying and giving me the link with more details about the bug! After I wrote the question, I did some more tinkering and added “/usr/local/bin/Rscript.” It’s working again like a charm!
Pingback: error code 4960 when saving a .plist - PhotoLens