Is it possible to know if a script executed inside a screen session finished or not? The script can be seen by typing jobs and I would like to terminate the screen session when the script is finished. How can I do so or can I have the screen terminate itself after the script successfully executes? It is not convenient to do it manually since I run 72 screen sessions at once. It would be really nice to somehow check the status of the jobs inside the screen sessions automatically and print them to stdout and have the screen sessions terminate when the jobs running within are finished.

  • 1
    Can't you just execute the jobs in the background? – Jan Aug 31 '14 at 22:10
  • 3
    "I would like to terminate the screen session when the script is finished." some_script && exit – John1024 Aug 31 '14 at 22:20
  • @John1024 Seems plausible can I stuff it into a detached screen? – Vesnog Sep 1 '14 at 9:26
  • @Vesnog Yes. The exit will cause the window that it is in to close. If that is the only window in the screen session, then the screen session will close. If, as shown, && is used, the window will remain open for your inspection if the script fails. If you want it to close regardless of whether the script succeeded or failed, then replace && with ;. – John1024 Sep 1 '14 at 18:20

You just chain the commands 1. your_script and 2. kill this screen session

Let foo.sh be your script. The command to kill a screen session is kill. You issue commands to a screen session with screen -X, making screen -X kill the shell/bash command to kill the screen session you are currently in. You would

screen -S the_session_for_my_job

to create the screen session and then you would

/path/to/foo.sh; screen -X kill

This will kill the screen session, independent from the number of windows and will do so after foo.sh has finished.

A more complicated way is to watch process ids and issue the kill command when a certain PID disappears.

Let 12345 the PID of the process you want to watch. Then you would

while ps a | grep ^12345;do sleep 2; done; screen -X kill

The while loop watches the process list for a process with the PID 12345. If such a process exists, it will sleep 2 seconds before checking again. If no process with this PID exists, the while loop terminates. Then the kill command is issued.

  • Okay thanks for the answer can I use the second statement with the stuff command? Actually I send the commands to a detached screen with a zsh script. – Vesnog Sep 1 '14 at 9:10
  • Yes, I saw your other post. It should work just as well as any other string you can stuff. – Bananguin Sep 1 '14 at 18:26

I use email with the following:

command | mail-s job_output to@email.com; exit

Or if it's already running you can type (or copy-paste if there is lots of output) commands to execute after completion:

echo $STY done | mail -s job_done to@email.com; exit
  • Thanks this is a nice option too, but how can it send an email to me. – Vesnog Sep 1 '14 at 9:47

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.