user465465
user465465

Reputation: 441

OSX create launchd service for bash script execution

I have written a bash script to continuously checks a service. The script has some functions and also calls other source bash files and works fine when executed manually. The script has a continuous loop statement something like,

while true; do
{

$path/is_it_running
if [ "$?" == "0" ]; then
{
echo "yes, it is running"
$path/test
if [ "$?" != "0" ]; then
fix_the_issues
fi
}

else 
echo "It is not running!"

fi
sleep 10
}
done

/Library/LaunchDaemons/my.own.service.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>my.own.service</string>
        <key>ProgramArguments</key>
        <array>
            <string>/bin/bash</string>
            <string>/path/to/bash/script</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
         <key>KeepAlive</key>
        <true/>
        <key>StandardOutPath</key>
        <string>/var/log/my-service.log</string>
    </dict>
</plist>

sudo launchctl load -w /Library/LaunchDaemons/my.own.service.plist

/var/log/system.log:

com.apple.xpc.launchd[1] (com.apple.quicklook[80608]): Endpoint has been activated through legacy launch(3) APIs. Please switch to XPC or bootstrap_check_in(): com.apple.quicklook

The issue what i'm facing: The script is not running exactly how it runs when manually executed. I can say launchd triggers the script as I could see "yes, it is running" and "It is not running!" messages from the plist StandardOutPath log file /var/log/my-service.log.

Can anyone help me here to successfully run the bash script as service?. It looks difficult for me in osx unlike debian/centos. BTW, it's osx EI Captain and Sierra editions.

Upvotes: 0

Views: 5489

Answers (2)

tripleee
tripleee

Reputation: 189357

This is a stylistic fix for the bash antipatterns in your script. I would post it as a comment but comments cannot contain code formatting.

while true; do
#{  no braces necessary here
# note indentation
    # Don't examine $? -- if already does that for you
    if $path/is_it_running; then
    #{ no braces necessary here
    # note indentation
        echo "yes, it is running"
        # Don't examine $? -- if already does that for you
        if $path/test; then
            fix_the_issues
        fi

    #}  No braces necessary       
    else 
        # Note indentation
        echo "It is not running!"
    fi
    sleep 10
#} No braces necessarsy
done

You might also want to visit http://shellcheck.net/ for an automated review of your scripts. (Though it doesn't examine indentation, which is purely for human legibility.)

Upvotes: 3

Ashutosh Jindal
Ashutosh Jindal

Reputation: 18869

The following works for me for a bash script on OSX 10.10:

<?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>Disabled</key>
    <false/>
    <key>EnvironmentVariables</key>
    <dict>
        <key>PATH</key>
        <string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin</string>
    </dict>
    <key>KeepAlive</key>
    <dict>
         <key>SuccessfulExit</key>
         <false/>
     </dict>
    <key>Label</key>
    <string>SomeNameHere</string>
    <key>ProgramArguments</key>
    <array>
        <string>/bin/bash</string>
        <string>/path/to/bash/script</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

I used Lingon to create it. You could use the free trial for it as well to solve your current issue.

You would need to use version 4:

Requires macOS 10.11 or later (works perfectly with macOS 10.12 Sierra)

Upvotes: 3

Related Questions