I am having a windows service which I am planning to do an auto restart through windows batch file using "net stop" command. Sometime the service may stop abruptly and show a message box "Could not stop windows service".
Please advise me if i stop the service from windows batch file using "net stop" command whether the popup message will show if the command is unable is stop the service. Kindly assist.
Net Stop is synchronous, meaning it will wait until the service is stopped.
So you are planning to restart the service just do
net stop servicename && net start servicename
net stop will wait until the service is stopped and then run net start
Additionally, using the && method, we reply on the first command to be successful then run the second, if the first command fails, it will not run the second command.
If you have a very stubborn service which is not able to stop, you can build in a loop to check if it is running by using tasklist then attempt to restart if %ERRORLEVEL% is not 0.
You can also see how long the timeout is for the Service Kill in regedit:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\WaitToKillServiceTimeout
Mine for instance in windows 8 is set to 5000 seconds
Related
We may run Firebird as application using command line:
firebird.exe -a -p 3050
Is that possible to shutdown the firebird process using command line too?
There is no "easy" way to do it. If you started firebird.exe as an application, you can quit it by right-clicking on its icon in the taskbar, and selecting shutdown.
The only alternative is to kill it using taskkill, for example:
taskkill /IM firebird.exe
This has the following downsides:
if you have active connections, this will produce a popup to ask for confirmation of shutdown (just like when doing this from the taskbar)
if you have multiple firebird.exe processes, they will all be terminated
You can also force kill to shutdown, this will not produce the popup; open connections will be killed without prompting:
taskkill /F /IM firebird.exe
However, if you regularly need to do this, it might be better to install Firebird as a Window service that doesn't startup automatically. You can then control the service using NET START and NET STOP (or using instsvc).
For example, install Firebird as a service (require administrator command prompt):
instsvc install -demand -name firebird3
This creates a service called "Firebird Server - firebird3"
You can then start and stop the service using NET START "Firebird Server - firebird3" and NET STOP "Firebird Server - firebird3", although this also requires elevated administrator privileges.
I need to run a powershell script whenever the server is rebooted/shutdown (whether graceful or disgraceful reboot).
The script will stop 4 application services at an interval of 1 minute and then finally reboots the system.(This is a business requirement, don't ask why)
How can I make server to invoke the .ps1 script whenever a reboot or a shutdown is initiated.
My test results:
I tried to create a test script which will generate a text file with current date/time and added it to the scheduled task on the trigger of event log 6006 (which is created whenever a system reboot/shutdown is initiated.)
I checked the box -"Run with highest privileges" but after system restart no text file was generated as it was supposed to, although it generates when ran manually.
Do we have any better approach to implement this?
(My final expectation should look like this-
On a random day a random user initiated reboot after a monthly patch when a command prompt window opens before him with message something like:
Stopping service abc...
Stopped.
Waiting for 60 seconds.
Stopping service xyz...
Stopped
EDIT: I've been successfully able to invoke the .ps1 file by adding it to the gpedit as suggested by Kory and Alroc but the script runs only in background when computer restart is initiated. It doesn't opens a regular cmd window to show the progress.
I'm adding the .ps1 script as well below which stops 2 services(chosen for testing purpose) at an interval of 10 seconds and will show the timer as well, only when ran manually.When invoked by the shutdown command it'll stop services only in the background without showing the progress to the user. Kindly assist to achieve this?
Write-Host "Shutdown script invoked"
stop-service W32Time -force -PassThru
for($i = 10 ; $i -gt 0 ; $i--)
{
Write-Progress -Activity "`n Waiting for" -status "`$i equals $i seconds"
sleep 1
}
stop-service wuauserv -force -PassThru
You can use GPO to configure a shutdown script for systems.
You might be able to to it via a Win32_ComputerShutdownEvent watcher as well.
After deep digging, I've finally figured out how to make the cmd window visible while system shutdown in progress.
Here is the complete steps of performing above mentioned expectation:
Open gpedit.msc
Navigate to Computer Configuration->Windows
Settings->Scripts(Startup/Shutdown)->Shutdown.
Go to Shutdown properties. In the powershell scripts tab add your
script and select 'Run Windows Powershell script first'
Above steps will enable the invoke of script at every system shutdown. Now to make the script visible and show progress:
Navigate to Computer Configuration->Administrative
Templates->System->Scripts
Among the policies showing in the right pane enable below
properties:
Run Windows Powershell scripts first at computer start,shutdown
Run shutdown scripts visible
I am trying to create a script that checks if the service Print Spooler is running or not.
The commands to start and stop, and check the status are listed below.
sc start Spooler # Starts service
sc stop Spooler # Stops service
sc query Spooler # Returns status of Spooler
I wanted to hide the output of these commands from the user.
I wanted to check if the service is running.
If yes, then echo to user "Print Spooler already running." and then pause before close.
If no, then echo "Service stopped" then start the service and echo "Service starter" and then pause before close.
Any help will be appreciated.
Why would you want to do something so roundabout. We don't test then do, we do then test if it worked. Testing often uses the same resources as doing, so you chewed up twice the battery life, slowed other programs down TWICE.
#echo off
sc start spooler>%temp%\status.txt
findstr /c:"1056"<%temp%\status.txt>nul&&(Echo Service Already Running&pause&Goto :EOF)
findstr /c:"FAILED"<%temp%\status.txt>nul&&(Echo Service Can't Start&pause&Goto :EOF)||(Echo Service Started&pause&Goto :EOF)
I've created a service, which is a plain console application running via srvany.exe. It was set up to immediately restart when destroyed. Here's the script:
sc delete "Test Service"
sc create "Test Service" binpath= "C:\Windows\System32\srvany.exe" start= auto
sc failure "Test Service" reset= 86400 actions= restart/0
sc description "Test Service" Blah
reg add "HKLM\SYSTEM\CurrentControlSet\Services\Test Service\Parameters" /v Application /d "C:\<path>\testserv.exe"
sc start "Test Service"
It's created properly, and restarts if I kill srvany with taskmgr. But when I kill testserv, srvany keeps running, effectively ruining my plans for immediate restart.
Is there a (either console or registry) key for srvany to exit when its child process gets killed? If not, is there any other simple ways to accomplish this task (without writing a service)?
While srvany will gladly start your application as a service, it does not monitor the spawned process in any way to achieve what you want. We have a competing software called AlwaysUp; read more about srvany's shortcomings there.
The article in CoreTech's answer is from AlwaysUp which itself is a paid application. If you are looking for a free alternative that monitors child processes, try NSSM.
I am trying to start windows 7 unattended using the startnet.cmd file. The problem is that when the start setup.exe command is triggered, the setup runs and closes, but cannot reboot because the startnet.cmd is still open. If I put "exit", it starts the setup and exits immediately. Is there a way to check uf the setup ran successfully and then perform an exit?
wpeinit
net use u: \\192.168.2.10\windows
u:
start setup.exe /unattend:\\192.168.2.10\windows\unattend.xml /noreboot
REM ping -n 30 127.0.0.1
REM exit
I think your commands are correct but you explicitly state that setup.exe shall not initiate a reboot after the first phase of the setup procedure has completed.
The black cmd.exe Window will have to remain open during the setup procedure. If it's terminated then Windows PE performs an immediate reboot. Hence your setup.exe process will be terminated. I think your comment using ping to delay the exit very well shows that you have tried to delay terminating the shell process. But as you can't know how long the setup process will take you cannot fill any good value in the delay algorithm.
Although I think you don't need to. You are launching setup.exe using start which detaches the process and returns to the shell immediately. In fact since setup.exe is the last command you run you could also live with the fact that setup.exe is not detached. Either by not using start or by using start /wait
wpeinit
net use u: \\192.168.2.10\windows
u:
setup.exe /unattend:\\192.168.2.10\windows\unattend.xml /noreboot
exit
or
wpeinit
net use u: \\192.168.2.10\windows
u:
start /wait setup.exe /unattend:\\192.168.2.10\windows\unattend.xml /noreboot
exit
Although I think the entire purpose of this would be to return to the shell and perform the exit (which initiates a reboot) command after setup.exe terminates.
As there are no more commands to be executed I suggest letting setup.exe do the reboot itself by removing the /noreboot flag:
wpeinit
net use u: \\192.168.2.10\windows
u:
start setup.exe /unattend:\\192.168.2.10\windows\unattend.xml
This should keep the shell process (cmd.exe) open while setup is running. At the end of the setup process it will initiate a reboot (initiated by setup.exe) and therefore also te