I want to restart Windows service using command prompt in [Icons] section using Inno Setup. Please help me to solve the problem.
You can use sc start [service] to start a service and sc stop [service] to stop it. With some services net start [service] is doing the same.
But if you want to use it in the same batch, be aware that sc stop won't wait for the service to be stopped. In this case you have to use net stop [service] followed by net start [service]. This will be executed synchronously.
You could create a .bat-file with following content:
net stop "my service name"
net start "my service name"
net.exe stop "servicename" && net.exe start "servicename"
To restart a running service:
net stop "service name" && net start "service name"
However, if you don't know if the service is running in the first place and want to restart or start it, use this:
net stop "service name" & net start "service name"
This works if the service is already running or not.
For reference, here is the documentation on conditional processing symbols.
This is my code, to start/stop a Windows service using SC command. If the service fails to start/stop, it will print a log info. You can try it by Inno Setup.
{ start a service }
Exec(ExpandConstant('{cmd}'), '/C sc start ServiceName', '',
SW_HIDE, ewWaitUntilTerminated, ResultCode);
Log('sc start ServiceName:'+SysErrorMessage(ResultCode));
{ stop a service }
Exec(ExpandConstant('{cmd}'), '/C sc stop ServiceName', '',
SW_HIDE, ewWaitUntilTerminated, ResultCode);
Log('sc stop ServiceName:'+SysErrorMessage(ResultCode));
You can start and stop and query services using the SC command. As for innosetup i'm not sure.
PowerShell features a Restart-Service cmdlet, which either starts or restarts the service as appropriate.
The Restart-Service cmdlet sends a stop message and then a start message to the Windows Service Controller for a specified service. If a service was already stopped, it is started without notifying you of an error.
You can specify the services by their service names or display names, or you can use the InputObject parameter to pass an object that represents each service that you want to restart.
It is a little more foolproof than running two separate commands.
The easiest way to use it just pass either the service name or the display name directly:
Restart-Service 'Service Name'
It can be used directly from the standard cmd prompt with a command like:
powershell -command "Restart-Service 'Service Name'"
Related
I want to run an old .NET application in a docker windows server container (https://hub.docker.com/r/microsoft/windowsservercore/).
Everything would be easy if this application didn't require an UI. Its UI does a lot of stuff and this stuff cannot be done through command line or other API.
Basically, the perfect thing would be to reach this running container through RDP.
From my understanding, it is nothing more than a service (TermService) running on a certain TCP port (3389 being the default one).
But it seems that TermService is not running in microsoft/windowsservercore containers.
I found an article showing how to activate it : https://withinrafael.com/2018/03/09/using-remote-desktop-services-in-containers/
Basically, I kept the same Dockerfile, just changing some credentials.
#escape=`
FROM microsoft/windowsservercore:1709_KB4074588
RUN net user /add jerome
RUN net user jerome aDifficultPassword
RUN net localgroup "Remote Desktop Users" jerome /add
RUN net localgroup "Administrators" jerome /add
RUN cmd /k reg add "HKLM\System\CurrentControlSet\Control\Terminal Server" /v TemporaryALiC /t REG_DWORD /d 1
I launch the container with this command :
docker run -it -p3389:3389 myimage powershell
When I connect to the container and type some powershell commands to list running services, I can see that TermService is well running. This is the command I use to list services.
Get-Service
When I list opened TCP ports, I can see that 3389 is listened. This is the command I use to show opened ports.
netstat -an
When I try to connect to the container through my remote desktop client, things seems OK at start.
It asks me for host.
Then for a username and password.
If I type wrong credentials, it says me "Wrong credentials", so there is well a communication with the server.
If I type good credentials, nothing happens. No error message at all, but no display screen too...
I don't really know if logs are generated somewhere or not.
I would be OK if instead of RDS, something like TigerVNC was working. I have never tried this tool before but it seems that it could do the job.
How would you do to control a GUI application running in a windows container?
You can find logs for RDP client in event viewer : "Application and Services Logs"\Microsoft\Windows\TerminalServices-ClientActiveXCore. Here's what is says for me :
The client has established a multi-transport connection to the server.
RDPClient_SSL: An error was encountered when transitioning from TsSslStateDisconnected to TsSslStateDisconnected in response to TsSslEventInvalidState (error code 0x8000FFFF).
RDP ClientActiveX has been disconnected (Reason= 2)
reason 2 is session closed by client.
My paranoia tells me that microsoft went back and patched the image to prevent people from using RDP with docker, but who knows, maybe we're just missing something obvious.
After I build my application on Jenkins,
the output message says my build is success, however I get this error message in the end, and my website can't work...
D:\Jenkins\jobs\1.job\workspace>SC \\123.45.12.133 START w3svc
[SC] StartService FAILED 1056:
An instance of the service is already running.
Any suggestion?
It says the w3svc(World Wide Web publishing service) can not start by Jenkins.
So you should start it manually by following steps:
Open your services in administrative tool
Right click w3svc(World Wide Web publishing service) and re-start it.
Or use command line and input sc start w3svc
When trying to restart an existing windows service via NSSM, I randomly get the below message which is written to the error log/error output. Any ideas on how to rectify? Ideally, accept as a valid response.
Unexpected status SERVICE_START_PENDING in response to START control
You will get "SERVICE_START_PENDING" if the the service takes too long to start (it means the service hasn't told Windows that it's started yet). "Too long" is up to the application issuing the start request. In the case of NSSM this appears to be very short, so if your system is under load the service is taking longer to start than NSSM expects.
There appears to be no way to tell NSSM how long a start or stop operation should take. For stop requests it even ignores its own shutdown timeout settings. Your option then for using NSSM is to compile from source and add a timeout option. Otherwise use a different tool, e.g. net:
net stop <service>
net start <service>
I believe that this issue was caused by the service itself.
It is highly probable that the service has bug and hang, fail, or take too much time to stop properly. Which raise issue when try to start it.
If the service fail:
In my opinion, there is a potential workaround,by setting up a service recovery option in the service properties. Then select "Run a program" when the service failed.
Then code a batch to get the PID of this service and kill it, then use NSSM to start it again.
In this batch you may use "SC query" command to check the service status:
C:\Windows>sc query "MyService" | find "STATE"
STATE : 3 STOP_PENDING
Note that if you use NSSM only to hide the windows, you may achieve the same goal with the Task Scheduler only.
In the "General" tab on task property. If you select "Run whether user is logged on or not", this will run from session 0 which won't show any window to you.
Then what you need to do on your scheduled task are, to kill / restart the target process itself as you do now. This will work more robustly.
If the service is not an official windows service but more an EXE program file (dev on your side) then converted to a services with NSSM, there is a high chance of failure.
It could be better to rewrite/recompile the program as an actual Windows service.
The problem is that NSSM is timing out waiting for your service to start. Unfortunately it does not seem to be possible to provide NSSM with a custom timespan for the service start-up timeout.
One workaround is still use NSSM to install the service, but then use PowerShell to start and stop the service. PowerShell allows you to specify your own wait timeout (since you probably don't want your script to wait indefinitely).
$serviceName = 'My service'
$service = Get-Service $serviceName -ErrorAction Ignore
Write-Verbose "Starting service '$serviceName'."
$service.Start()
$waitTimeout = New-TimeSpan -Seconds 5
$service.WaitForStatus([System.ServiceProcess.ServiceControllerStatus]::Running, $waitTimeout)
You can read more about the PowerShell Get-Service command here: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-service
Recently happened to me... not only I cannot restart the service but also stop it is not possible neither.
Lets kill it.
Identify the process id (PID). Replace [SERVICE-NAME] with your service's name.
sc queryex "[SERVICE-NAME]"
You should get something like this:
SERVICE_NAME: xxxxxxxxx
TYPE : 30 WIN32
STATE : 4 RUNNING
(STOPPABLE, PAUSABLE, ACCEPTS_SHUTDOWN)
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x0
PID : 4812
FLAGS :
Manually kill the process. Replace [PID] with the value from the previous step.
taskkill /pid [PID] /f
This error is also related to
Windows could not stop the [SERVICE-NAME] service on Local Computer.
Error 1053: The service did not respond to the start or control
request in a timely fashion.
I have created a service using sc.exe for tomcat6.exe. The command is:
sc.exe create "Tomcat Service" binPath= "D:\tomcat\bin\tomcat.exe
Creating the service was successful, but I get the following error while starting the service.
Error 1053: the service did not respond to the start or control request in a timely fashion
Both starting from cmd using sc.exe start "Tomcat Server" and services.msc result in the same error.
Can anyone help me please?
Thank you all in advance.
While Tomcat.exe is a true windows service, installing it with SC is not good enough. Install Tomcat 6 as a service using the "service.bat" file or by composing your own command line options for tomcat6.exe (including the //IS// parameter).
I have created a windows service and in the service in control panel -> administrative tools -> services, its status is starting.
I want to stop this service, but the stop option is grayed out.
How can I start/stop the service?
Every time I restart, then it becomes stopped and I can delete it.
If you run the command:
sc queryex <service name>
where is the the name of the service, not the display name (spooler, not Print Spooler), at the cmd prompt it will return the PID of the process the service is running as. Take that PID and run
taskkill /F /PID <Service PID>
to force the PID to stop. Sometimes if the process hangs while stopping the GUI won't let you do anything with the service.
You could do it in one line (useful for ci-environments):
taskkill /fi "Services eq SERVICE_NAME" /F
Filter -> Services -> ServiceName equals SERVICE_NAMES -> Force
Source: https://technet.microsoft.com/en-us/library/bb491009.aspx
If the stop option is greyed out then your service did not indicate that it was accepting SERVICE_ACCEPT_STOP when it last called SetServiceStatus. If you're using .NET, then you need to set the CanStop property in ServiceBase.
Of course, if you're accepting stop requests, then you'd better make sure that your service can safely handle those requests, especially if your service is still progressing through its startup code.
As Aaron mentioned above, some services do not accept SERVICE_ACCEPT_STOP messages, by the time it was developed. And that is hard coded into the executable. Period. A workaroud would be not to have it started, and as you cannot change its properties, forcibly do the following:
Boot into safe mode (Windows 10 users might need msconfig > boot > safe boot)
Regedit into HKLM > System > ControlSet001 > Services
Locate your service entry
Change 'Start' key to 3 (manual startup) or 4 (disabled)
If you cannot change the entry, right-click on your service name on the left pane, select 'Permissions', check that 'Everyone' has full access and try step 4 again.
Don't forget to disable safe boot from msconfig again, and reboot !
Open command prompt with admin access and type the following commands there .
a)
tasklist
it displays list of all available services . There you can see the service you want to stop/start/restart . Remember PID value of the service you want to force stop.
b) Now type
taskkill /f /PID [PID value of the service]
and press enter. On success you will get the message
“SUCCESS: The process with PID has been terminated”.
Ex : taskkill /f /PID 5088
This will forcibly kill the frozen service. You can now return to Server Manager and restart the service.
sc queryex <service name>
taskkill /F /PID <Service PID>
eg
I solved the problem with the following steps:
Open "services.msc" from command / Windows RUN.
Find the service (which is greyed out).
Double click on that service and go to the "Recovery" tab.
Ensure that
First Failure action is selected as "Take No action".
Second Failure action is selected as "Take No action".
Subsequent Failures action is selected as "Take No action".
and Press OK.
Now, the service will not try to restart and you can able to delete the greyed out service from services list (i.e. greyed out will be gone).
Here's a simple method to kill a service you can't stop directly, if that service has dependencies.
Open the service's properties window & click on dependencies tab
See what it needs to run
Stop one of those if possible, being sure that it won't also crash Windows
For example, stopping "network store interface service" aka nsi will kill an unkillable dnscache service. It will also kill all network capabilities & may require restarting Windows to get them back. I've had to do this to edit the hosts file, sometimes dnscache refuses to let go & you can't update hosts without killing it first but you can't do it directly.
The crucial thing that a lot of the suggestions dont make clear is that you must 'start command window as administrator' even if your already logged in as an administrator.