Jump to content

start as system daemon?


lesmikesell

Recommended Posts

lesmikesell

Is there an example of how to start emby as a system daemon on OSX somewhere?   My wife and I share an imac with separate user accounts.  I'd like the server to continue running regardless of which user account is active.

Link to post
Share on other sites
  • 4 weeks later...

I haven't yet but have been meaning to do this. Emby now runs on a Mac Mini somewhere in my apartment, so I've been running it in a `screen` session from the command line.

 

I have done launchd scripts before, for stuff like keeping Google Drive up back when it used to crash after a day or two, periodically clearing out /var/log/asl back when that was causing laggy terminal startup, running periodic backups, etc.

 

Look over the documentation for launchd here while perusing what you currently have running. If you run man launchd it'll show you the folders with plists. Your plist will go in /Library/LaunchDaemons. /System/Library/LaunchDaemons has a ton of good examples (that's where ssh and apache are configured, for instance).

 

To load a new custom launchd config you'll use: `sudo launchctl load -w /Library/LaunchDaemons/org.example.emby.plist`

As a suggestion, use a domain name you can remember, so you can easily figure out if your thing is loaded with `sudo launchctl list | grep example`.

Just run `launchctl` to get a full list of what you can do with it.

 

Without actually doing it myself (yet), I think you'll start off with something like this:
 

<?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>org.example.emby</string>
    <key>Program</key>
    <string>/Applications/Emby.Server.Mac.app/Contents/MacOS/Emby.Server.Mac</string>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <dict>
        <key>SuccessfulExit</key>
        <false/>
    </dict>
    <key>UserName</key>
    <string>Tuxedo</string>
    <key>ProcessType</key>
    <string>Interactive</string>
    <key>StandardOutPath</key>
    <string>/var/log/emby.out.log</string>
    <key>StandardErrorPath</key>
    <string>/var/log/emby.err.log</string>
</dict>
</plist>

Running `man launchd.plist` or looking here will explain every option available.

 

KeepAlive with SuccessfulExit false means that launchd will only restart the process if it exited with a failure. If KeepAlive was just true, and you click server shutdown, then launchd would immediately restart the server. With the setting as I have it, shutting down the server through the web-ui should actually stop it and you'll have to run `sudo launchctl start org.example.emby` to start it again manually, but if the server crashes launchd will automatically restart it.

I'm not entirely sure what launchd will think happened when you click server restart in the web ui. If it starts back up in a new process, then launchd will no longer think it's running so having KeepAlive true set will cause launchd to try starting emby from scratch, which would then mean two processes, and the later one is going to crash, and launchd will restart it, etc. etc. I have to test around this. Google Drive caused a similar problem because I had it running from a LaunchDaemon, but I forgot to remove it from my OSX startup items, so the launchd process was stuck in a restart-crash loop. Like I said, I want to test this all.

 

UserName is so that emby uses ~/.config/emby-server/ instead of... actually, on my systems that appears to be my primary account anyway, but I'd rather not risk a whole bunch of stuff being created in ~/username/.config with root:admin permissions rather than Tuxedo:staff.

 

StandardOutPath is just so I don't see You have new mail in /var/mail every time I open a new terminal.

 

I'm probably going to be using my own ffmpeg/ffprobe so I'll be using ProgramArguments and a list of arguments rather than just Program. I install emby and ffmpeg via homebrew because the Mini it's running on is entirely headless.

Edited by Tuxedo
Link to post
Share on other sites
  • 2 years later...
partimers

Anyone getting to Launch Emby as Daemon on Macs? I tried this method (did the necessary corrections for my user name and also changed the path to the application to "/Applications/EmbyServer.app/Contents/MacOS/EmbyServer" as that is what I have on my mac (perhaps this changed since the topic was created). 

 

I keep getting error "Service could not initialize: 16G1212: xpcproxy" on the console and the Emby does not start as Daemon.

Any clue?

Link to post
Share on other sites

Anyone getting to Launch Emby as Daemon on Macs? I tried this method (did the necessary corrections for my user name and also changed the path to the application to "/Applications/EmbyServer.app/Contents/MacOS/EmbyServer" as that is what I have on my mac (perhaps this changed since the topic was created). 

 

I keep getting error "Service could not initialize: 16G1212: xpcproxy" on the console and the Emby does not start as Daemon.

Any clue?

 

Hi, i  haven't tested this, but if you try running with the command line param -service, then it will disable the UI components. See if that makes any difference. Thanks.

Link to post
Share on other sites
  • 1 year later...

I've been converting various items into services today. Emby was one of them, each running as their own user.

 

To load Emby 4.2.1 as a service, the following will work after you have installed Emby and run it successfully as directed by the instructions (you should use a standard user without sudo privs if you want more security):

 

Create a file: org.emby.plist with the contents below and just replace the [username] with the account name you want Emby to run on. Then from the terminal, run this command:

 

    sudo cp <path to file>  /Library/LaunchDaemons/ 

 

and then run:

 

    sudo launchctl load  /Library/LaunchDaemons/org.emby.plist

 

to start emby. 

 

    sudo launchctl unload  /Library/LaunchDaemons/org.emby.plist

 

will stop it. Do allow a few second for the server to shutdown prior to restarting it. 

<?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>org.emby</string>

    <key>EnvironmentVariables</key>
    <dict>
      <key>PATH</key>
      <string>/usr/local/bin/:/usr/bin:/usr/sbin:/sbin:$PATH</string>
      <key>LC_CTYPE</key>
      <string>UTF-8</string>
    </dict>

    <key>Program</key>
    <string>/Applications/EmbyServer.app/Contents/MacOS/EmbyServer</string>
    
    <key>ProgramArguments</key>
    <array>
        <string>/Applications/EmbyServer.app/Contents/MacOS/EmbyServer</string>
        <string>-service</string>
    </array>

    <key>AbandonProcessGroup</key>
    <false/>

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

    <key>KeepAlive</key>
    <dict>
      <key>SuccessfulExit</key>
      <false/>
    </dict>

    <key>UserName</key>
    <string>[your account username here]</string>

    <key>ProcessType</key>
    <string>Background</string>

  </dict>
</plist>

Edited by elgrit
Link to post
Share on other sites

Hi, that's great info, thanks for posting !

 

 

I should mention that's running on the latest 10.13.6 as of today. It successfully starts and stops. 

 

I've had iOS, AppleTV, and Web clients all running against it. 

Link to post
Share on other sites
  • 9 months later...
TheWishfulOne

This did not work for me (using Mojave). I did the following:

  1. created the plist file with text.app
  2. checked that the syntax was OK using ‘plutil’
  3. loaded it to /user/library/launchagents
  4. the Emby server works when that user logs on - and I can access it with my iPad
  5. moved the plist file to /Library/launchdaemons
  6. changed the ownership to root
  7. loaded it with ‘sudo launchctl load /library/launchdaemons/org.emby.plist’
  8. it does not work. I get error 78

What is the issue? Also why do I need to use launchctl commands. Shouldn't the plist file run automatically when the machine is booted?

 

Sorry but I'm not familiar with plist files or unix so have to ask some basic questions!

 

Thanks

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...