How do I reattach and close multiple screen sessions from a list
-
18-09-2019 - |
Question
I am an Ubuntu 9.04 user. Given a list of screen sessions such as the following:
9076.pts-30.moe (09/27/2009 11:30:08 PM) (Attached)
8778.pts-24.moe (09/27/2009 11:29:46 PM) (Detached)
8674.pts-0.moe (09/27/2009 11:29:25 PM) (Attached)
22649.pts-28.moe (09/27/2009 11:51:46 AM) (Detached)
22543.pts-24.moe (09/27/2009 11:50:56 AM) (Detached)
22228.pts-16.moe (09/27/2009 11:49:59 AM) (Detached)
How would I close out multiple screens based on a time criterion? For instance, all screens initiated before 12:00 PM. I normally would type:
screen -dr 22649.pts-28.moe
exit
...
and close each one out manually, but this is tedious. Thanks in advance.
Solution
You can send commands to a screen using the -X parameter. So you can close a screen from the command line by saying "screen -S 22649.pts-28.moe -X quit".
To select the screens by time, I guess you have to run a script to compare the times.
Personally, I would use os.popen() and time.strptime() in python.
OTHER TIPS
Here's how I kill all detached screens:
screen -ls | grep Detached | awk -F" " '{print $1}' | xargs -I{} screen -X -S {} kill
if you want to forcibly kill all screens, just change grep Detached
above to grep tached
. Works for me all the tine, at the very least!
for time criterion, you can start with this:
screen -ls | grep Detached | awk -F " " '{print $2,$3,$4","$1}'
this prints something like:
(01/03/2012 02:10:42 AM),4504.test2
(01/03/2012 02:10:12 AM),4445.test1
(01/03/2012 02:02:58 AM),4333.test0
where the first group is the timestamp and separated by a comma, is the PID.name.
so you could use/pipe to awk
(or awk -F"," '{print $1}'
to exactly extract the timestamps only, in parentheses) again to parse the time inside the parentheses.. remember, screen -ls
always lists the earliest screen last!
I have not figured out how to do time criterion filtering myself, i'll edit this in the future if i could.. good luck buddy!
The number before the dot (22649
in your example) is the PID of the screen process. Just kill it (kill 22649
)