Launch daemon won't launch bash script in OSX - macos

I am learning how to create a Launch Daemon. This job aims to create a text file on on the user's desktop on boot up. The plist file is placed in Library/LaunchDaemons/. Here is the plist file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.test.createFile</string>
<key>Program</key>
<string>/Users/hlocng/Desktop/createFile.sh</string>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
... and here is the bash script:
#!/bin/bash
touch /Users/hlocng/Desktop/newFile.txt
I tried launchctl load /Library/LaunchDaemon/com.test.createFile and there was no error but no file is created, not even when I reboot the computer.
I do not know what I am doing wrong. Any help is appreciated. Thanks you!

The permission of the plist file must belong to root. ls -l can be used to check the permission. sudo chown root:wheel filePath changes the permission to root and everything worked fine.
Thanks to #Ken Thomases!

Related

OSX asdf Launch Daemon Does Not Have Access to Languages

I have a launch daemon plist that runs a python script. The problem is that it is unable to run the script because it does not have access to python.
I am trying to make a launch daemon to change mac address (https://github.com/feross/SpoofMAC)
Here is the plist file in /Library/LaunchDaemons:
<?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>MacSpoof</string>
<key>ProgramArguments</key>
<array>
<string>/Users/username/.asdf/shims/spoof-mac.py</string>
<string>randomize</string>
<string>en0</string>
</array>
<key>StandardOutPath</key>
<string>/Users/username/Projects/MAC/out.log</string>
<key>StandardErrorPath</key>
<string>/Users/username/Projects/MAC/err.log</string>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
The Error Log reads:
No version is set for command spoof-mac.py
Consider adding one of the following versions in your config file at
python 3.11.1
My tool_version does include Python, how can I make this work?
Okay, I kinda got it working. I moved the plist file from /Library/LaunchDaemons to /Library/LaunchAgents and it works because LaunchAgents run after the user is logged in. It's not exactly what I wanted, but it works.

Why won't LaunchAgents run my Automator app?

I'd like to run an app I created via Automator every 5 minutes, so I placed the following com.user.wilson.plist file in this folder:
/Library/LaunchAgents
<?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.user.wilson</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/open</string>
<string>-a</string>
<string>/Users/paul/Documents/Wilson/Script/mt-wilson-background_app</string>
</array>
<key>StartInterval</key>
<integer>300</integer>
</dict>
</plist>
Then, I loaded it using the following command in the terminal:
launchctl load Library/LaunchAgents/com.user.wilson.plist
but for some reason, the app never runs.
I can, however, successfully run the app using this command:
/usr/bin/open -a /Users/paul/Documents/Wilson/Script/mt-wilson-background_app
Any ideas why the .plist file won't do what I'm expecting it to?
In order to see what's going wrong, you can add a log file in your plist 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>Label</key>
<string>com.user.wilson</string>
<key>StandardErrorPath</key>
<string>/Users/paul/Documents/Wilson/Script/err.log</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/open</string>
<string>-a</string>
<string>/Users/paul/Documents/Wilson/Script/mt-wilson-background_app</string>
</array>
<key>StartInterval</key>
<integer>300</integer>
</dict>
</plist>
Note: For the modifications to take effect, unload and load again:
launchctl unload Library/LaunchAgents/com.user.wilson.plist
launchctl load Library/LaunchAgents/com.user.wilson.plist
Typically, if the err.log says it can't find your app, it means it's a permission issue.
I would suggest you try to move your app from /Users/paul/Documents/Wilson/Script/mt-wilson-background_app to /Users/paul/Documents/mt-wilson-background_app
Then update your plist accordingly, unload an reload your plist, is it working better now?
I ran into nearly the exact same problem. I finally (FINALLY!!!) found a cure that worked for me.
Originally, the broken version of the .plist launch agent that wouldn't run no matter what I tried was in /Library/LaunchAgents. Moving the agent to /Users/[me]/LaunchAgents eliminated the "Application Not Running" error.
It seems counterintuitive since the root agent should be able to run everything from any location, but I can only guess that AppleScript's check to see if an app is running or not is user account-dependent somehow. I'm betting there's something you can add to the AppleScript to actually fix this the "right" way, but this works well enough for me, so I'm taking the win.

Launchd PATH issue with TeamCity

I recently asked this question, and got everything working appropriately. The build agent is launching, RAM disk is being created.
It seems that there is an issue with the PATH environment variable. When I run the same command from terminal, all my builds build fine. When it is launched from launchd, I get errors like "can not find mono" or "can not find nuget" both of those paths are environment variables.
How can I modify the PATH variable for a launchd file?
Here is my Launch Agent:
<?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.datafinch.teamcity</string>
<key>Program</key>
<string>/Users/administrator/startup.sh</string>
<key>RunAtLoad</key>
<true/>
<key>AbandonProcessGroup</key>
<true/>
</dict>
</plist>
And here is the script startup.sh
#!/bin/bash
setenv PATH /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Frameworks/Mono.framework/Versions/Current/Commands
DISK=`/usr/bin/hdiutil attach -nobrowse -nomount ram://16777216`
/usr/sbin/diskutil erasevolume HFS+ "RamDiskCache" $DISK
/Users/administrator/buildAgent/bin/agent.sh start
When I set the PATH explicitly in my startup.sh it still doesnt work.

Mac OSX LaunchDaemon on Startup, Shell Script with SSH

I'm hoping someone can help me with this. I've been working on it literally all day...
I want a LaunchDaemon to execute a shell script at startup. Here is my plist file, located at /Library/LaunchDaemons/com.mhi.backup.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>Label</key>
<string>com.mhi.backup</string>
<key>UserName</key>
<string>Joel</string>
<key>GroupName</key>
<string>Admin</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/mhi_websites_backup.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
It executes correctly when I load it from the terminal (launchctl load /Library/LaunchDaemons/com.mhi.backup.plist), but not on startup.
Here is my script, for reference:
#!/bin/bash
sleep 15 #delay script to ensure time for network connection
ssh user#hostname << HERE
mysqldump -u <user_name> -pPASSWORD --all-databases | lzma > alldatabases.sql.lzma
tar cfa backup-`date '+M%mD%dY%y'`.tar.lzma webapps alldatabases.sql.lzma
exit
HERE
scp user#hostname:backup-`date '+M%mD%dY%y'`.tar.lzma /Users/Joel/Desktop
Could someone please help?
Thanks so much,
JG
What errors are you seeing? I would expect that you may have a PATH problem here. Where is mysqldump? If it's in /usr/local/bin, then you probably want to make that explicit, or set the default path in /etc/launchd.conf.
Is the plist owned by root? If a plist in /Library/Launch{Agents,Daemons}/ is not owned by root, it can be loaded with launchctl without sudo, but it is not loaded at login.
You could also try moving the plist to /Library/LaunchAgents/ and adding a LimitLoadToSessionType key:
<key>LimitLoadToSessionType</key>
<array>
<string>LoginWindow</string>
<string>Aqua</string>
</array>
See the Daemons and Agents tech note.

Start script osx launch daemon

I'm trying to run an executable on my machine (mbp retina osx mountain lion) on startup.
This is what the script looks like:
<?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.netresponsibility.daemon</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/net-responsibility</string>
<string>--daemon</string>
</array>
<key>OnDemand</key>
<true/>
</dict>
</plist>
I put it in the /System/Library/LaunchDaemons/ directory. It's named com.netresponsibility.daemon.plist and has the same permissions as all the others.
-rw-r--r-- 1 root wheel 420 Oct 11 12:39 com.netresponsibility.daemon.plist
When I restart the executable is not called. Any ideas as to what I'm missing?
First, you shouldn't ever put anything in /System/Library. That path is reserved for OS X files. You should put your files in /Library/LaunchDaemons/.
Second, you need to tell the system to load your plist, it's not enough to just put it there. To do that, you use launchctl:
$ sudo launchctl load -w /Library/LaunchDaemons/com.netresponsibility.daemon.plist
For more information, man launchctl.

Resources