Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A script to restart Apache after power failure in 10.2 Internet
I've seen a couple of fairly complex hints that were written to overcome the bug in OS X 10.2 that fails to restart the Apache webserver after a power failure. Being a UNIX newbie (fear of complex UNIX hints), and already having some experience with AppleScript's ability to execute shell scripts through the do shell script command, I thought I'd try creating an AppleScript solution. I wanted to create an application to run as a StartUpItem that starts the Apache server via an apachectl start command. It works and is dead simple:

Open Script Editor, and type the following line in, changing the placeholder value yourpassword to your administrator password:
do shell script 
 "/usr/sbin/apachectl start" password 
 "yourpassword" with administrator privileges
Save 'Start Apache' as file format Application in a convenient location. Set the application to run on startup (in 10.2, this is in the Preference Pane 'Login Items'). Energy Saver option 'Restart automatically after a power failure' must be checked. Toggle the switch on your power source to test (close all open files first, of course!).
    •    
  • Currently 3.00 / 5
  You rated: 3 / 5 (4 votes cast)
 
[14,978 views]  

A script to restart Apache after power failure in 10.2 | 8 comments | Create New Account
Click here to return to the 'A script to restart Apache after power failure in 10.2' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A script to restart Apache after power failure in 10.2
Authored by: mcroft on Mar 08, '05 12:19:10PM

I thought turning it on in the sharing control panel added it as a startup item.

In any case, you might want to use "apachectl graceful" instead of "start".



[ Reply to This | # ]
A script to restart Apache after power failure in 10.2
Authored by: goatbar on Mar 08, '05 01:06:35PM

This would be a good place for using sudo to allow a user to run apachectl with out a passwd (sudoers keyword - NOPASSWD). They you are not saving your password cleartext to somewhere on your disk. Then the applescript would just need "sudo" in front of /usr/sbin/apachectl

man sudoers for more info on the sudo config file and remember to user visudo to edit the sudoers

-kurt



[ Reply to This | # ]
A script to restart Apache after power failure in 10.2
Authored by: kindall on Mar 08, '05 03:00:46PM
This hint doesn't restart Apache after a restart -- it restarts Apache on login. Thus it only works if you have Mac OS X set to log you into an appropriate account on startup. Which, on a server, is probably a bad idea...

[ Reply to This | # ]
A script to restart Apache after power failure in 10.2
Authored by: TvE on Mar 09, '05 12:56:51PM

I think that the author is NOT running OS X Server but OS X client - the server has a Watchdog process that *should* take care of restarting Apache etc. (I have not verified that it actually DOES it - since I have not had any powerfailiures (yet) ;-)



[ Reply to This | # ]
The Unix Way
Authored by: escowles on Mar 08, '05 04:53:43PM

To allow starting of apache without a password hardcoded in a script (among the most insecure things you can do, btw), you should use sudo's NOPASSWD option. Use visudo to edit /etc/sudoers and add the line:

USERNAME ALL = NOPASSWD: /usr/sbin/apachectl
(you'll need to change USERNAME to your short username).

If you want all admin users to be able to restart apache without password, do:

%admin ALL = NOPASSWD: /usr/sbin/apachectl

Also, as another user pointed out, the startup items are only run when you login, not at boot. To run something when the machine boots, add it to your crontab using the @reboot keyword. Use crontab -e to edit crontab and add the line:

@reboot sudo /usr/sbin/apachectl graceful

Though at this point, you might just want to edit /System/Library/StartupItems/Apache to always startup apache, regardless of the config. I'm not sure what the bug is that makes it not work after a power failure, but just making the StartService function read:

apachectl start
would probably do the trick.

-Esme



[ Reply to This | # ]
The Mac Way
Authored by: thinkyhead on Mar 08, '05 10:03:58PM

I don't know why Apache wouldn't be starting correctly after a power failure on a system with a journaled file system. But if this is a persistent issue then you need to use a more savvy method to restart it than a login item.

The way Apache starts during boot on Mac OS X is through the script /System/Library/StartupItems/Apache/Apache. Whether or not it starts is controlled by the line WEBSERVER=-YES- in /etc/hostconfig. When you click the checkbox in the Sharing panel this line in /etc/hostconfig is set to -YES- and Apache is started with the command SystemStarter start "Web Server".

If you are finding that Apache isn't starting after a power failure, one way to handle it is to make your own StartupItems bundle in the user-configurable /Library/StartupItems folder following Apple's methods (which may or may not work if it runs into the same problem).

But the smartest method by far is to create a cron script that tests for the Apache process every five minutes and runs SystemStarter start "Web Server" as root if it finds no Apache running. In fact here's the script:


#/bin/tcsh
ps -axc | grep httpd || SystemStarter start "Web Server"

By the way the appropriate place to use apachectl graceful is when you're doing something like rolling the logs in a script or editing the httpd.conf file and you want to restart Apache without disrupting connected users.

---
|
| slur was here
|

[ Reply to This | # ]

The Mac Way (10.2)
Authored by: thinkyhead on Mar 08, '05 10:10:11PM
I see your post referred to 10.2 and not 10.3, so I'm not sure if the SystemStarter works the same way. If not you can replace the systemstarter line in the cron script with apachectl start and it will work just fine. The main reason I suggest the SystemStarter method is that it shields you from the variety and version of web server.

---
|
| slur was here
|

[ Reply to This | # ]

The Mac Way
Authored by: derekhed on Mar 09, '05 02:47:57AM

Nice post slur.
Thanks.



[ Reply to This | # ]