2

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

  • Apply the handbrake now! Granting your Apache process access to an "administrative token" is a very bad idea. What are you actually trying to achieve? – Simon Catlin Apr 13 '15 at 21:00
  • This is offline (local server - XAMPP) and works over a network. we are trying to hack a way to be able to restrict printing until a job has being entered into the web app (job order)....this is gonna to be possible in a funny way. 1. the staffs dont know much about system services 2. i set the computers printer spooler to manual and always disabled...so if you try to print...you will not find a printer installed....but when you enter the job into the app i trigger a script to start printer spooler and it triggers a script to stop again after 2 mins..when they should have sent job to printer – user2666633 Apr 13 '15 at 21:08
  • Hmmmm.... Your code (HTML, PHP, scripts, etc) executed on Web server (local server - XAMPP) . Printing - is's CLIENT (PC with browser accessing your site over network) feature. Are you trying to control spooler service on clients from server??? – Sergey Apr 13 '15 at 21:22
  • yes, thats why i said its funny but very possible...just think about it....try this...open your command prompt and type "net stop spooler" then try to print...you will not see any preinstalled printer until you run "net start spooler" so if i can run this command via my php script on local server then i can choose when a system can print or not. The PC will not be able to print till user goto services.msc and start printer spooler or he runs a command line net start spooler – user2666633 Apr 13 '15 at 21:26
  • This all feels very hacky. I don't generally just recommend products but have you thought of investing in for example [papercut](http://www.papercut.com/)? I'm pretty sure if you look at your billable rate and compare it to the price of most print management systems you're spending more by not just going for an off-the-shelf solution. – Reaces Apr 13 '15 at 21:27
  • @Reaces i totally agree its just so hacky but the problem is the web app has more other cool custom features (expenditure, staff managements, attendance, customer management, job order etc) this is just like the finally feature to make it complete...cant find any software with all the features we need and if we find then it might have to be customized for us and we might not be able to pay :( – user2666633 Apr 13 '15 at 21:32
  • @user2666633 Why does this _have_ to be included in your web app? Not everything needs to be Frankensteined together. In fact it's _much_ more difficult to scale something if it's bloated and self-made. When your company grows you might end up ripping it up and creating separate modules after all. – Reaces Apr 13 '15 at 21:34
  • @Reaces i totally agree with you – user2666633 Apr 13 '15 at 21:40
  • user2666633, 1) your PHP code runs on server(!). So your "net start/stop spooler" will control spooler service on server (local server with XAMPP) only, not clients accessing your web app over network. 2) About starting/stopping spooler (and other services) on server itself... instead of playing with various system settings which can affect security (even in offline) it's easy to implement simple procedure with 'semaphore file': create file from PHP (it's easy), e.g. spolerrestart.txt and create an every minute batch task with script checking file existance and restarting service if found. – Sergey Apr 13 '15 at 21:42

1 Answers1

1

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.

Aaron Gillion
  • 107
  • 1
  • 5