Sinan and nandhp solutions will work for this task. threads and select are powerful tools in the arsenal of Perl programmers, but I would be reluctant to offer them to someone "the first ever program." Therefore, I propose a different approach.
To simplify the formulation of this problem, we want to do something (run the command to kill processes on the remote server) when something else happens (the screen saver has been active for 15 minutes).
use strict; use warnings; initialize_program(); until (something_happens()) { sleep 60; } do_something(); exit;
The do_something part is simple:
sub do_something { print "*** killing all jla processes on anvil...\n"; $result = `ssh anvil pkill -u jla`; print "*** should all be dead\n"; print $result; }
For the something_happens part of the program, I suggest sending dbus-monitor output to a file in the background and reading from the file whenever you want to know the status of the screen saver. The dbus-monitor program produces output rather slowly, and reading from a Perl file descriptor will tend to block (unless you recognize and use select ).
I will correct the dbus-monitor command a bit. This command will print a time stamp every time the screen saver state changes:
my $command = q[dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver',member='ActiveChanged'" | perl -ne 'print time," $_" if /boolean/'];
and we will run our program by doing:
sub initialize_program {
Now, to find out if the screen saver is active and how long it has been stored, we analyze /tmp/screensavermonitor from time to time.
sub something_happens { open (my $fh, '<', '/tmp/screensavermonitor') or return do { warn $!;0 }; my @output = <$fh>; close $fh;