I'm trying to note down workstation/System screen lock of each employee working in ubuntu OS. I needed to store these record in a DataBase. using JAVA. I have searched all over and got on idea for UBUNTU; But got idea how to do the same in windows OS.
2 Answers
From here:
gnome-screensaver-command -q | grep "is active"
Use the Runtime class to execute that command and read back the result.
EDIT: use grep -q
Here an example how to use it:
public class ScreenSaver {
/*
* Pipes are a shell feature, so you have to open a shell first.
*
* You could use process.getInputStream() to read the output and parse it.
*
* For productive use i would prefer using the Inputstream.
*/
private static final String COMMAND = "gnome-screensaver-command -q | grep -q 'is active'";
private static final String[] OPEN_SHELL = { "/bin/sh", "-c", COMMAND };
private static final int EXPECTED_EXIT_CODE = 0;
public static boolean isScreenSaverActive() {
final Runtime runtime = Runtime.getRuntime();
Process process = null;
try {
/*
* open a shell and execute the command in that shell
*/
process = runtime.exec(OPEN_SHELL);
/*
* wait for the command to finish
*/
return process.waitFor() == EXPECTED_EXIT_CODE;
} catch(final IOException e) {
e.printStackTrace();
} catch(final InterruptedException e) {
e.printStackTrace();
}
return false;
}
public static void main(final String[] args) {
System.out.println("Screensaver is active: " + isScreenSaverActive());
}
}
EDIT: added perl script watching dbus signals. Source: Gnome Screensaver FAQ
#!/usr/bin/perl
my $cmd = "dbus-monitor --session \"type='signal',interface='org.gnome.ScreenSaver',member='ActiveChanged'\"";
open (IN, "$cmd |");
while (<IN>) {
if (m/^\s+boolean true/) {
print "*** Screensaver is active ***\n";
} elsif (m/^\s+boolean false/) {
print "*** Screensaver is no longer active ***\n";
}
}
13 Comments
Sathish Kumar k k
Thanks I got your code working, but I wrote your code inside a java code and packed it as a .jar file to run. Wile I lock or unlock the screen i need to run this file how to do it. I got an idea to monitor screensaver and run my jar file by the code instructed by Mr. viki.omega9, but it gives syntax error. which I mentioned in comment of his answer.
Sathish Kumar k k
I too have a similar type of code as you gave. The thing I need now is to make this jar file execute only while screen is locked and unlocked.
Ortwin Angermeier
So you need something like to listen to a event when the screen is locked/unlocked and then execute the jar file? Please clarify, i don't really understand what you want to accomplish.
Ortwin Angermeier
Take a look at: live.gnome.org/GnomeScreensaver/…
Ortwin Angermeier
The script is written in perl, it's no shell script!
|
Try having a look here, ( Similar duplicate), Detect workstation/System Screen Lock using Python(ubuntu))
GNOME Screensaver FAQ This should be an awesome reference for you to get up to speed. I suppose you are using GNOME.
7 Comments
Sathish Kumar k k
I have seen these sites already and I get syntax error while I run in my terminal. As I'm new to Linux I cant able to debug those syntax error. the error I get this
**/home/sathishkumarkk/Desktop/userinfo.sh: 2: my: not found /home/sathishkumarkk/Desktop/userinfo.sh: 4: Syntax error: word unexpected (expecting ")")**viki.omega9
You should paste your entire script as nobody can debug your errors without looking at code. Use pastie.org and show your code.
Sathish Kumar k k
here is the shell script which i ran in terminal. My shell script inside .sh file
viki.omega9
I think its a good idea if you could brush up your bash scripting skills first. The most obvious error in the second line was, 'my $cmd' which is wrong and should be just, '$cmd'.
Sathish Kumar k k
I had checked by removing
mylater it shows = is not found and what about the error in 2nd line?. one the answerer @ ortang told that this code is perl and not a bash scripting. since i'm very new to linux i cant make the difference, and what do you say for what he has said? |