Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 272
ScArcher2 Avatar asked Dec 08 '08 15:12

ScArcher2


1 Answers

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.

like image 94
derobert Avatar answered Oct 18 '22 15:10

derobert