6

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"
Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
  • How are you sure that the screen session isn't started as root? Please start a program in screen that way that cannot fail (like sleep 1000). – Hauke Laging Jun 23 '14 at 22:44
  • No luck using the command screen -dmS test sleep 1000. No screen sockets running in both the root account and my own upon reboot. However, no errors in the log. – Riley Tench Jun 23 '14 at 23:13
  • If I execute screen -dmS test sleep 1000 via crontab then there is the expected screen session. Trying with cron may be easier for you anyway. – Hauke Laging Jun 24 '14 at 01:30

3 Answers3

4

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
  • Doesn't the last line of the example script start an useless instance of top ? Is it necessary to launch it even if the program itself won't be used ? – rautamiekka Mar 12 '16 at 22:26
  • 1
    You need to replace top with whatever application you want to run inside your screen session of course. I only used top for the example because it's well-known and available on most systems. – Guido Nickels Mar 15 '16 at 07:22
-1

Create /var/run/screen as root with the appropriate permissions before you make the screen call.

Hauke Laging
  • 90,279
-1

Problem solved, I switched from using upstart to using cron. It was much more simple and everything works great now.

For anyone reading this that might be curious as to how I did it, I made a simple shell script:

#!/bin/bash
java -Xms256M -Xmx256M -jar /home/ridog/BungeeCord/BungeeCord.jar

And set it to run on startup with a new line in crontab -e:

@reboot screen -dmS BungeeCord sh /home/ridog/BungeeCord/run.sh

Thanks for your help!

  • this does not work if your drive is mounted after the system boots. – chovy Nov 02 '14 at 04:51
  • https://bugs.launchpad.net/ubuntu/+source/screen/+bug/1380080 is a regression report. It has stopped working for me on 14.04 – Lord Loh. Aug 06 '15 at 09:24