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

家住魔仙堡 提交于 2019-11-30 17:06:31

问题


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

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.

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?


回答1:


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 restart only in the sudoers file.

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

#!/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):

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.




回答2:


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).



来源:https://stackoverflow.com/questions/349884/how-do-i-programatically-restart-a-system-servicenot-apache-from-apache-in-lin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!