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.
How to know when a job in screen finishes?
gnu screen;job control
You just chain the commands 1. your_script and 2. kill this screen sessionLet 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 wouldscreen -S the_session_for_my_jobto create the screen session and then you would/path/to/foo.sh; screen -X killThis 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 wouldwhile ps a | grep ^12345;do sleep 2; done; screen -X killThe 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.