I have an old method of killing idle mosh-server processes, which doesn't work on my new VPS.
Here's the old method, which I run in root's crontab to clean up mosh processes that have been idle for 10+ days:
for tty in `w -sf | grep -P '\d\ddays mosh-server' | cut -c 10-15`; do kill -9 `ps -o pid= -t $tty`; done
The problem is that on the new VPS, these processes don't have TTYs, so I can't match by TTY and kill mosh that way. Here's what I've got so far (BTW, the new VPS is running kernel 3.2 on Debian wheezy, the old one was the prior Debian edition).
for loginday in `w | grep -P '\d\ddays.* mosh-server' | cut -c 34-40 | sed 's/^\([0-9]*\)\([a-zA-Z]*\).*$/\2 \1/'`; do echo $loginday; done
I'm trying to get the login day from 'w' and correlate it later with info from 'ps':
ps -C mosh-server -o bsdstart
Thus, I could correlate any idle mosh-server sessions started on the same day as login (which is probably 99.9% of them in the wild) and kill them THAT way, using 'ps' to get the pid.
However, 'w' outputs login day thusly:
Which I cut to simply "03Oct".
And 'ps' outputs thusly:
You can see in the above sed command, which is the part that isn't working, I'm trying to take in "03Oct" and output "Oct 03" (I'll deal with the zeroes when I get this working) and I cannot for the life of me get sed to work inside the for loop when it seems to work when I test it with 'echo':
echo 03Oct | sed 's/\([0-9]*\)\([a-zA-Z]*\).*/\2 \1/'
What's mangling the sed command once I paste it into the for loop?
Better yet, is there a better or less hacky way to do this? Given that detached idle mosh-server processes don't have TTYs on Linux 3.2 that I can easily associate? Edit: Also, on systems with libutempter installed, mosh-server will write information to utmp that I suspect would be useful, but I don't know how to get at it.