Screen session doesn't close after script has run

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












0















I write a bash file, which contains screen commands. This bash file is then called from a python script using subprocess. All works perfectly fine. However, the screen sessions don't close after the script has (successfully) run and stay open. This is unwanted behaviour.



Here is an example of the bash file:



#!/bin/bash
VIRTUAL_ENV_DISABLE_PROMPT=true
source generic_path/bin/activate
cd generic_directory
screen -dmS session_1
screen -S session_1 -X stuff "Rscript script_1.Rn"


I suspect, that I have to change something in the screen commands, right? Could you advise, what I have to change, so the screen session automatically close, after the script has run.










share|improve this question
























  • Can you explain why you need to run this script in a screen session?

    – Panki
    Jan 11 at 8:38











  • Sure. We are trying to schedule scripts. This bash file is actually created automatically. In order to be able to run multiple scripts (possibly) simultaneous, we thought using screen would be the best way to go. Also we want to be able to log in/out to our server without aborting the scripts. If you have a better solution, I'm glad to hear it. Thanks Panki.

    – Joko
    Jan 11 at 8:45











  • I don't know if this fits your use case, but can't you simply run the script in the background (by appending &)?

    – Panki
    Jan 11 at 9:13















0















I write a bash file, which contains screen commands. This bash file is then called from a python script using subprocess. All works perfectly fine. However, the screen sessions don't close after the script has (successfully) run and stay open. This is unwanted behaviour.



Here is an example of the bash file:



#!/bin/bash
VIRTUAL_ENV_DISABLE_PROMPT=true
source generic_path/bin/activate
cd generic_directory
screen -dmS session_1
screen -S session_1 -X stuff "Rscript script_1.Rn"


I suspect, that I have to change something in the screen commands, right? Could you advise, what I have to change, so the screen session automatically close, after the script has run.










share|improve this question
























  • Can you explain why you need to run this script in a screen session?

    – Panki
    Jan 11 at 8:38











  • Sure. We are trying to schedule scripts. This bash file is actually created automatically. In order to be able to run multiple scripts (possibly) simultaneous, we thought using screen would be the best way to go. Also we want to be able to log in/out to our server without aborting the scripts. If you have a better solution, I'm glad to hear it. Thanks Panki.

    – Joko
    Jan 11 at 8:45











  • I don't know if this fits your use case, but can't you simply run the script in the background (by appending &)?

    – Panki
    Jan 11 at 9:13













0












0








0








I write a bash file, which contains screen commands. This bash file is then called from a python script using subprocess. All works perfectly fine. However, the screen sessions don't close after the script has (successfully) run and stay open. This is unwanted behaviour.



Here is an example of the bash file:



#!/bin/bash
VIRTUAL_ENV_DISABLE_PROMPT=true
source generic_path/bin/activate
cd generic_directory
screen -dmS session_1
screen -S session_1 -X stuff "Rscript script_1.Rn"


I suspect, that I have to change something in the screen commands, right? Could you advise, what I have to change, so the screen session automatically close, after the script has run.










share|improve this question
















I write a bash file, which contains screen commands. This bash file is then called from a python script using subprocess. All works perfectly fine. However, the screen sessions don't close after the script has (successfully) run and stay open. This is unwanted behaviour.



Here is an example of the bash file:



#!/bin/bash
VIRTUAL_ENV_DISABLE_PROMPT=true
source generic_path/bin/activate
cd generic_directory
screen -dmS session_1
screen -S session_1 -X stuff "Rscript script_1.Rn"


I suspect, that I have to change something in the screen commands, right? Could you advise, what I have to change, so the screen session automatically close, after the script has run.







gnu-screen






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 12 at 1:13









Rui F Ribeiro

39.6k1479132




39.6k1479132










asked Jan 11 at 8:35









JokoJoko

31




31












  • Can you explain why you need to run this script in a screen session?

    – Panki
    Jan 11 at 8:38











  • Sure. We are trying to schedule scripts. This bash file is actually created automatically. In order to be able to run multiple scripts (possibly) simultaneous, we thought using screen would be the best way to go. Also we want to be able to log in/out to our server without aborting the scripts. If you have a better solution, I'm glad to hear it. Thanks Panki.

    – Joko
    Jan 11 at 8:45











  • I don't know if this fits your use case, but can't you simply run the script in the background (by appending &)?

    – Panki
    Jan 11 at 9:13

















  • Can you explain why you need to run this script in a screen session?

    – Panki
    Jan 11 at 8:38











  • Sure. We are trying to schedule scripts. This bash file is actually created automatically. In order to be able to run multiple scripts (possibly) simultaneous, we thought using screen would be the best way to go. Also we want to be able to log in/out to our server without aborting the scripts. If you have a better solution, I'm glad to hear it. Thanks Panki.

    – Joko
    Jan 11 at 8:45











  • I don't know if this fits your use case, but can't you simply run the script in the background (by appending &)?

    – Panki
    Jan 11 at 9:13
















Can you explain why you need to run this script in a screen session?

– Panki
Jan 11 at 8:38





Can you explain why you need to run this script in a screen session?

– Panki
Jan 11 at 8:38













Sure. We are trying to schedule scripts. This bash file is actually created automatically. In order to be able to run multiple scripts (possibly) simultaneous, we thought using screen would be the best way to go. Also we want to be able to log in/out to our server without aborting the scripts. If you have a better solution, I'm glad to hear it. Thanks Panki.

– Joko
Jan 11 at 8:45





Sure. We are trying to schedule scripts. This bash file is actually created automatically. In order to be able to run multiple scripts (possibly) simultaneous, we thought using screen would be the best way to go. Also we want to be able to log in/out to our server without aborting the scripts. If you have a better solution, I'm glad to hear it. Thanks Panki.

– Joko
Jan 11 at 8:45













I don't know if this fits your use case, but can't you simply run the script in the background (by appending &)?

– Panki
Jan 11 at 9:13





I don't know if this fits your use case, but can't you simply run the script in the background (by appending &)?

– Panki
Jan 11 at 9:13










2 Answers
2






active

oldest

votes


















0














When you execute something like



screen -dmS session_1
screen -S session_1 -X stuff "Rscript script_1.Rn"


you send to the terminal (in previously created screen) the sequence of characters Rscript script_1.Rn. This way the shell will execute the command and, then, wait for the next command.



You need to execute something like Rscript script_1.R && exit to ask to the shell to exit when your command completes successfully (please, adapt to your shell syntax).



screen -dmS session_1
screen -S session_1 -X stuff "Rscript script_1.R && exitn"


In any case, there is no need to create a window and then "remote control" it. You can simply ask to screen to execute a command for you.



screen -dm Rscript script_1.R


Last but not least, to use screen to put in background a script is one of the weirdest idea I ever heard. Please use cron or at or & instead (according to your needs).



If "Rscript" is not only a random example, please read Run R script from command line.



R CMD BATCH script_1.R &





share|improve this answer























  • This is amazing. Thanks a lot for your comprehensive answer. The part with "&& exit" solved my problem just wonderfully. Regarding the other parts of your answer, there are several reasons why we do it this way. I won't go into detail here, but it has to do with debugging abilities. And a strange behaviour with virtual environments and packrat causes the simple case of "simply ask to screen to execute a command for you" to fail. As of now, we have no clue why.

    – Joko
    Jan 11 at 14:04



















0














If you want to "schedule" a script, I think "Crontab" might be a good solution.
Another solution is to run the script in background mode (with &).






share|improve this answer






















    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%2f493891%2fscreen-session-doesnt-close-after-script-has-run%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









    0














    When you execute something like



    screen -dmS session_1
    screen -S session_1 -X stuff "Rscript script_1.Rn"


    you send to the terminal (in previously created screen) the sequence of characters Rscript script_1.Rn. This way the shell will execute the command and, then, wait for the next command.



    You need to execute something like Rscript script_1.R && exit to ask to the shell to exit when your command completes successfully (please, adapt to your shell syntax).



    screen -dmS session_1
    screen -S session_1 -X stuff "Rscript script_1.R && exitn"


    In any case, there is no need to create a window and then "remote control" it. You can simply ask to screen to execute a command for you.



    screen -dm Rscript script_1.R


    Last but not least, to use screen to put in background a script is one of the weirdest idea I ever heard. Please use cron or at or & instead (according to your needs).



    If "Rscript" is not only a random example, please read Run R script from command line.



    R CMD BATCH script_1.R &





    share|improve this answer























    • This is amazing. Thanks a lot for your comprehensive answer. The part with "&& exit" solved my problem just wonderfully. Regarding the other parts of your answer, there are several reasons why we do it this way. I won't go into detail here, but it has to do with debugging abilities. And a strange behaviour with virtual environments and packrat causes the simple case of "simply ask to screen to execute a command for you" to fail. As of now, we have no clue why.

      – Joko
      Jan 11 at 14:04
















    0














    When you execute something like



    screen -dmS session_1
    screen -S session_1 -X stuff "Rscript script_1.Rn"


    you send to the terminal (in previously created screen) the sequence of characters Rscript script_1.Rn. This way the shell will execute the command and, then, wait for the next command.



    You need to execute something like Rscript script_1.R && exit to ask to the shell to exit when your command completes successfully (please, adapt to your shell syntax).



    screen -dmS session_1
    screen -S session_1 -X stuff "Rscript script_1.R && exitn"


    In any case, there is no need to create a window and then "remote control" it. You can simply ask to screen to execute a command for you.



    screen -dm Rscript script_1.R


    Last but not least, to use screen to put in background a script is one of the weirdest idea I ever heard. Please use cron or at or & instead (according to your needs).



    If "Rscript" is not only a random example, please read Run R script from command line.



    R CMD BATCH script_1.R &





    share|improve this answer























    • This is amazing. Thanks a lot for your comprehensive answer. The part with "&& exit" solved my problem just wonderfully. Regarding the other parts of your answer, there are several reasons why we do it this way. I won't go into detail here, but it has to do with debugging abilities. And a strange behaviour with virtual environments and packrat causes the simple case of "simply ask to screen to execute a command for you" to fail. As of now, we have no clue why.

      – Joko
      Jan 11 at 14:04














    0












    0








    0







    When you execute something like



    screen -dmS session_1
    screen -S session_1 -X stuff "Rscript script_1.Rn"


    you send to the terminal (in previously created screen) the sequence of characters Rscript script_1.Rn. This way the shell will execute the command and, then, wait for the next command.



    You need to execute something like Rscript script_1.R && exit to ask to the shell to exit when your command completes successfully (please, adapt to your shell syntax).



    screen -dmS session_1
    screen -S session_1 -X stuff "Rscript script_1.R && exitn"


    In any case, there is no need to create a window and then "remote control" it. You can simply ask to screen to execute a command for you.



    screen -dm Rscript script_1.R


    Last but not least, to use screen to put in background a script is one of the weirdest idea I ever heard. Please use cron or at or & instead (according to your needs).



    If "Rscript" is not only a random example, please read Run R script from command line.



    R CMD BATCH script_1.R &





    share|improve this answer













    When you execute something like



    screen -dmS session_1
    screen -S session_1 -X stuff "Rscript script_1.Rn"


    you send to the terminal (in previously created screen) the sequence of characters Rscript script_1.Rn. This way the shell will execute the command and, then, wait for the next command.



    You need to execute something like Rscript script_1.R && exit to ask to the shell to exit when your command completes successfully (please, adapt to your shell syntax).



    screen -dmS session_1
    screen -S session_1 -X stuff "Rscript script_1.R && exitn"


    In any case, there is no need to create a window and then "remote control" it. You can simply ask to screen to execute a command for you.



    screen -dm Rscript script_1.R


    Last but not least, to use screen to put in background a script is one of the weirdest idea I ever heard. Please use cron or at or & instead (according to your needs).



    If "Rscript" is not only a random example, please read Run R script from command line.



    R CMD BATCH script_1.R &






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 11 at 11:07









    andcozandcoz

    12.6k33139




    12.6k33139












    • This is amazing. Thanks a lot for your comprehensive answer. The part with "&& exit" solved my problem just wonderfully. Regarding the other parts of your answer, there are several reasons why we do it this way. I won't go into detail here, but it has to do with debugging abilities. And a strange behaviour with virtual environments and packrat causes the simple case of "simply ask to screen to execute a command for you" to fail. As of now, we have no clue why.

      – Joko
      Jan 11 at 14:04


















    • This is amazing. Thanks a lot for your comprehensive answer. The part with "&& exit" solved my problem just wonderfully. Regarding the other parts of your answer, there are several reasons why we do it this way. I won't go into detail here, but it has to do with debugging abilities. And a strange behaviour with virtual environments and packrat causes the simple case of "simply ask to screen to execute a command for you" to fail. As of now, we have no clue why.

      – Joko
      Jan 11 at 14:04

















    This is amazing. Thanks a lot for your comprehensive answer. The part with "&& exit" solved my problem just wonderfully. Regarding the other parts of your answer, there are several reasons why we do it this way. I won't go into detail here, but it has to do with debugging abilities. And a strange behaviour with virtual environments and packrat causes the simple case of "simply ask to screen to execute a command for you" to fail. As of now, we have no clue why.

    – Joko
    Jan 11 at 14:04






    This is amazing. Thanks a lot for your comprehensive answer. The part with "&& exit" solved my problem just wonderfully. Regarding the other parts of your answer, there are several reasons why we do it this way. I won't go into detail here, but it has to do with debugging abilities. And a strange behaviour with virtual environments and packrat causes the simple case of "simply ask to screen to execute a command for you" to fail. As of now, we have no clue why.

    – Joko
    Jan 11 at 14:04














    0














    If you want to "schedule" a script, I think "Crontab" might be a good solution.
    Another solution is to run the script in background mode (with &).






    share|improve this answer



























      0














      If you want to "schedule" a script, I think "Crontab" might be a good solution.
      Another solution is to run the script in background mode (with &).






      share|improve this answer

























        0












        0








        0







        If you want to "schedule" a script, I think "Crontab" might be a good solution.
        Another solution is to run the script in background mode (with &).






        share|improve this answer













        If you want to "schedule" a script, I think "Crontab" might be a good solution.
        Another solution is to run the script in background mode (with &).







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 11 at 10:13









        SyllaSylla

        753




        753



























            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%2f493891%2fscreen-session-doesnt-close-after-script-has-run%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

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?