Search code examples
macosshosx-mavericksbootlaunchd

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.


Solution

  • 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>