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:

source generic_path/bin/activate
cd generic_directory
screen -dmS session_1
screen -S session_1 -X stuff "Rscript script_1.R\n"

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.

  • 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

When you execute something like

screen -dmS session_1
screen -S session_1 -X stuff "Rscript script_1.R\n"

you send to the terminal (in previously created screen) the sequence of characters Rscript script_1.R\n. 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 && exit\n"

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

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 &).

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.