Script Fails With Launchd - Runs Fine From Command Line

Buadhai

macrumors regular
Original poster
Jan 15, 2018
133
48
Korat, Thailand
I'm trying to run autossh with launchd.

My autossh script is one line and runs fine from the command line:

Code:
/opt/local/bin/autossh -M 0 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -R 19990:localhost:22 mnewman@korat.myddns.rocks -p 10000
But it fails when run from this launchd plist:

Code:
<?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>KeepAlive</key>
    <true/>
    <key>Label</key>
    <string>com.mgnewman.autossh</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/mnewman/bin/autossh.sh</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>
I know that it's loaded:

Code:
Axe:launchagents mnewman$ launchctl load com.mgnewman.autossh.plist
com.mgnewman.autossh: Already loaded

Any ideas?
 

chown33

Moderator
Staff member
Aug 9, 2009
8,380
4,367
Pumpkindale
The things that immediately come to mind are:
1. The working directory.
2. Environment variables.

The working directory affects relative pathnames, which the command may use.

Environment variables can affect how commands work. Among other vars that may have an effect, the PATH env var determines where commands are searched for.

I suggest running the script with the 'bash' -x option, so it outputs diagnostic info to stderr or stdout. There are several ways to accomplish this in a launchd task.


I also suggest adding a shebang on the first line of your shell script. Maybe the reason it's failing under launchd is simply that the command interpreter isn't being chosen correctly.
 

Buadhai

macrumors regular
Original poster
Jan 15, 2018
133
48
Korat, Thailand
Thanks for taking the time to reply.

The shell script does have the shebang. I just didn't copy it when I posted it here.

I had already tried adding the PATH to the launchd plist. Still no joy.

I don't know how to use the -x option. (I searched, but….)

The sad thing is that the script no longer works when run from the command line so I'm even more lost today than I was yesterday.

Edit: I figured out why the script stopped running from the command line. I added the "-f" option to autossh which is supposed to move it to the background. Removing that option from the script made it work again.

Still no joy with launchd
 
Last edited:

Buadhai

macrumors regular
Original poster
Jan 15, 2018
133
48
Korat, Thailand
Working now. Turns out to get it to work with launchd I had to add two parameters to the shell script: "-f" to run in the background and "-N" execute no command. I have no idea why this worked.

Code:
#!/bin/bash

/opt/local/bin/autossh -f -M 0 -N -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -R 19990:localhost:22 mnewman@korat.myddns.rocks -p 10000