如何在 linux 中以编程方式从 apache 重新启动系统服务(不是 apache)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/349884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 17:01:09  来源:igfitidea点击:

How do I programatically restart a system service(not apache) from apache in linux?

phplinuxapachecgirestart

提问by ScArcher2

I need to simple way to allow an end user to restart tomcat from a web page served from apache on the same box.

我需要一种简单的方法来允许最终用户从同一机器上的 apache 提供的网页重新启动 tomcat。

We're trying to make it easy for our QC department to deploy a new version of our webapp to apache. We're using samba, but we need an easy way for them to stop / start the tomcat server before/after the deployment.

我们正在努力让我们的 QC 部门能够轻松地将我们的 web 应用程序的新版本部署到 apache。我们正在使用 samba,但我们需要一种简单的方法让他们在部署之前/之后停止/启动 tomcat 服务器。

This would only be for internal qc boxes. Is there an existing solution for this? or would it be easier to write a few quick php application to handle this?

这仅适用于内部质量控制盒。是否有针对此的现有解决方案?或者编写一些快速的 php 应用程序来处理这个问题会更容易吗?

回答by derobert

Like Skip said, but don't run the CGI as root. Instead, have the CGI call sudo. You can give your web server permission to run /etc/init.d/tomcat restartonly in the sudoers file.

就像 Skip 说的那样,但不要以 root 身份运行 CGI。相反,让 CGI 调用 sudo。您可以授予 Web 服务器/etc/init.d/tomcat restart仅在 sudoers 文件中运行的权限。

I've actually done this at work; the relevant part of the CGI looks like this:

我实际上已经在工作中这样做了;CGI 的相关部分如下所示:

#!/usr/bin/perl
use CGI;
use IPC::Run3;
my $CGI = new CGI;

my $output;
if (defined $CGI->param('go') && 'restart' eq $CGI->param('go')) {
    run3 [ qw(sudo /etc/init.d/tomcat5.5 restart) ], \undef, $output, $output;
}

print <<EOF
Content-type: text/html

Blah, blah, blah, HTML form, displays $output at some point.
EOF

Here is an example line from /etc/sudoers (use visudo to edit, of course):

这是 /etc/sudoers 中的示例行(当然,使用 visudo 进行编辑):

ALL     ALL=(root) NOPASSWD: /etc/init.d/tomcat5.5 restart

That allows everyone to restart tomcat. You could limit it to Apache only if you'd like.

这允许每个人重新启动tomcat。您可以仅在您愿意时将其限制为 Apache。

回答by Skip Head

I would use a CGI script. Set it up to run as root and call '/etc/init.d/tomcat restart' (or however you restart tomcat on your box).

我会使用 CGI 脚本。将其设置为以 root 身份运行并调用“/etc/init.d/tomcat restart”(或者您在您的机器上重新启动 tomcat)。