I have a Python web application running in a CherryPy server, which is running as a windows service. I have a batch file to deploy this application, but I'm still having to remote desktop in to the server to restart the service. Is there any way to script this?
I tried:
psexec \\server "net restart cherrypyservice"
But this doesn't seem to work.
you could use the sc command-line tool but i don't know how to do it specifically in python.
https://stackoverflow.com/questions/133883/stop-and-start-a-service-via-batch-or-cmd-file/133926#133926
DESCRIPTION: SC is a command line program used for communicating with the NT Service Controller and services. USAGE: sc [command] [service name] ...
EXAMPLE: sc start MyService
Using Russinovich's psservice:
If you want to use psexec:
Though in this case, sc is recommended. It does everything you need if you're going to shell out.
try
with whatever remote execution engine you prefer.
If you have a server that with WinRM enabled on the machines and the PC from which you are deploying the batch file is added to WinRM trusted hosts of the server you can use Invoke-Command
You trigger the batch file using Invoke-Command.
I personally use Invoke-Command when trying to do operation on multiple windows servers .
Hope it helps :)
Using PowerShell Interactively (localy):
Using PowerShell Interactively (remotely):
In a function (remotely):
Using the WMI method
(Get-WmiObject win32_service -computer stp7cor1737ltv4 -filter "Name='SPtimerv3'").invokemethod("StartService",$null)
(Where psservice is another SysInternals app)
or
write a bat file:
net stop "service name"
net start "service name"
you can remote execute the file on the host using:
psexec \\hostname -c batfile.bat