Remotely starting a screen session through ssh and closing the ssh session immediately
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
ssh gnu-screen
add a comment |
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
ssh gnu-screen
1
Remove the-t
from yourssh
,screen
handles its own tty, and that's just getting in the way here
– Eric Renouf
Feb 29 '16 at 18:40
add a comment |
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
ssh gnu-screen
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
ssh gnu-screen
ssh gnu-screen
edited Feb 29 '16 at 18:37
Shelby. S
asked Feb 29 '16 at 18:11
Shelby. SShelby. S
12716
12716
1
Remove the-t
from yourssh
,screen
handles its own tty, and that's just getting in the way here
– Eric Renouf
Feb 29 '16 at 18:40
add a comment |
1
Remove the-t
from yourssh
,screen
handles its own tty, and that's just getting in the way here
– Eric Renouf
Feb 29 '16 at 18:40
1
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
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
add a comment |
2 Answers
2
active
oldest
votes
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
add a comment |
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.
New contributor
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f266571%2fremotely-starting-a-screen-session-through-ssh-and-closing-the-ssh-session-immed%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
add a comment |
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
add a comment |
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
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
edited Feb 29 '16 at 18:34
answered Feb 29 '16 at 18:23
Eric RenoufEric Renouf
13.5k43050
13.5k43050
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
add a comment |
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
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
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
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
I have updated the question with my attempt while using -dm :(
– Shelby. S
Feb 29 '16 at 18:35
add a comment |
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.
New contributor
add a comment |
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.
New contributor
add a comment |
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.
New contributor
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.
New contributor
New contributor
answered 17 mins ago
poleguypoleguy
1
1
New contributor
New contributor
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f266571%2fremotely-starting-a-screen-session-through-ssh-and-closing-the-ssh-session-immed%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Remove the
-t
from yourssh
,screen
handles its own tty, and that's just getting in the way here– Eric Renouf
Feb 29 '16 at 18:40