I frequently use screen when I log into the interactive nodes to a supercomputer that I have access to -- and I often run things and move on. There are about 20 separate nodes that I can log into; and if I check any one of them I'll have something like 4 detached sessions. Each of those sessions will have maybe 5 screen sessions within that.
Is there a quick way to cycle through all of these and close them down if they are not running any processes? My current process is to screen -ls
and then screen -r ####
then type exit
until I'm back to the base screen.
EDIT
Here's a script that I based on Thor's answer:
for screen_pid in $(screen -ls | sed -nr 's/^\t+([0-9]+).*/\1/p');
do
for shell_tty in $(ps h --ppid $screen_pid -o tty);
do
number_of_processes=$(ps a -o tty | grep $shell_tty | wc -l)
if (( number_of_processes > 1 )); then
echo number $number_of_processes
else
# screen -S $screen_pid -X kill
screen -S $screen_pid -X quit
fi
done
done
When I run it with kill, nothing seems to happen to the sessions. When I run with quit, it appears to kill the whole session and anything running inside (without checking if there are processes running). Tweaks from here?
Ctrl-d
to exit would speed you up.