Windows – Run php exec function to stop windows services

apache-2.2PHPuacwindowswindows-service

I have written a web program in php that needs to stop a windows service and start's it when run like this:

 exec('net stop spooler'); //to stop printer spooler
 exec('net start spooler'); //to start printer spooler

the above codes doesnt stop the printer spooler and i think i know why, because when i open up cmd- command prompt normally and type net start spooler i get access deny…but when i open cmd – command prompt as administrator the command line will work, so i think apache also needs to be given administrator permission so that the above code can work but i cant figure out how to grant the code to run as administrator.

the printer spooler windows service can be found when you click start > type run> in run command type services.msc then you will see printer spooler as one of the windows services…when the above code runs…it should stop that service or starts it

Best Answer

Have you tried the sc command as well? Similar question

Instead of jumping through administrative execution hoops, how about you have PHP write to a file, a physical file, and have a separate program, running with Administrative rights, fire the net stop command? This not only ensures execution but also ensures security. For best reliability you can have a quick VB.NET application with a Timer to check the file every x seconds, but I'm sure you can get away with a running batch file as well, in a pinch. But with VB.NET you can run the process in the background on its own thread, and ensure it stays running.

Hope this sheds a light.

Related Topic