Welcome to our forums! Please take a few moments to read through our Community Guidelines (also conveniently linked in the header at the top of each page). There, you'll find guidelines on conduct, tips on getting the help you may be searching for, and more!

Ubuntu service script

puithovepuithove Posts: 407Members, Plex Pass Plex Pass
Anybody put together a service script for Ubuntu yet?
«1

Comments

  • puithovepuithove Posts: 407Members, Plex Pass Plex Pass
    edited June 2011
    Well, I learned a thing or two about "upstart" and got this config put together. It's extremely simplistic but seems to do what I was wanting. It will start and stop the PMS and it will also respawn if the process exits without issuing the stop command (such as a process crash).

    do this:

    sudo nano /etc/init/plexpms.conf
    



    Paste the following into the file - you'll have to modify it with the path to wherever you put your PMS folder. Also notice at the end of the "su" line change "tvs" to the name of the user you want to execute PMS as.

    # plexpms - service job file
    
    description "Plex PMS"
    author "Plex Gods"
    
    # When to start the service
    start on runlevel [2345]
    
    # When to stop the service
    stop on runlevel [016]
    
    # Automatically restart process if crashed
    respawn
    
    # Sets nice and ionice level for job
    nice -5
    
    # What to execute
    script
            cd /home/tvs/PlexMediaServer
            su -c ./start.sh tvs
    end script
    



    Now PMS will start at system boot running as user "tvs" and will be stopped prior to system shutdown.

    You can stop the service manually by:
    sudo service plexpms stop
    


    or start it manually by
    sudo service plexpms start
    


    Enjoy!
  • serakserak Posts: 25Members, Plex Pass Plex Pass
    Thanks puithove, this works well. Saved me having to do look into it myself haha
  • FevidaFevida Posts: 67Members, Plex Pass Plex Pass
    Thnx for sharing, working great!
  • agentbikesagentbikes Posts: 51Members
    edited May 2011
    I'm trying to install this via SSH (terminal) from my Mac. Does anyone know how to save the script in GNU nano?

    Thanks!
  • FevidaFevida Posts: 67Members, Plex Pass Plex Pass
    Ctrl + X
  • agentbikesagentbikes Posts: 51Members
    Thanks puithove the script works like a charm.
    Thank you, too, Fevida for the fast reply ;)
  • puithovepuithove Posts: 407Members, Plex Pass Plex Pass
    Cool - glad it's helping you guys.
  • thomashwthomashw Posts: 140Members, Plex Pass Plex Pass
    Thanks for the script. Works great.
  • stormeriderstormerider Posts: 758Members, Plex Pass ✭✭
    Thanks, that looks a lot cleaner than the SysV init scripts I've had to deal with in the past. Hurray for upstart!
  • jkpjkp Posts: 136Members, Plex Pass
    Good work!
  • elanelan Sheriff of Plexville MauiPosts: 9,114Members, Plex Employee, Plex Pass, Plex Ninja Plex Employee
    Very nice, thanks!
  • puithovepuithove Posts: 407Members, Plex Pass Plex Pass
    I decided I wanted to set a higher priority especially for the ffmpeg process for transcoding. Sure enough Upstart has the "nice" stanza. The job is then started with that priority setting and any spawned processes (including ffmpeg) inherit it. This sets the ionice priority to the same as well. Very clean and "nice"! :)

    I've updated the top post with the nice stanza.

    On a related note I set sabnzbd which also runs on this machine to a lower priority to keep it from interfering when it's doing its par2 and unrar stuff (fairly io intensive).
  • agentbikesagentbikes Posts: 51Members
    puithove wrote:

    On a related note I set sabnzbd which also runs on this machine to a lower priority to keep it from interfering when it's doing its par2 and unrar stuff (fairly io intensive).


    Does it help? If I'm watching a movie and sab starts unpacking the movie stutters like hell. :angry:
    Could you also describe how I can do that?
  • puithovepuithove Posts: 407Members, Plex Pass Plex Pass
    agentbikes wrote:

    Does it help? If I'm watching a movie and sab starts unpacking the movie stutters like hell. :angry:
    Could you also describe how I can do that?


    Yes, it definitely will help. Check out this thread over on the Sab forums: http://forums.sabnzb...hp?topic=5052.0
  • NHiXNHiX Posts: 11Members ✭✭
    Hey puithove, awesome upstart script. I'm having a little trouble getting plex to start up. I have no problems running the start.sh script, but when I try to run

    sudo service plexmediaserver start
    


    I'm seeing this error in syslog when I change the user in the script to a system user (in my case I created a user called plex)

    plexmediaserver main process (3236) terminated with status 1
    


    I'm seeing this error in syslog when I add my normal login to the script above (I tried this as I am able to start up plex manually with no problem)

    plexmediaserver main process (3364) terminated with status 127
    


    Any thoughts on what might be causing this? I haven't been able to find any information on upstart status codes. Thanks again!
  • puithovepuithove Posts: 407Members, Plex Pass Plex Pass
    edited July 2011

    Hey puithove, awesome upstart script. I'm having a little trouble getting plex to start up. I have no problems running the start.sh script, but when I try to run


    I'm definitely no expert here. Let's start with the basics and get those out of the way:
    • You say you can run it manually by launching the start.sh script - what user are you logged in with when you do that? Is it the same "plex" user you're specifying in the service config file?
    • Are you able to launch the start.sh script as user "plex" - if not, what error do you get?
    • You say you then changed the config to start as your normal login - but did you leave the "cd <path>" statement pointing to the actual plexmediaserver directory?

    Since it appears that you setup PMS under your normal login and then created a new "plex" user I'm thinking you've probably got a permissions issue. You need to setup PMS under the user you want to run it as and make sure it's working there before it'll be able to start as that user as a service.
  • NHiXNHiX Posts: 11Members ✭✭
    I think you may be correct, I am using the plex user in the script, however I'm unable to su to the plex user (because it is a system user with no shell). The upstart script probably has the same issue... it can't su to plex. I'll try a non-system user and see if that works. I'd like to avoid creating a new potential login (I thought thats how system users worked) but it may not be possible in this case??

    Thanks!!
  • puithovepuithove Posts: 407Members, Plex Pass Plex Pass

    I think you may be correct, I am using the plex user in the script, however I'm unable to su to the plex user (because it is a system user with no shell). The upstart script probably has the same issue... it can't su to plex. I'll try a non-system user and see if that works. I'd like to avoid creating a new potential login (I thought thats how system users worked) but it may not be possible in this case??

    Thanks!!


    Ah, well I think that because of the way the PMS is running, it needs a real user. Get it set up and running as the user you want to run it under, and then I expect the Upstart piece will work fine for you. You won't have to be "logged in" as that user all the time, Upstart will start it as that user running as a service so it'll start when the system boots without having to log in.
  • NHiXNHiX Posts: 11Members ✭✭
    That did the trick, thanks puithove. I'm not a fan of these types of users being able to login to the box, so for those wondering, you can still disable login for the pms user via --disable-login or --disable-password when adding the user.
  • chaveirochaveiro Posts: 7Members
    I use this more elegant solution:

    # plexpms - service job file
    
    description "Plex Media Server"
    author "http://www.plexapp.com/&quot;
    
    # When to start the service
    start on (local-filesystems and net-device-up IFACE&#33;=lo)
    
    # When to stop the service
    stop on runlevel [&#33;2345]
    
    # Automatically restart process if crashed
    respawn
    
    # Sets nice and ionice level for job
    nice -5
    
    # What to execute
    script
        if [ -r /etc/default/plexmediaserver ]; then
            . /etc/default/plexmediaserver
        fi 
        start-stop-daemon --start -c &#036;PLEX_MEDIA_SERVER_USER --exec /usr/sbin/start_pms
    end script
    
«1
Sign In or Register to comment.