I want to write a program that ssh's into remote boxes and runs jobs there if the remote computer is not actively being used. I'll be logging in as clusterJobRunner@remoteBox, and the other user will be logged in as someLocalUser@remoteBox.
Is there a way to see if a remote user is actively using the box using either Python or Java?
If the aim is to avoid bothering someLocalUser, you could consider running your job on a lower priority. See the documentation for nice.
I assume that the "actively used" part is the tricky part.
If it is sufficient to check whether or not another user is logged in, you can use the commands w and who and perhaps last and lastlog. All these commands several parameter which you can lookup in the manuals.
From Java / Python you can execute these commands and parse their output.
On the other hand: The tools w and who use the file utmp to get their information. A quick Google turned up nothing for Java but for Python I've found the library pyutmp which you can use to read the utmp file directly without parsing the command output.
Whether the user logged in and went to lunch (possibly locking the screen) is a completely other story.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With