perl:使用system()启动进程,然后检查它是否在特定的显示编号下运行

发布于 2024-10-01 22:15:20 字数 722 浏览 0 评论 0原文

我有一个锁定的“信息亭”终端服务器。

该终端服务器有一个 perl 脚本作为其 .Xsession,并启动一个 Tk 界面。当 Tk 界面完成后,perl 脚本启动“process2”并让用户与“process2”(这是一个图形应用程序)交互。

如果用户篡改“process2”并使其崩溃,用户可能能够访问底层桌面,因此我想检查“process2”是否正在运行,以及“process2”是否未在 $display 上运行,我只想执行注销(这将注销 perl 脚本当前运行的显示)。

由于系统同时向 10 个不同的用户运行 10 个“process2”实例,因此我不能仅使用“ps”或类似的东西检查“process2”是否正在系统上运行。我需要检查“process2”是否正在该特定显示器 $display 下运行。 请注意,所有 10 个用户在所有会话中都以相同的用户名登录,因此我无法检查特定用户运行的所有进程,这也会返回所有 10 个实例。

就像:

system("process2 &");
while(1) {
  sleep(1);
  if (is_it_running("process2", $display) == false) {
    system("logout &");
  }
}

我需要知道它的功能“is_it_running”应该是什么样子。 $display 可以包含原始显示编号,例如:“:1.0”,也可以包含解析出来的显示编号,例如:“1”。

I have a locked down "kiosk" terminal server.

This terminal server has a perl script as its .Xsession, and launches a Tk interface. When that Tk interface is done, the perl script launches "process2" and lets the user interact with "process2" (which is a graphical application).

If a user tampers with "process2", and make it crash, the user might be able to access the underlying desktop, therefore I would want to check if "process2" is running, and if "process2" is not running on $display, I would want to just execute logout (which would logout the display the perl script is currently running as).

Since the system is running 10 instances of "process2" to 10 different users simultanuosly, I cant just check if "process2" is running on the system with "ps" or someting like that. I need to check if "process2" is running under that specific display $display.
Note that all 10 users log on as the same username in all sessions, so I cannot check all processes run by a specific user, that would return all 10 instances too.

Like:

system("process2 &");
while(1) {
  sleep(1);
  if (is_it_running("process2", $display) == false) {
    system("logout &");
  }
}

Its the function "is_it_running" that I need to get to know how it should look.
$display can either contain the raw display number, like this: ":1.0", or it can contain the display number parsed out, like this: "1".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最初的梦 2024-10-08 22:15:20

如果您使用 forkexec 而不是 system("...&"),您可以存储子进程的进程ID并更直接地检查它们的状态。另请参阅 perlipc

If you use fork and exec instead of system("...&"), you can store the Process IDs of your child processes and more directly check their status. See also perlipc.

棒棒糖 2024-10-08 22:15:20

为什么不在前台运行 process2 呢?然后你的 Perl 脚本在执行完成之前不会重新获得控制权,此时它可以退出:

system("process2");
system("logout");

当然,如果这是整个脚本,也许 bash 脚本会更有意义。

Why not just run process2 in the foreground? Then your perl script won't get control back until it's done executing, at which point it can exit:

system("process2");
system("logout");

Of course, if that's the entire script, maybe a bash script would make more sense.

青萝楚歌 2024-10-08 22:15:20

经过多次尝试我解决了。

打开管道

$pidofcall = open(HANDLE, "process2|");

然后我做了我需要做的任何事情,并且我让服务器在与 process2 失去连接时向我发送信号。如果我确实需要退出,我只需执行“gotokillprocess;”即可。然后我只是:

killprocess:
kill(9,$pidofcall);
close(HANDLE);
$mw->destroy;
system("logout");

I solved it after many attempts.

Did a piped open

$pidofcall = open(HANDLE, "process2|");

Then I did whatever I did need to do, and I made the server send a signal to me if it loses connection with process2. If I did need to bang out, I simply did a "goto killprocess;" Then I simply had:

killprocess:
kill(9,$pidofcall);
close(HANDLE);
$mw->destroy;
system("logout");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文