Remotely starting a screen session through ssh and closing the ssh session immediately












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









share|improve this question




















  • 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















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









share|improve this question




















  • 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












3








3


1






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









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 your ssh, screen handles its own tty, and that's just getting in the way here

    – Eric Renouf
    Feb 29 '16 at 18:40














  • 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








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










2 Answers
2






active

oldest

votes


















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






share|improve this answer


























  • 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.






share|improve this answer








New contributor




poleguy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    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






    share|improve this answer


























    • 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


















    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






    share|improve this answer


























    • 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
















    3












    3








    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






    share|improve this answer















    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







    share|improve this answer














    share|improve this answer



    share|improve this answer








    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





















    • 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















    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.






    share|improve this answer








    New contributor




    poleguy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.

























      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.






      share|improve this answer








      New contributor




      poleguy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.























        0












        0








        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.






        share|improve this answer








        New contributor




        poleguy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.










        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.







        share|improve this answer








        New contributor




        poleguy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer






        New contributor




        poleguy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered 17 mins ago









        poleguypoleguy

        1




        1




        New contributor




        poleguy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        poleguy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        poleguy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Loup dans la culture

            How to solve the problem of ntp “Unable to contact time server” from KDE?

            ASUS Zenbook UX433/UX333 — Configure Touchpad-embedded numpad on Linux