sh/expect/sudo/screen 所有需要在一个命令中一起完成

发布于 2024-09-16 05:05:17 字数 584 浏览 5 评论 0原文

我需要发生什么:PHP 需要启动一个在后台运行的具有 root 权限的服务器应用程序。这一切都应该保持沉默。

-需要sudo来允许php执行需要root权限的操作。

- 需要屏幕才能允许应用程序在启动该进程的网页范围之外运行。

需要 -Expect 以便屏幕有一个运行点

-Sh 是需要的,因为无论开始运行什么都需要后台运行,大概是使用 &操作员。它还需要将任何输出通过管道传输到 /dev/null/ 因为我不希望我的 PHP 页面返回任何内容。如果有人能想出更好的方法在 PHP 中进行调用(fork...?),这可能是可以协商的

作为一个例子,我尝试使用的脚本是:

#!/usr/bin/expect -f
spawn sh ( screen -t srcds /usr/local/srcds_l/startserv )& > /dev/null
exit 0

作为参考,我正在尝试启动一个 Counter-Strike 源server,startserv 是处理启动服务器并收集其输出的 C 代码的名称。任何人都可以纠正我该片段的语法,或者告诉我为什么这样做是错误的吗?

What I need to have happen: PHP needs to launch an server app which has root permissions running in the background. All of this should be silent.

-Sudo is needed to allow php to perform an op that requires root permissions.

-Screen is required to allow the app to run outside the scope of the webpage which started the process.

-Expect is needed so that screen has a pts in which to run

-Sh is needed because whatever starts running needs to be backgrounded, presumably with the & operator. It would also need to pipe any output to /dev/null/since I don't want my PHP page returning anything. This is probably negotiable somehow if somebody can think of a better way to do the call in PHP (fork...?)

As an example, the script I tried to use was:

#!/usr/bin/expect -f
spawn sh ( screen -t srcds /usr/local/srcds_l/startserv )& > /dev/null
exit 0

For reference I am trying to start a Counter-Strike Source server, and startserv is the name of the C code which handles launching the server and collecting its output. Can anybody correct my syntax for that snippet, or tell me why its the wrong thing to do?

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

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

发布评论

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

评论(3

献世佛 2024-09-23 05:05:17

这种情况最终通过简单地使用 fork 和进程组控制使进程自行守护,然后让 PHP 执行带有 sudo 的系统调用来解决。

我有另一个类似的情况,最终发现

unbuffer -p

它在内部使用 screen,并完成我最初考虑让 screen Expect 和 sh 做的工作。

This situation was eventually solved by simply making the process daemonize itself using a fork and process group control and then just having PHP do a system call with a sudo in it.

I had another similar situation to eventually found

unbuffer -p

which uses screen internally, and does the kind of work that I was initially thinking of having screen expect and sh do.

初相遇 2024-09-23 05:05:17

我没有为您提供完整的解决方案,但您可能想查看 nohup 而不是 screen。这也消除了 Expect 的需要。

I don't have a complete solution for you, but you might want to look at nohup instead of screen. That eliminates the Expect need as well.

鯉魚旗 2024-09-23 05:05:17

传达 1 位信息(“现在就开始”)是巨大的开销。如果您忘记了 PHP,那么所有其他从属要求都会消失。

如果我尝试这样做,我可能会编写一个 perl 脚本来侦听 TCP 端口上的“启动”命令,然后分叉进程本身。这样就不再需要 sh、expect、screen 和 sudo。

That's a huge amount of overhead to communicate 1 bit of information ("start yourself now"). If you forget PHP then all the other subordinate requirements fall away.

If I was trying to do this, I'd probably write a perl script to listen on a TCP port for a "start" command and just fork off the process itself. This gets rid need for sh, expect, screen, and sudo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文