Periodically Run an R Script as a Background Process using launchd under OSX

launchdBeforelaunchdAfter

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.

R script

setwd('~/')
xBar <- mean(c(1,2,1,21,2,3,2))
write.csv(xBar,'rOutput.csv')

Save code as rCode.R to the ~/ directory.

Bash shell script

/usr/bin/Rscript ~/rCode.R

Save code as rShellScript.sh file to the ~/ directory.

Execute chmod +x rShellScript.sh in the terminal to make this file runnable.

*.plist file

<?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 ~/Library/LaunchAgents.

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.

Advertisement

13 thoughts on “Periodically Run an R Script as a Background Process using launchd under OSX

  1. 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..

  2. 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.

  3. 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!

  4. 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.

      • 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!

  5. Pingback: error code 4960 when saving a .plist - PhotoLens

Leave a Reply to Felix Dietrich Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s