Batch file won`t start program when launched from windows service - windows

I have created a batch file to run when a specific service stops.
The batch file should stop the relevant software running, restart some services (stop / start) and start the software again.
It`s a simple code:
#echo off
Taskkill /F /IM program1.exe
Taskkill /F /IM program2.exe
timeout /t 5
net stop service1
net stop service2 && net start service2
net start service3
timeout /t 2
start C:/path/program1.exe
start C:/path/program2.exe
Have set the service up to run the batch file as recovery at first failure.
Batch file is linked via a shortcut to be able to run it as administrator.
This works perfectly when running the batch file directly, but when it`s executed by the service recovery, the start-up of the software fails.
Does anyone have any idea what could be wrong?

In some circumstances timeout /t 5 might cause trouble. Try to replace it with PING -n 6 127.0.0.1 > NUL.

Related

Batch File as Service - TaskKill not killing task

If I run my batch script as normal from cmd it seems to work fine.
What I'm trying to do is setup my script as a service using nssm. That seems to work as expected except for Taskkill - this isn't killing the desired application for some reason.
Anybody have some suggestions?
This is my taskkill code in my bat file:
TASKKILL /f /t /im "application.exe"

Shut down machine after being idle with batch file?

I don't even know if this is possible, but is there anyway to shut down a machine after (n) minutes, using a batch file?
Currently I already have a batch file which works alongside ranorex and a virtual machine, and I need the machine to shut itself down after it has been idle for 10 minutes or so, just to give everything else in the batch file plenty of time to run. Is there any way of doing this?
Just use the command for shutdown and add the option for the time to wait:
shutdown /s /t 600 /f
/s is for shutting down the computer
/t is for wait before do the operation with time in seconds (10*60=600)
/f is for forcefully close all applications
With shutdown /a you can abort the action befor the time runs off. For more options use the help of the shutdown-command.

Batch file to commit changes to Embedded XP and then restart pc

I would like to create a batch file to run a cmd command to commit changes to a windows embedded pc from a USB drive and restart the PC to make the changes active.
The cmd line I use is:
ewfmgr -commit c:
But I need to open the cmd prompt and run the command then once it has run, restart the PC
This is what worked
#echo OFF
:reboot
c:\windows\system32\ewfmgr.exe C: -commit
echo Rebooting...Please Wait
c:\windows\system32\xpepm.exe -restart
pause
shutdown /? could give some hints. Then your batch file might look as follows:
ewfmgr -commit c:
shutdown /r
To ensure batch wait until ewfmgr command ends, use
start "" /W ewfmgr -commit c:
shutdown /r
With the /W or /WAIT switch, the start command will start application and wait for it to terminate. More info on start command.
To give a some kind of wait after the commit command so that it can run and finish its task, e.g. for a delay of 30 seconds:
add timeout /T 30 /nobreak>nul line before shutdown /r, and/or
use shutdown with /t xxx switch (this sets the time-out period
before shutdown to xxx seconds), i.e. shutdown /r /t 30
A workaround if timeout command is not recognized: PING -n 31 127.0.0.1>nul
Create a new file with the .bat extension. Open it in your preferred text editor and enter the commands you want to be run, and save the file. The commands that JosefZ wrote would probably do the job perfectly.

Starting and ending batch files

I am attempting to start recording the screen using Camtasia however am triggering this using a batch file.
As i need the recordings to be 30 seconds long, i have a secondary batch file. It doesn't work as intended as i would like to avoid multiple windows executing the commands opening. I know the command taskkill isn't correct and have included it to illustrate what i am intending to achieve.
The batch files need to be able to run on both Windows 7 and Windows 8 devices.
The process is started with a starting batch file that launches the secondary batch file to commence the countdown.
StartFile.bat
start EndFile.bat
"C:\Program Files\TechSmith\Camtasia Studio 8\CamRecorder.exe" /record
EndFile.bat
timeout /T 30 /NOBREAK
"C:\Program Files\TechSmith\Camtasia Studio 8\CamRecorder.exe" /stop
timeout /T 30 /NOBREAK
taskkill /f /im StartFile.bat <-- To kill the first batch file so there aren't multiple windows
start StartFile.bat <-- To create a new instance of the starting batch file.
exit
Just do it all in a single batch file with a GOTO loop (edited to take account of the discussion in the comments; the start is only needed because starting recording doesn't return control):
:startofloop
start "" "C:\Program Files\TechSmith\Camtasia Studio 8\CamRecorder.exe" /record
timeout /T 30 /NOBREAK
"C:\Program Files\TechSmith\Camtasia Studio 8\CamRecorder.exe" /stop
timeout /T 30 /NOBREAK
goto startofloop

Stop a Windows Service that is dependant on another service via Batch File

All,
I'm trying to stop a Windows service that we have created that is dependant on another service. I just want to stop both of the service using a batch file, sc command for example, where the services are running on a remote machine.
I have tried stopping the services in the order of dependancy (least dependant first), but it does not stop the service.
For example Service1 depends upon Service2 which is configured within the Service settings in the Services console. I am running the script on my Windows 7 PC and the server runs Windows Server 2003.
The following lines are in the noddy batch file I created:
sc \\SERVER stop "Service1"
sc \\SERVER stop "Service2"
The output in the Command Console is:
D:\Test>sc \\SERVER stop "Service2"
[SC] ControlService FAILED 1051:
A stop control has been sent to a service that other running services are dependent on.
The service Service2 will not stop. Service1 stops fine.
Any ideas?
Thanks,
Andez
The "net stop" command has a parameter which is not commented. This parameter is /yes and will automatically stop all the dependent services too
So to stop a service with or without dependencies you just type
net stop spooler /yes
You can check which dependencies a service has by running sc qc <service>
And in order to script that and retrieve the dependencies you can put it in a for-loop
Example:
#echo off
setlocal enabledelayedexpansion
set service=winmgmt
set server=server
for /f "Tokens=2 Delims=:" %%i in ('sc \\%server% qc %service% ^| find /i "DEPENDENCIES"') do (
set depservice=%%i
rem removes spaces
set depservice=!depservice: =!
sc \\%server% stop "!depservice!"
rem extra: accumulate all dependencies to one variable
set alldeps=!alldeps!, !depservice!
rem remove first ", " in variable
set alldeps=!alldeps=~2!
)
sc \\%server% stop "%service%" && echo Both %service% and !alldeps! were stopped || echo Something went wrong stopping %service%
exit /b
The above will only work if the service that you want to stop only has one dependency.
To anyone experiencing similar problems:
It's important to remember that the SC command is asynchronous. The following may help:
Halt batch file until service stop is complete?
Niklas batch file doesn't work for me.
It appears that on Windows Server 2008 R2, the qc command shows the services that this service depends on. They are not relevant at this point, you can stop the service without causing a ripple in their life.
What you actually want are the services that depend on the service being killed. You get those with the EnumDepend command to sc.exe.
Unfortunately, the output syntax is quite a bit different, so you'll need to preserve the logic shown above but replace the parsing.

Resources