How to start a screen session using an upstart job with a non privileged user

gnu-screenstartupupstart

I am trying to create an Upstart job that starts a new screen session on boot, in which I would like to automatically start a java executable. Here is the .conf file I am currently trying to get to work, although I have tried several others:

description     "Run the bungeecord jar"

start on (local-filesystems and net-device-up IFACE=eth0 and runlevel [2345])
stop on runlevel [016]

exec start-stop-daemon --start -c ridog --exec /usr/bin/screen -- -dmUS BungeeCord java -server -XX:UseSSE=4 -XX:+UseCMSCompactAtFullCollection -XX:MaxPermSize=356m -XX:ParallelGCThreads=6 -XX:+UseConcMarkSweepGC -XX:+DisableExplicitGC -XX:+CMSIncrementalMode -XX:+CMSIncrementalPacing -XX:+UseCompressedOops -XX:+AggressiveOpts -Xmx256M -jar BungeeCord.jar

pre-stop script
    screen -S BungeeCord -X foo "end^M"
end script

To my knowledge, the script seems to work fine, I can run sudo start bungeecord and get the intended result, however, restarting the machine does not work. Instead, I get this error in the /var/log/upstart/bungeecord.log:

Cannot make directory '/var/run/screen': Permission denied

I've looked up this error and the search results are obscure and inconclusive. I've tried running the command as root, this removes the error but still no screen session. I've tried different commands like this:

su ridog -c "screen -dmS BungeeCord java -jar /home/ridog/BungeeCord/BungeeCord.jar"

Best Answer

Invoking screen via upstart is indeed somewhat tricky. The first problem regarding non-existing /var/run/screen can be easily solved though.

On Ubuntu 10.10 to 13.10 there is an upstart task which is responsible for cleaning up and (re-)creating /var/run/screen on bootup so you need to make sure your upstart script will run after it:

start on stopped screen-cleanup

On Ubuntu 10.04 and earlier as well as Ubuntu 14.04 and later that code is in the init script /etc/init.d/screen-cleanup which means upstart jobs can refer to it as the result of rc:

start on stopped rc

However, screen will probably still complain about /var/run/screen permissions. This can be workarounded by invoking screen via setsid:

exec setsid screen -Dm /some/command

Your screen session will fork once, so you need to add the "expect fork" stanza to make sure upstart follows the correct pid.

Here's a complete example script (needs at least Ubuntu 12.04):

# screen startup script
# requires upstart v1.4 or newer

description "running top in screen session"

start on ( local-filesystems
           and stopped rc )
stop on runlevel [!2345]

respawn

setuid test
setgid test

# "setsid screen -Dm" only forks once
expect fork

# use setsid to avoid screen complaining about /var/run/screen permissions.
exec setsid screen -Dm -S mytopsession /usr/bin/top
Related Question