Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Automate a terminal command

Hello


There is a command I run in Terminal every time I restart my computer that watches and keeps 2 folders in Sync. The command is as follows...


nohup fswatch-run -r /"Folder to watch"/ /"location of rSync shell".sh >/dev/null 2>&1 &


The nohup and & ensure the programs continue to run after I close down Terminal

The small .sh file is a simple rSync shell that keeps the 2 folders in sync

rsync -aE --delete /"location of folder 1"/ /"location of folder 2"


I have lost hours trying to automate this process i.e. through Automator and AppleScript (via do shell script) but cannot seem to get it to work.


Has anyone got any ideas? Am I doing something wrong or missing something?


Cheers

Andrew

MacBook Pro (Retina, 15-inch, Late 2013), OS X Mavericks (10.9.5), Automator

Posted on Oct 10, 2015 5:35 AM

Reply
6 replies

Oct 12, 2015 1:37 PM in response to Skybridge Translation

Thanks for your reply.


I am happy to go on a script writing learning journey, but I suppose what I really wanted at this point in time was just a script that did the job.


Having said that, I did manage to write a small script, save it as an application, and stick it in my Startup Items (through System Preference). That works, but now I have to close several terminals after startup.


Is there a cleaner way to do it? Through a launchd or Daemon perhaps?


Cheers

Andrew

Dec 20, 2015 1:21 PM in response to andrewfromculcairn

Here's a sample launch daemon plist file = "com.your_username.watch.plist"

which is placed in /Library/LaunchDaemons so that launchd runs it once at boot.


<?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.your_username.watch</string>

<key>ProgramArguments</key>

<array>

<string>/Users/your_username/Documents/watch.sh</string>

</array>

<key>RunAtLoad</key>

<true/>

</dict>

</plist>


You can place your shell script file in your Documents folder with name = "watch.sh".

This shell script contains the command you specified in your original post.


Each time your machine is booted, launchd will execute the plist file.


You can read up on Launch Daemons here

https://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPSystem Startup/Chapters/CreatingLaunchdJobs.htm…


You can also read up using "man launchd.plist"


Hope this helps

Automate a terminal command

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.