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

Start and stop Apache from the dock Internet
I wanted a way to quickly start and stop Apache without having to go into System Prefs every time. This small AppleScript can be placed in the Dock, allowing one-click launching and terminating of Apache. One caveat: In order for this to work, you have to edit /etc -> sudoers to give yourself the ability to launch Apache without having to enter your password -- see man sudoers for help with that process. That's a potential security risk, but as I see it, it's no riskier than allowing System Prefs to launch it via a button.
  do shell script "sudo /usr/sbin/apachectl stop"
  if result = "/usr/sbin/apachectl stop: httpd (no pid file) 
  not running" then
    do shell script "sudo /usr/sbin/apachectl start"
    display dialog "Apache Started" with icon 2 buttons 
    {"OK"} giving up after 1
  else
    display dialog "Apache Stopped" with icon 2 buttons 
    {"OK"} giving up after 1
  end if
Enter the script in Script Editor, save it as an application, and drag it into your dock.
    •    
  • Currently 2.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[14,811 views]  

Start and stop Apache from the dock | 7 comments | Create New Account
Click here to return to the 'Start and stop Apache from the dock' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Start and stop Apache from the dock
Authored by: olwylee on May 22, '03 11:27:05AM

Kewl hint, but what I find easier for starting Apache is SharingMenu. it should be available at macupdate or VT.

If you can't find it there, ask me nice ;=) in an email and i would be glad to send it to you.



[ Reply to This | # ]
Start and stop Apache from the dock
Authored by: cello on May 22, '03 11:48:43AM

olwylee was faster...

the direct link is:

http://www.mani.de/en/software/macosx/sharingmenu/index.html



[ Reply to This | # ]
Start and stop Apache from the dock
Authored by: dr_turgeon on May 22, '03 01:32:54PM

The nice thing about this hint is the help one gets in making these kinds of simple script programs for OS X, using a real life example. A program that does it for you is mostly beside the point.

What would be a nice add-on to this hint would be a suggestion about getting compiled scripts to run instantly without the overhead of launching an app. I know script menu can, but what about from the dock?



[ Reply to This | # ]
Start and stop Apache from the dock
Authored by: robJ on May 22, '03 04:22:29PM
Save this as a stay-open application. When launched, it should run the script provided by the original poster. On subsequent clicks of the script's Dock icon, it will run the script again, without the need to go through the entire launch lag. I haven't tested the code but it should be close to what you want.
on run
	my toggle -- remove this if you don't want it to execute the code on launch.
end run

on reopen
	my toggle
end reopen

on toggle()
	do shell script "sudo /usr/sbin/apachectl stop"
	if result = "/usr/sbin/apachectl stop: httpd (no pid file) 
  not running" then
		do shell script "sudo /usr/sbin/apachectl start"
		display dialog "Apache Started" with icon 2 buttons 
			{"OK"} giving up after 1
	else
		display dialog "Apache Stopped" with icon 2 buttons 
			{"OK"} giving up after 1
	end if
	
	tell application "System Events" to 
		set visible of first application process whose frontmost is true to false
end toggle
     

[ Reply to This | # ]
Start and stop Apache from the dock
Authored by: discordantus on May 22, '03 04:36:03PM
If you aren't too keen on changing the sudoers file to not require your password, you can let applescript take care of the auth for you:


do shell script "/usr/sbin/apachectl stop" with administrator privileges
if result = "/usr/sbin/apachectl stop: httpd (no pid file) not running" then
	do shell script "/usr/sbin/apachectl start" with administrator privileges
	display dialog "Apache Started" with icon 2 buttons 
		{"OK"} giving up after 1
else
	display dialog "Apache Stopped" with icon 2 buttons 
		{"OK"} giving up after 1
end if

If you want, you can throw your password into the script and it won't ask you for it:

do shell script "/usr/sbin/apachectl stop" password "mypassword" with administrator privileges

On a side note, I had a problem with the line break in the second statement, after "(no pid file)"... I had to remove it to get the script to work.

[ Reply to This | # ]

Start and stop Apache from the dock
Authored by: Graff on May 22, '03 08:14:55PM

On a side note, I had a problem with the line break in the second statement, after "(no pid file)"... I had to remove it to get the script to work.
Right, that's because the line break occurs within a quoted string. In order to break lines in code in the middle of a quoted string you need to make two strings with a concatenation and the line break in between them.

Original lines:
if result = "/usr/sbin/apachectl stop: httpd (no pid file) 
not running" then
do shell script "sudo /usr/sbin/apachectl start"

Corrected lines:
if result = "/usr/sbin/apachectl stop: httpd (no pid file)" & 
"not running" then
do shell script "sudo /usr/sbin/apachectl start"



[ Reply to This | # ]
Start and stop Apache from the dock
Authored by: JYF on Dec 09, '09 08:22:13AM
The scripts above don't work anymore in Mac OS X 10.6.x running Apache 2.

V.K., from Apple discussion boards, provides a solution here.

[ Reply to This | # ]