Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am trying to include a call to restart the apache2 server within an .sh script using PHP that runs daily to perform a maintenace routine on my server. I am able to kill the apache server and clear any queued apache processes by placing pkill -9 apache2 within an exec call for php. I am doing it this way to ensure that any apache processes that are build up on the server get removed and do not affect the running of the server.

once the apache processes are killed, I try to restart the apache server by calling /etc/init.d/apache2 restart within a similar exec call, however I get the following error:

sh: /etc/init.d/apache2/: not found

If I run the same command from the command line is executes as expected, just not within the .sh script. Other shell commands within this script also execute without problems.

If anyone can shed some light on this issue I will be very greatful. Thanks in advance.

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

Well the first thing to know is the user that runs the php script (generally www-data under Debian ) don't have the right to stop or start apache.

Plus if you use Php to stop Apache, how can Php start Apache if the apache server is down (so the php won't be executed)

Well the second thing is why you simply don't do /etc/init.d/apache2 stop instead of killing it ? If we forget the rights for the user www-data, you should have in the .sh file :

#!/bin/sh
/etc/init.d/apache2 stop
#
# Maintenance routine
#
/etc/init.d/apache2 start

And at least, why do you use Php to perform maintenance on the server ? can't you use a shell script that you will cron ?

share|improve this answer
    
I should have made that clear that it is command line PHP running through a shell script that is running through cron. I want to kill the processes to ensure that the resources that they are using are freely available to the server again quickly. –  coffeecoder Feb 1 '10 at 10:48
    
well so I don't get it when you say "the apache processes are killed, I try to restart the apache server by calling /etc/init.d/apache2 restart within a similar exec" that means you surely have something like <?php exec('/etc/init.d/apache2 start'); ?> This won't work, because since the process is stopped, php won't work, so myabe try to start the server directly through the script, and not with php. Did I understood what you asked for ? –  Razique Feb 1 '10 at 10:57
    
I have been asked to do it this way by a higher up in the company, just to make sure that apache is not running and stop it if it is i have solved the issue by calling a shell script directly as you suggest, i think i was trying to make it too 'fancy' and failed. Thank you for your responses. –  coffeecoder Feb 1 '10 at 11:11
    
:-) you definitely have all the system tools you need (perl, bash, etc...) in order to achieve such maintenance operations (stopping process, backup a file, etc... ) Plus it's safier (what happens if there is a security hole in the php script ? ) –  Razique Feb 1 '10 at 11:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.