Start script osx launch daemon - macos

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.

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.

Run shell script at boot time (OS X)

I've been trying to get this to work but haven't had any success.
I have this jar file: /Users/ivanmorelos/Documents/guiprueba.jar
I also made this sh file:
#!/bin/bash
java -jar guiprueba.jar
the path to this sh is: /Users/ivanmorelos/Documents/guiprueba.sh
If I run this script from the terminal like
bash /Users/ivanmorelos/Documents/guiprueba.sh
then the jar executes perfectly.
Now I went to /Library/LaunchDaemons/ and made the following com.ivan.gui.plist:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple$
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.ivan.gui</string>
<key>ProgramArguments</key>
<array>
<string>/Users/ivanmorelos/Documents/guiprueba.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>UserName</key>
<string>root</string>
</dict>
</plist>
*I made the sh executable and it belongs to ivanmorelos, so does the jar file.
*The plist belongs to root.
As I understand this should make my jar execute at boot time and therefor, before the login screen comes up. Am I correct? But any way nothing happens and I don't know where the error is. Could you please help me? I would really appreciate it.
EDIT
I made the following change in the script:
java -jar guiprueba.jar
for
touch texto.txt
to create a simple file but it still doesn't do it.
So I finally found the solution to this problem.
I left my jar at /Users/ivanmorelos/Documents/ owned by root:wheel. A ls -l shows this:
-rw-r--r-- 1 root wheel 2365 Jul 29 11:27 guiprueba.jar
Then moved my plist to /System/Library/LaunchDaemons/ with the following permissions:
-rw-r--r-- 1 root wheel 372 Jul 29 12:49 com.ivan.plist
Also moved my script to /usr/sbin/ with the following permissions:
-rwxr-xr-x 1 root wheel 129 Jul 29 13:07 /usr/sbin/guiprueba
and as the ls -l shows I removed the .sh
And that's it. Now my script and jar execute at boot time.
This is the final plist:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs$
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.ivan</string>
<key>ProgramArguments</key>
<array>
<string>/usr/sbin/guiprueba</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>

Launch daemon won't launch bash script in OSX

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!

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.

Resources