title: Ubuntu, Screen, SSH, and Systemd
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>Originally, this is what I was using in my service unit for systemd to start the app:</p>
  
  <pre><code>ExecStart=/bin/sh -c '/usr/bin/screen -DmS screen-name /usr/bin/java -server -someotheroptions -jar /path/to/jar/file.jar'
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>Cannot open your terminal '/dev/pts/1' - please check.
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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.</p>
  
  <p>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?</p>