Start Appium server upon system boot (MacOS, launchctl, launchd)

Hi. I’m trying to setup Appium server to stat automatically after system startup. Also I’d like it to restart after a crash. Appium was installed as a npm package and it launches and works fine when I type “appium” in terminal.
What I did is created file io.appium.server.plist in /Library/LaunchAgents/ with the following content:

<?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>io.appium.server</string>
        <key>OnDemand</key>
        <false/>
        <key>UserName</key>
        <string><valid_user_name></string>
        <key>GroupName</key>
        <string>staff</string>
        <key>ProgramArguments</key>
        <array>
                <string>appium</string>
                <string>--session-override</string>
        </array>
</dict>
</plist>

Then assigned proper owner (chown) and permissions (chmod) to this file and executed the command:

sudo launchctl load /Library/LaunchDaemons/io.appium.server.plist

For some reason this did not start appium server, I cannot connect to it. The “launchctl list” command reported that my task exited with 78 status code, but in Appium’s documentation I cannot find what’s meant by this exit code. Can anybody help please?

I haven’t done it the same way as you, but creating an automator application for my start appium shell script then adding that automator app to startup items works fine.

This will not relaunch appium after its crash.

If you have a script that checks whether appium is running on the specified port and starts if if not true, it will relaunch appium after crash.

It was the only way I could get multiple appium instances running (logging to the same file) without any terminal window open and without throwing error messages.

I feel like building my own script to check appium status and restart it is bad solution since this functionality is already build into operating system. All you need is to use it. So I’m trying to investigate why Appium exits with status code (exit code) 78 and how to fix this.

I recently found a blog post where such task was resolved the same way, but for some reason this is not working on my end, so I need assistance: http://www.michaelvoong.com/2013/06/19/starting-appium-via-launchd/

the Appium will not started if you put appium –session-override, because launchctl it is not bash, it could not locate the appium.
you could try
/usr/local/bin/node
/usr/local/lib/node_modules/appium/bin/appium.js

to replace appium in your configuration. it is working in my side

Wonderful, appium starts now, thanks @jackysf !

Another issue came up. For Android it cannot find ANDROID_HOME varibale:

Could not find adb. Please set the ANDROID_HOME environment variable with the Android SDK root directory path.

and for iOS it can’t find ideviceinstaller:

Could not initialize ideviceinstaller; make sure it is installed and works on your system

@jackysf , could you help to configure this please?

I have no idea about Android.
for iOS, can you confirm you have xcode and ios simulator installed in your local?
if it is, try https://github.com/appium/appium/issues/4584

@jackysf , yes, everything is set up and works fine when I start appium from terminal. But when Appium is started by launchd, it rises that errors. I attempted to set ANDROID_HOME variable in /etc/launchd.conf , but this didn’t help… :frowning:

When I started appium-doctor.js as launchd agent, it’s output was:

Running iOS Checks
Б°■ Xcode is installed at /Applications/Xcode.app/Contents/Developer
Б°■ Xcode Command Line Tools are installed.
Б°■ DevToolsSecurity is enabled.
Б°■ The Authorization DB is set up properly.
Б°■ Node binary found at /usr/local/bin/node
Б°■ iOS Checks were successful.

Running Android Checks
Б°√ ANDROID_HOME is not set
Appium-Doctor detected problems. Please fix and rerun Appium-Doctor.

When I run doctor from terminal:

$ node /usr/local/lib/node_modules/appium/bin/appium-doctor.js
Running iOS Checks
Б°■ Xcode is installed at /Applications/Xcode.app/Contents/Developer
Б°■ Xcode Command Line Tools are installed.
Б°■ DevToolsSecurity is enabled.
Б°■ The Authorization DB is set up properly.
Б°■ Node binary found at /usr/local/bin/node
Б°■ iOS Checks were successful.

Running Android Checks
Б°■ ANDROID_HOME is set to "/Users/username/Desktop/android-sdk-macosx"
Б°■ JAVA_HOME is set to "/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home."
Б°■ ADB exists at /Users/username/Desktop/android-sdk-macosx/platform-tools/adb
Б°■ Android exists at /Users/username/Desktop/android-sdk-macosx/tools/android
Б°■ Emulator exists at /Users/username/Desktop/android-sdk-macosx/tools/emulator
Б°■ Android Checks were successful.

Б°■ All Checks were successful

I think the root cause of this is bash not been init as well.
sorry, i haven’t solution for it yet.
Please let me know if you resolve this

@jackysf , I’ve added these environment variables, including PATH, into plist file. This did work but I’m not sure whether this solution is correct:

    <key>EnvironmentVariables</key>
    <dict>
            <key>ANDROID_HOME</key>
            <string>/path/to/android-sdk-macosx</string>
            <key>JAVA_HOME</key>
            <string>/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home</string>
            <key>PATH</key>
            <string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:</string>
    </dict>

work solution is a good solution.:slight_smile:
I am also thinking another solution.

How about set your MAC OSX as auto login? The bash should be init with your login, then the PATH probably work.

Auto login is already on. I guess launchd is executed before any user gets a chance to log in.

depends on where you put the plist. if you put into ~/Library/LaunchAgents, then launchd must process after you login.
if put into /Library/LaunchDaemons, the launch with system start

My plist is under ~/Library/LaunchAgents

put your env parameters into /etc/profile will work.

echo “export JAVA_HOME=$(/usr/libexec/java_home)” >> /etc/profile
echo “export ANDROID_HOME=/usr/local/opt/android-sdk” >> /etc/profile

Hi Simon!
Could you share the details I am new to Mac-Os. I am trying to run appium in background I have written script which will run in background even if we close the terminal.
I tried it with automator but i cannot see node process running in activity control
how can I achieve with startup items.?
It is desperately needed to conclude in my standup call.

Hi al007!
Could you share the complete script (.plist) I am desperately needed this to run in background along with session override as we are automating appium and selenium for android tests.
I have written the script which will run in background even if terminal close but i need this at startup

Thanks
Sai

Hola, I just go through setting up appium server using launchctl and it’s finally working… Here’s my gists which contains working plist https://gist.github.com/risinek/fd250ff81ce22d2c7744b1567de53675

What do people use for log rotation? I’ve been trying newsyslog, but I find that I lose logging as soon as it fills up the specified number of backups.