Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find out if someone is actively using a Linux computer in Python or Java?

Tags:

java

python

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?

like image 574
Thomas Johnson Avatar asked May 10 '26 23:05

Thomas Johnson


2 Answers

If the aim is to avoid bothering someLocalUser, you could consider running your job on a lower priority. See the documentation for nice.

like image 63
Eero Aaltonen Avatar answered May 13 '26 12:05

Eero Aaltonen


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.

like image 21
A.H. Avatar answered May 13 '26 12:05

A.H.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!