How to cleanup all unused screens?

286 Views Asked by At

I would like to remove from my systems all screens I've opened to do my tasks. But I need to get rid of the screens that aren't doing anything. I don't want to kill a screen that something happens inside.

As an example, I run a long command in one screen called screen3 while screens named screen1 and screen2 are previous screens, no jobs are running inside them. The goal is to have a command or script (in crontab) that automatically clean user1 and user2.

1

There are 1 best solutions below

0
Luca Moscato On

First attempt - detection only

screen -wipe > /dev/null

screen_pids=$(screen -ls | grep Detached | awk '/\.*\t/ {print strtonum($1)}')

for scr_pid in $screen_pids
do
   bash_child=$(ps -el | grep $scr_pid | grep bash | awk '{print $4}')
   if [ $(pgrep -P $bash_child | wc -l) -eq "0" ]; then
      # in that case screen $scr_pid could be killed
      echo "The screen $scr_pid doesn't have any child process and should be terminated"
   fi
done