VERY simple Launchd plist not running my script

2019-01-31 19:11发布

问题:

I'm trying to figure out why my launchd script is not working. It is extremely simple, but I am new to the mac environment and trying to get accustomed. Here's my plist. I know ProgramArguments is required, so I just put the script path in there.

<?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.tomcat.plist</string>   
  <key>ProgramArguments</key>  
  <array>  
    <string>/opt/apache-tomcat-5.5.27/bin/startup.sh</string>  
  </array>  
  <key>OnDemand</key>  
  <false/>  
</dict>  
</plist>

When I try to run launchctl load <name> it seems to load properly (in that it doesn't give me any error messages), but the script doesn't seem to be executing, even on reboot.

I've used all the examples I've found online and I can't figure out why this isn't running my script on start up.

回答1:

Just in case anyone else runs across this issue, and already has <key>RunAtLoad</key><true/> in their plist, I want to provide some additional solutions.

Double check permissions to make sure that your script is executable (look for an 'x'):

ls -l /opt/apache-tomcat-5.5.27/bin/startup.sh

Modify permissions if necessary:

chmod +x /opt/apache-tomcat-5.5.27/bin/startup.sh

Also run the script directly first and make sure it works:

/opt/apache-tomcat-5.5.27/bin/startup.sh

If the script is executable, and runs fine directly, try tailing the system log to debug launchd:

sudo launchctl log level debug 
tail -f /var/log/system.log

The -f flag (basically) continually shows the end (latest entries) of the log. You can remove this flag to just print a snapshot of the end of the log. If you use this flag, you'll need to open a new terminal to run other commands. Press CTRL + C to end the tail session. For more information:

man tail

When you're finished debugging:

sudo launchctl log level error

There are other log levels. For more information:

man launchctl

If you make any changes to the script or plist, make sure you reload the plist. For example:

launchctl unload ~/Library/LaunchAgents/com.tomcat.plist
launchctl load ~/Library/LaunchAgents/com.tomcat.plist

If you ONLY made changes to the script and NOT the plist, you can just restart the plist:

launchctl stop com.tomcat.plist
launchctl start com.tomcat.plist

If you add the following key-value to your plist:

<key>KeepAlive</key>
<true/>

Then you can just run:

launchctl stop com.tomcat.plist

And it will restart automatically.

If none of this helps, and you're specifically having problems with setting up Tomcat on OS X, this tutorial might be of help.



回答2:

To make your script run automatically when you call launchctl load, you need to add :-

<key>RunAtLoad</key>
<true/>

Alternatively you could use :-

launchctl start com.tomcat.plist


回答3:

Although I imagine most people will not have this problem, I figure it is worth putting here since I spent nearly two hours trying to figure out why launchd load was not working despite returning a 0 exit code.

The problem was simple. My plist file had the wrong file extension (I had "plst"), and launchctl was silently refusing to load the file. Changing the extension to plist resolved the issue.



标签: macos launchd