-1

How do I set up a systemd service for a Java application that uses screen, and still be able to reattach to that screen? I have searched high and low for an answer, and tried several different things, but I think systemd is throwing a wrench into the whole thing.

I have a Java application set to run as a service in systemd. It uses Screen, because I need to be able to interactively work with it after it's running. It's on an Ubuntu 16.04 server without a GUI. I manage it remotely via SSH. The Java app runs under its own user, which does not have sudo rights. I used the sudoers file to grant that user permission to start and stop the particular service associated with the Java app. The problem comes when I need to reattach to the screen that the app is running in.

Originally, this is what I was using in my service unit for systemd to start the app:

ExecStart=/bin/sh -c '/usr/bin/screen -DmS screen-name /usr/bin/java -server -someotheroptions -jar /path/to/jar/file.jar'

When I SSH in with a user specific for SSH connections, sudo to the specific user that the app runs as, and try to reattach to the screen that the app is running in, I kept getting this error:

Cannot open your terminal '/dev/pts/1' - please check.

The error, I'm sure you know, can have a different number, but most often I get a '1' in it. After finding some information online, I couldn't understand how I wasn't starting a shell with /bin/sh before, but changed the command to this to see if it made any difference:

ExecStart=/usr/bin/script -q -c "/usr/bin/screen -DmS screen-name /usr/bin/java -server -someotheroptions -jar /path/to/jar/file.jar" /dev/null

Both commands work just fine to launch my app. I can start and stop the service at will, and reboot and have the service start without problem. It all works. Except for reattaching to the screen across SSH.

Does anyone have any idea what I can do to get this working so that when I SSH in, sudo to the app's user, and try to reattach, it works?

Alex P.
  • 30,437
  • 17
  • 118
  • 169
Tyrelius
  • 31
  • 10
  • Just to add some detail, I can sudo to the app's user and run `screen -list` and see the screen I want. So I know that the screen is initializing properly. – Tyrelius Sep 05 '17 at 22:31
  • I also can interact (send commands to) with the screen non-interactively, from crontab, using `screen -S screen-name -X stuff "commandhere^M"` The only problem is reattaching to the screen so that I can see output from application events and commands I input. – Tyrelius Sep 05 '17 at 22:36

1 Answers1

0

I think I finally came to understand the answer. When people say to run script /dev/null before running screen, they don't mean to run it before you start the screen session through systemd. They mean run it when you ssh in and sudo to the app's user. Once you've run it, while you're in the script session, you can then use screen to reattach to the screen your app is running in.

So, to clarify for others who may see this after seeing commonly refered to answers, such as this: https://serverfault.com/questions/116775/sudo-as-different-user-and-running-screen/116830

  • You set up systemd to launch your screen session detached.
  • When you want to connect, you SSH into your box.
  • You then sudo to the user that is running that screen session.
  • Once you're sudo'd to that user, you run script /dev/null
  • Now, you can use screen -r screen-name to reattach to that screen.
Tyrelius
  • 31
  • 10