【发布时间】:2012-01-18 06:47:27
【问题描述】:
我正在尝试记下在 ubuntu 操作系统中工作的每个员工的工作站/系统屏幕锁定。我需要将这些记录存储在数据库中。使用 JAVA。我已经到处搜索并想到了 UBUNTU;但是知道如何在 Windows 操作系统中做同样的事情。
【问题讨论】:
我正在尝试记下在 ubuntu 操作系统中工作的每个员工的工作站/系统屏幕锁定。我需要将这些记录存储在数据库中。使用 JAVA。我已经到处搜索并想到了 UBUNTU;但是知道如何在 Windows 操作系统中做同样的事情。
【问题讨论】:
来自here:
gnome-screensaver-command -q | grep "is active"
使用Runtime 类执行该命令并读回结果。
编辑:使用grep -q
这里是一个如何使用它的例子:
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());
}
}
编辑:添加了观察 dbus 信号的 perl 脚本。来源: 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";
}
}
【讨论】:
试试看这里,(类似重复),Detect workstation/System Screen Lock using Python(ubuntu))
GNOME Screensaver FAQ 这应该是一个很棒的参考资料,可以帮助您快速上手。我想你正在使用 GNOME。
【讨论】:
**/home/sathishkumarkk/Desktop/userinfo.sh: 2: my: not found /home/sathishkumarkk/Desktop/userinfo.sh: 4: Syntax error: word unexpected (expecting ")")**
my进行了检查,后来它显示= 没有找到,那么第二行的错误呢?一位回答者@ ortang 告诉我这段代码是 perl 而不是 bash 脚本。因为我对 linux 很陌生,所以我无法有所作为,你对他所说的话有什么看法?