LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Restart Apache from cgi-bin shell script url (https://www.linuxquestions.org/questions/linux-newbie-8/restart-apache-from-cgi-bin-shell-script-url-4175502793/)

unclesamcrazy 04-24-2014 12:59 AM

Restart Apache from cgi-bin shell script url
 
I want to restart the apache server using url.
I am using cgi-bin shell script to do so but I am not able to do it.
I have created a file apache and put it in /var/www/cgi-bin.
I have written following code in the file apache.
Code:

#!/bin/bash
 echo "Content-type: text/html"
 echo ""
 echo "<html><head><title>Demo</title></head><body>"
service httpd restart
 echo "</body></html>"

but when I execute the file in the browser using
Code:

http://server_ip/cgi-bin/apache
it gives me following output on the web page
Code:

Stopping httpd: [FAILED] Starting httpd:
I have updated /etc/sudoers file and put following content in it
Code:

www-data    ALL=NOPASSWD:/sbin/service httpd restart
but I am not able to restart apache using url.
I can execute only commands which do not require sudo.

I want to restart apache using web url. Please help me.

Thanks a lot !!!

jpollard 04-24-2014 08:23 AM

Think about what you are trying to do... a restart requires apache to stop. To stop apache means that the CGI must also be stopped... To stop the CGI means the command requesting the restart is also stopped - and before the process can finish.

Now it CAN be done, but you would have to allow the sudo command to submit the restart command to cron (use the "batch" command, or the "at" command) so that nothing running under apache is running at the time the cron request restarts apache.

You can TRY putting the sudo command into background, but it may just be killed as part of the process tree (can try using the "nohup" command, but I'm not sure about the timing - you might have to put a 1 second sleep in there to give time for the CGI to finish).

unclesamcrazy 04-24-2014 10:19 AM

Thanks for your answer. Yes it is happening what you are saying. Restart means stop the service and start it again but cgi-bin script needs apache running to execute the complete script and when I say restart, it stops the apache and once apache is stopped, the script is not able to start it again. I have to do it manually using command line.
So is it a dead lock? can't it be done?
You are saying about `at` command but I don't know how to use it.

Well again thanks for your help. if you could do please help me with nohup so It will start the apache after 5 seconds or 10 seconds after thescript execution and stop apache immediately with running cgi-bin script.
so I think it can solve my problem, the procedure will like
Code:

first set command in the script to start apache after 10 seconds --> stop apache with the script --> after 10 seconds --> apache started automatically
Thanks again ! :)

John VV 04-24-2014 11:43 AM

i would use ssh like you are supposed to do

allowing some person access THROUGH the web page to restart the server is asking for a TON of TROUBLE

a "script kiddy" WILL end up having a LOT of "fun" at your expense


All times are GMT -5. The time now is 06:14 PM.