Home > Archive > PHP Programming > March 2007 > Reboot Linux Service with PHP
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Reboot Linux Service with PHP
|
|
| theGerm 2007-03-29, 7:00 pm |
| Is it possible for me to restart a service with PHP? I need to run
this from a webpage
/sbin/service squid reload
| |
| petersprc 2007-03-29, 7:00 pm |
| On Mar 29, 11:59 am, "theGerm" <jrmo...@gmail.com> wrote:
> Is it possible for me to restart a service with PHP? I need to run
> this from a webpage
>
> /sbin/service squid reload
You could do somethine like:
$cmd = 'sudo /sbin/service squid reload 2>&1';
exec($cmd, $output, $exitCode);
if ($exitCode != 0) {
trigger_error("Command \"$cmd\" failed with exit code $exitCode: " .
join("\n", $output), E_USER_ERROR);
}
| |
| Toby A Inkster 2007-03-29, 7:00 pm |
| theGerm wrote:
> Is it possible for me to restart a service with PHP? I need to run
> this from a webpage
>
> /sbin/service squid reload
You can run that using PHP's system() function:
<?php system("/sbin/service squid reload"); ?>
However, normally "/sbin/service X" is just a shortcut to running
"/etc/init.d/X" and if you take a look at the permissions for the contents
of the "/etc/init.d/" directory, they'll mostly be only executable by
root. (And the web server doesn't run as root!)
So the initial reaction is to just change the permissions on
"/etc/init.d/squid" to allow non-root users to run it. That won't work
though.
What you'd need to do is use "sudo" to allow whatever user apache runs
under (normally the user is called "httpd", "apache" or "nobody") to be
able to run "/etc/init.d/squid".
--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
G of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux
* = I'm getting there!
| |
| theGerm 2007-03-29, 7:00 pm |
| On Mar 29, 11:34 am, Toby A Inkster <usenet200...@tobyinkster.co.uk>
wrote:
> theGerm wrote:
>
>
> You can run that using PHP's system() function:
>
> <?php system("/sbin/service squid reload"); ?>
>
> However, normally "/sbin/service X" is just a shortcut to running
> "/etc/init.d/X" and if you take a look at the permissions for the contents
> of the "/etc/init.d/" directory, they'll mostly be only executable by
> root. (And the web server doesn't run as root!)
>
> So the initial reaction is to just change the permissions on
> "/etc/init.d/squid" to allow non-root users to run it. That won't work
> though.
>
> What you'd need to do is use "sudo" to allow whatever user apache runs
> under (normally the user is called "httpd", "apache" or "nobody") to be
> able to run "/etc/init.d/squid".
>
> --
> Toby A Inkster BSc (Hons) ARCS
> Contact Me ~http://tobyinkster.co.uk/contact
> G of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux
>
> * = I'm getting there!
I have this
<?php system("sudo /etc/init.d/squid reload 2>&1"); ?>
on that page I just see text - Password:
| |
| theGerm 2007-03-29, 7:00 pm |
| On Mar 29, 11:16 am, "petersprc" <peters...@gmail.com> wrote:
> On Mar 29, 11:59 am, "theGerm" <jrmo...@gmail.com> wrote:
>
>
>
> You could do somethine like:
>
> $cmd = 'sudo /sbin/service squid reload 2>&1';
> exec($cmd, $output, $exitCode);
> if ($exitCode != 0) {
> trigger_error("Command \"$cmd\" failed with exit code $exitCode: " .
> join("\n", $output), E_USER_ERROR);
>
>
>
> }- Hide quoted text -
>
> - Show quoted text -
With this
<?PHP
$cmd = 'sudo /etc/init.d/squid reload 2>&1';
exec($cmd, $output, $exitCode);
if ($exitCode != 0) {
trigger_error("Command \"$cmd\" failed with exit code $exitCode:
" .
join("\n", $output), E_USER_ERROR);
?>
The page stays blank but the server does not seem to reload.
| |
| Schraalhans Keukenmeester 2007-03-29, 7:00 pm |
| theGerm wrote:
> On Mar 29, 11:34 am, Toby A Inkster <usenet200...@tobyinkster.co.uk>
> wrote:
>
> I have this
>
> <?php system("sudo /etc/init.d/squid reload 2>&1"); ?>
>
> on that page I just see text - Password:
>
>
You can configure sudo's behaviour per command/user. As it is, it seems
like yours is set up so the sudo-ing user has to supply their own
password first before sudo will exec the command (squid int his case,
which prolly has root exec rights only, maybe group wheel as well.)
in /etc/sudoers you can set the desired behaviour for squid and/or the
user that 'runs' apache/php. Normally apache is started as root, but in
httpd.conf the user that holds the apache processes is specified. See ps
aux for details on your apache user.
see man sudo, man sudoers for details on how to set the proper
behaviour. Or check google for mor elaborate examples/walkthroughs.
Hth
Sh.
| |
| Toby A Inkster 2007-03-29, 7:00 pm |
| theGerm wrote:
> I have this
> <?php system("sudo /etc/init.d/squid reload 2>&1"); ?>
> on that page I just see text - Password:
You need to configure sudo to be able to run that command without
prompting for a password. Take a look at the sudo man page, and if you
still need help, try a Linux newsgroup.
--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
G of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux
* = I'm getting there!
| |
| theGerm 2007-03-29, 7:00 pm |
| On Mar 29, 1:25 pm, Schraalhans Keukenmeester <bitbuc...@invalid.spam>
wrote:
> theGerm wrote:
>
>
>
>
>
>
>
>
>
>
> You can configure sudo's behaviour per command/user. As it is, it seems
> like yours is set up so the sudo-ing user has to supply their own
> password first before sudo will exec the command (squid int his case,
> which prolly has root exec rights only, maybe group wheel as well.)
>
> in /etc/sudoers you can set the desired behaviour for squid and/or the
> user that 'runs' apache/php. Normally apache is started as root, but in
> httpd.conf the user that holds the apache processes is specified. See ps
> aux for details on your apache user.
>
> see man sudo, man sudoers for details on how to set the proper
> behaviour. Or check google for mor elaborate examples/walkthroughs.
>
> Hth
>
> Sh.- Hide quoted text -
>
> - Show quoted text -
sudo got it now I added this line in my sudo file
apache ALL= NOPASSWD: /etc/init.d/squid
|
|
|
|
|