How do I programatically restart a system service(

2019-02-13 23:29发布

问题:

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