Launchd is not working - macos

I am having troubles making a Launchd script. It is fairly simple, all it needs to do is launch an application - the location is: /Library/Desktop Pictures/wallpaper
I've loaded the file using terminal, and terminal gives no errors but the script does not execute. I need the script to execute every 1 hour.
The 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>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>Label</key>
<string>wallpaper.restart</string>
<key>ProgramArguments</key>
<array>
<string>/Library/Desktop Pictures/wallpaper.wallpaperapp.app</string>
</array>
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>1</integer>
</dict>
</dict>
</plist>
Please Help.
Thanks,
Devansh
EDIT:
Below is the next better version of the above code, but still not much luck :(
<?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>KeepAlive</key>
<dict>
<key>AfterInitialDemand</key>
<true/>
</dict>
<key>Label</key>
<string>com.plisterine.backgroundchanger</string>
<key>LowPriorityIO</key>
<false/>
<key>Nice</key>
<integer>0</integer>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/osascript</string>
<string>/Library/Desktop Pictures/wallpaper/wallpaper.scpt</string>
</array>
<key>StartInterval</key>
<integer>3600</integer>
</dict>
</plist>

StartCalendarInterval runs the script at a specific time every day, in your case at 01:00 (1 am)
To run it every hour you have to write
<key>StartInterval</key>
<integer>3600</integer>

Related

How can I avoid using a bash script in my launchd plist?

This is my launchd plist
<?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>KeepAlive</key>
<true/>
<key>Label</key>
<string>com.localhost.hexo</string>
<key>ProgramArguments</key>
<array>
<string>/Users/frankg/dev/bin/start-hexo.sh</string>
</array>
<key>UserName</key>
<string>frankg</string>
<key>RunAtLoad</key>
<true/>
<key>StandardErrorPath</key>
<string>/Users/frankg/hexo</string>
<key>StandardOutPath</key>
<string>/Users/frankg/hexo</string>
</dict>
</plist>
This is my bash script
hexo server --cwd /Users/frankg/dev/code/apps/blog >> /tmp/MyLaunchdTest.out
This is working. How can I avoid the use of a bash script.
Specify the full path to the hexo executable, followed by each argument to it as a separate string in the ProgramArguments array. I'm a bit confused by the various output redirects -- in the current .plist you specify output to /Users/frankg/hexo, but in the script you redirect it to /tmp/MyLaunchdTest.out instead; I assume the latter is what you actually want? AIUI you could also use the WorkingDirectory key to replace the --cwd option. Something like this:
<?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>KeepAlive</key>
<true/>
<key>Label</key>
<string>com.localhost.hexo</string>
<key>ProgramArguments</key>
<array>
<string>/path/to/hexo</string>
<string>server</string>
</array>
<key>UserName</key>
<string>frankg</string>
<key>RunAtLoad</key>
<true/>
<key>StandardErrorPath</key>
<string>/Users/frankg/hexo</string>
<key>StandardOutPath</key>
<string>/tmp/MyLaunchdTest.out</string>
<key>WorkingDirectory</key>
<string>/Users/frankg/dev/code/apps/blog</string>
</dict>
</plist>

launchd start automatically

Trying to create a task that starts on every day at 4AM to restart my mac and execute some maintenance scripts. This is my script:
<?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>local.job</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/osascript</string>
<string>/Users/Media/Scripts/RestartMac.scpt</string>
</array>
<key>RunAtLoad</key>
<false/>
<key>StandardErrorPath</key>
<string>/Users/Media/Scripts/Output/RestartMac.txt</string>
<key>StandardOutPath</key>
<string>/Users/Media/Scripts/Output/RestartMac.txt</string>
<key>StartCalendarInterval</key>
<array>
<dict>
<key>Hour</key>
<integer>04</integer>
<key>Minute</key>
<integer>00</integer>
</dict>
</array>
</dict>
</plist>
If i run launchctl unload / load to reload my plist file it will execute the script. I am a bit confused cause i have set the RunAtLoad parameter to false. Tried a lot of things but i am unable to get it running. Has anyone an idea what is my fault here?
Operation system: OS X 10.9
have you tried
<key>AbandonProcessGroup</key>
<true/>

Detect Mac Application moved to trash folder using launch Agents

I want to run a script when my application moved into trash folder and I come to know that using launch agents its possible, I googled and made the plist but its not working or not calling the script..
<?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.meet.anywhere</string>
<key>WatchPaths</key>
<array>
<string>~/.Trash</string>
</array>
<key>ProgramArguments</key>
<array>
<string>/Users/eclit/Desktop/DeleteScript.scpt</string>
</array>
<key>KeepAlive</key>
<false/>
</dict>
</plist>
launchd cannot run the applescript directly. You need to use the command line tool osascript to run the applescript. As such your ProgramArguments portion should look like the following. Also, there's no need for the KeepAlive section. It does nothing so I'd remove it.
<key>ProgramArguments</key>
<array>
<string>osascript</string>
<string>/Users/eclit/Desktop/DeleteScript.scpt</string>
</array>
<?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.meet.anywhere</string>
<key>WatchPaths</key>
<array>
<string>~/.Trash/AppName.app</string>
</array>
<key>ProgramArguments</key>
<array>
<string>osascript</st
<string>/Users/eclit/Desktop/DeleteScript.scpt</string>
</array>
<key>KeepAlive</key>
<false/>
</dict>
</plist>

Creating a LaunchDaemon for Gitblit

I want to create a LaunchDaemon on OSX Mavericks for my Gitblit, this is my file (org.gitblit.plist):
<?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>KeepAlive</key>
<true/>
<key>Label</key>
<string>org.gitblit.plist</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/java</string>
<string>--baseFolder</string>
<string>data</string>
<string>-jar</string>
<string>/Applications/gitblit/gitblit.jar</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
It's not working, but when I try java -jar gitblit.jar --baseFolder data on /Applications/gitblit everything works fine. Am I missing something?
The Gitblit GO distribution comes with a shell script gitblit.sh that will work for this purpose. This is the version of the LaunchAgent plist ~/Library/LaunchAgents/org.gitblit.plist I used.
Note the addition of the WorkingDirectory key, as well as log files in case of errors.
<?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>KeepAlive</key>
<true/>
<key>Label</key>
<string>org.gitblit.plist</string>
<key>ProgramArguments</key>
<array>
<string>/bin/bash</string>
<string>/Applications/gitblit/gitblit.sh</string>
</array>
<key>WorkingDirectory</key>
<string>/Applications/gitblit/</string>
<key>RunAtLoad</key>
<true/>
<key>StandardOutPath</key>
<string>/Applications/gitblit/stdout.txt</string>
<key>StandardErrorPath</key>
<string>/Applications/gitblit/sterr.txt</string>
</dict>
</plist>
So I finally ended up cheating like this:
I saved this script on /opt/scripts/start_gitblit.sh:
#!/bin/bash
cd /Applications/gitblit/
java -jar gitblit.jar --baseFolder data
Then I stored this property list on ~/Library/LaunchAgents/org.gitblit.plist:
<?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>KeepAlive</key>
<true/>
<key>Label</key>
<string>org.gitblit.plist</string>
<key>ProgramArguments</key>
<array>
<string>/bin/bash</string>
<string>/opt/scripts/start_gitblit.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
Everything is working now.

How to register(install) as service java application with JSVC wrapper on MacOS X

I have an application which i want to run as service on MacOS X. I used JSVC as wrapper and currently it starts in console just fine, shutdown process is correct, etc. So now i have to register it as service. Found some manuals, wrote .plist file. Next i executed
sudo launchctl load /Library/LaunchDaemons/my.service.plist
sudo launchctl start my.service
And nothing happened. Service didn't started.
plist contents:
<?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>my.service</string>
<key>ProgramArguments</key>
<array>
<string>/servertest/MYService</string>
<string>-jvm</string>
<string>server</string>
<string>-outfile</string>
<string>out.txt</string>
<string>-errfile</string>
<string>err.txt</string>
<string>-verbose</string>
<string>-debug</string>
<string>-home</string>
<string>/System/Library/Frameworks/JavaVM.framework/Home</string>
<string>-cp</string>
<string>./lib/hsqldb.jar:./lib/my-wrapper.jar:./lib/commons-daemon-1.0.8.jar</string>
<string>my.service.DaemonMac</string>
</array>
<key>WorkingDirectory</key>
<string>/servertest</string>
<key>StandardOutPath</key>
<string>/servertest/stdout.log</string>
<key>StandardErrorPath</key>
<string>/servertest/stderr.log</string>
<key>KeepAlive</key>
<false/>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
What am i doing wrong?
Okay, nobody knows, i guess. Anyway, i handled it myself. Here's example which worked for me:
<?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>KeepAlive</key>
<false/>
<key>Label</key>
<string>myserver</string>
<key>OnDemand</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>/opt/MYServer/MYServer</string>
<string>-server</string>
<string>-outfile</string>
<string>/opt/MYServer/out.txt</string>
<string>-errfile</string>
<string>/opt/MYServer/err.txt</string>
<string>-verbose</string>
<string>-debug</string>
<string>-nodetach</string>
<string>-home</string>
<string>/System/Library/Frameworks/JavaVM.framework/Home</string>
<string>-cp</string>
<string>/opt/MYServer/lib/hsqldb.jar:/opt/MYServer/lib/my-wrapper.jar:/opt/MYServer/lib/commons-daemon-1.0.8.jar</string>
<string>my.service.DaemonMac</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StandardErrorPath</key>
<string>/opt/MYServer/stderr.log</string>
<key>StandardOutPath</key>
<string>/opt/MYServer/stdout.log</string>
<key>WorkingDirectory</key>
<string>/opt/MYServer</string>
</dict>
</plist>

Resources