3

My goal is to remote to a server through ssh, start a screen, start a script, let the script run, and exit the ssh session while keeping the screen running its own python script. This is what I have:

ssh -t myuser@hostname screen python somepath.py -s 'potato'

The problem with this is, after I run it, I have to manually ctrl + a + d, and exit out of the ssh session myself. Is there a way to do it all in one go without needing human interaction?

EDIT: I have tried the suggested method of using -dm

This is what I'm testing to make it easier to see:

ssh -t user@host screen "top"

remotely I see this:

user      2557  0.0  0.2  27192  1468 ?        Ss   13:35   0:00 SCREEN top
user      2562  0.0  0.1  11740   932 pts/0    S+   13:35   0:00 grep --color=auto SCREEN

but if I do:

ssh -t user@host screen -dm "top"

I immediately get a Connection to host closed. And nothing in my grep

ps aux | grep SCREEN
user      2614  0.0  0.1  11740   932 pts/0    S+   13:36   0:00 grep --color=auto SCREEN
  • 1
    Remove the -t from your ssh, screen handles its own tty, and that's just getting in the way here – Eric Renouf Feb 29 '16 at 18:40
3

You can use -d -m to your screen session to do it like:

ssh myuser@hostname screen -d -m "python somepath.py -s 'potato'"

That will create a new screen session, run your command in it and automatically detach you from it.

That option is documented as

-d -m
Start screen in detached mode. This creates a new session but doesn't attach to it. This is useful for system startup scripts.

on the GNU documentation page for screen

  • Hey Eric, thanks for the response, I tried doing this and I see that the connection to the host is closed immediately, which is what I need, I ssh to the server to see if the script has started, and I don't see anything after running a ps aux | grep python. I also do not get the email that the script is supposed to send during initialization. I'm not sure what's wrong. – Shelby. S Feb 29 '16 at 18:31
  • Try quoting your command, that worked in an sample for myself. I'll update my answer to show it that way – Eric Renouf Feb 29 '16 at 18:33
  • I have updated the question with my attempt while using -dm :( – Shelby. S Feb 29 '16 at 18:35
0

I find the -d -m option works but not with the quotes. I'd need to do:

ssh myuser@hostname screen -d -m python somepath.py -s 'potato'

I don't know why the accepted answer has quotes around the command as they aren't called for in the GNU documentation and they break it on my system (Centos 7).

For what it's worth: I noticed the screen doesn't stay alive when run via ssh under Jenkins. It's alive while the parent ssh connection and sh script are running, but dies when the parent closes, so there may be cases where screen can't solve the problem.

I even tried using this trick:

https://stackoverflow.com/questions/39471261/must-be-connected-to-a-terminal-error-with-screen-x-command-on-a-linux-contai

adding

script /dev/null

before the screen call, but it didn't fix the problem. I don't know what magic jenkins is doing to make everything die, but it's effective.

It might be killed after the fact by the jenkins process killer which could be disabled by adding:

export BUILD_ID=dontKillMe

https://serverfault.com/questions/502593/starting-a-forever-process-in-a-jenkins-build-step

In my case I had luck keeping a long running process (ssh tunnel) going by adding dontKillMe, although I moved away from using screen as it was unnecessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.