How to kill a service in a batch file every 20 seconds? - batch-file

A task called "FireSvc.exe" (McAffee service) keeps interfering with our app upgrades. I put
> taskkill /f /im "FireSvc.exe"
in a batch file. We continuously run this during installs so the software will successfully upgrade. I'm not positive why this service starts back so often. We have to run from the command line, because in the task manager you get "access denied" when trying to kill this task.
My question is, how would you make this run every 20-30 seconds?
We cannot install any type of non-approved software either. So, theres that...
Thanks for any input.

Here's what we use:
:runagain
taskkill /f /im "FireSvc.exe"
timeout /T 5
goto runagain

You can use vbscript's sleep command in a batch file with cscript like so...
First create a vbscript file (.vbs extension) that contains the following code (don't forget to save it with ANSI encoding otherwise it won't work):
Wscript.sleep 2500
Wscript.exit
Create a batch file in the same directory with this code:
#echo off
:Kill
cscript //NoLogo file.vbs
taskkill /f /im "FireSvc.exe"
GoTo Kill
I currently don't have access to a PC to check if it works so let me know what happens. I still think there might be a cleverer alternative... cheers!
Edit: Btw you can also simulate a sleep command with the ping command like so:
ping localhost -n 1 -w 2500 >nul
And if you are using windows vista or above you can use the timeout command.

Related

Killing a Batch script from another

I have a script that will call other scripts to run then go back to process.
Example:
Call a script to ping and when it completes, go back to script.
My issue is I have a script called Bounce.bat
Here is the code:
#Echo off
set IPADDRESS=127.0.0.1
set INTERVAL=60
:PINGINTERVAL
ping %IPADDRESS% -n 1
timeout %INTERVAL%
GOTO PINGINTERVAL
What I need is the command that will kill the Bounce.bat from inside another batch script.
I was going to try killing CMD but that kills the original script as well.
I Have tried experimenting as well as looking at script examples.
Does anyone know what is needed to kill this bat only?
You can use the Command "taskkill" to stop other processes in Windows.
You can write something like this in your other batch-file:
taskkill /IM MYBATCHFILENAME.bat
You can force to kill the process, if you are using the parameter /f at the end of your command.

Need Batch file help searching for specific string being created in another rolling open session?

Thanks. Thought I'd try writing a batch file to kill another open cmd session that is constantly open churning out lots of scrolling info.
I got a bit carried away and am now outta my league so to speak.
Basically I am trying to search for "£000.00" within a each emerging line of tet that appears in the other running open command window. The running command session is named in the window as C:\windows\system32\cmd.exe but is does have a valid .exe process name in task manager while running and open.
The batch file code below is as far as I've got.
The idea is that when the above string is found in the other process that process/program get closed down them re-launched.
This is as far as I've got.
#echo off
#echo off
title Shut down other process and relaunch
:loop
start /d "C:\Users\Desktop" ActiveDOSprogram.exe
:: #setlocal enabledelayedexpansion
:: #echo off
:: set stringfound=1
find /c "*£000.00*" C:\Windows\system32\cmd.exe && (echo found %date% %time%:~0,-3% >> "%USERPROFILE%\Desktop\Crash_Report.txt"
taskkill /IM ActiveDOSprogram.exe /F
timeout /t 20
start /d "C:\Users\Desktop" ActiveDOSprogram.exe
goto loop
So when I tried this without any variables and in a loop and I think i nearly blew my PC!
Reason I'm stuck is I'm really a novice at this (but trying) and I got as far as I think I need a variable in there somewhere, that only move's to the next line (taskkill+restart) when £000.00 is found in the other process.
Thanks
wingman

Run Batch Command Without Checking For Confirmation

I have a batch file that restarts several computers in my center. The batch file works fine but I would like if it ran faster. The reason it is slow is when it runs into a computer that has been turned off, it continues to try to find that computer for about 15 seconds before it moves to the next one.
Can I lower the time a batch command looks for the computer or just have it send the command and move on to the next?
I have pasted my current batch command below:
shutdown /f /r /m \\VAMAR-3STHWV1 /t 000
With the ping command I believe you can do it. By telling the ping command to only send one echo request and then &&ing the result with the shutdown command, something like:
ping VAMAR-3STHWV1 -n 1 >nul && shutdown /f /r /m \\VAMAR-3STHWV1 /t 000
That way the shutdown command gets executed only when the ping successfully reached out VAMAR-3STHWV1 in one echo request.

Why do not all started applications save the wanted information in the text files as expected?

I am trying to create a batch file on a USB drive that will run an .exe, save its text to a .txt file, and then close the .exe.
I am currently running into a weird problem, only 5 of the 18 .exe's are actually saving their text to a file.
This is the convention I am using to complete my task:
start IEPassView.exe /stext Results/IEPassView.txt
taskkill /f /im IEPassView.exe
start MailPassView.exe /stext Results/MailPassView.txt
taskkill /f /im MailPassView.exe
start MessenPass.exe /stext Results/MessenPass.txt
taskkill /f /im MessenPass.exe
start RouterPassView.exe /stext Results/RouterPassView.txt
taskkill /f /im RouterPassView.exe
start ProtectedStoragePassView.exe /stext Results/ProtectedStoragePassView.txt
taskkill /f /im ProtectedStoragePassView.exe
start DialUpPassView.exe /stext Results/DialUpPassView.txt
taskkill /f /im DialUpPassView.exe
I have 18 of the above blocks in a row all calling different small programs and even though 5 of them actually save the files none of them save a .cfg file as they sometimes do. Any help would be greatly appreciated, thanks.
There are mainly 3 different types of executables:
A console application is reading from stdin or a file and writing to stdout or a file and outputs error messages to stderr.
The processing of a batch file is halted on starting a console application until the console application terminated itself. The correct term is therefore: calling a console application.
The exit code of the console application is assigned to environment variable ERRORLEVEL and can be also directly evaluated for example with if errorlevel X rem do something
Many *.exe in System32 directory of Windows are such console applications, like find.exe, findstr.exe, ping.exe, ...
A GUI (graphical user interface) application is started as new process which means the Windows command processor does not halt batch processing until the GUI application terminates itself.
It is not easily possible to get something from within a command process from such applications. GUI applications are designed for interacting via a graphical user interface with a user and not via standard streams or files with a command process.
A typical example for such applications is Windows Explorer explorer.exe in Windows directory.
A hybrid application supports both interfaces and can be therefore used from within a command process as well as by a user via GUI.
Hybrid applications are rare because not easy to code. The behavior of hybrid applications on usage from within a batch file must be find out by testing.
Example for such applications are Windows Registry Editor regedit.exe or shareware archiver tool WinRAR WinRAR.exe.
It is best to look on simple examples to see the differences regarding starting/calling all 3 types of applications from within a batch file.
Example for console application:
#echo off
cls
%SystemRoot%\System32\ping.exe 127.0.0.1 -n 5
echo.
echo Ping finished pinging the own computer (localhost).
echo.
pause
The command processing is halted until ping.exe terminated itself which takes 4 seconds. There is no need for start or call for such applications, except the console application should be intentionally executed in a separate process.
Example for a GUI application:
#echo off
cls
%WinDir%\Explorer.exe
echo.
echo Windows Explorer opened and is still running!
echo.
pause
This batch file outputs the text and message prompt to press any key immediately after starting Windows Explorer indicating that Windows Explorer was started as separate process and Windows command processor immediately continued on the next lines of the batch file. So although Windows Explorer was started and is still running, the batch processing continued, too.
Example for a hybrid application:
#echo off
cls
"%ProgramFiles%\WinRAR\WinRAR.exe"
echo.
echo User exited WinRAR.
echo.
pause
This batch file starts WinRAR without any parameter (usually not useful from within a batch file) if being installed at all in default installation directory. But batch processing is halted until the user exited WinRAR for example by clicking on X symbol of the WinRAR´s application window.
But opening a command prompt window and executing from within the window
"%ProgramFiles%\WinRAR\WinRAR.exe"
results in getting immediately the prompt back in command window to type and execute the next command. So WinRAR finds out what is the parent process and acts accordingly.
Windows Registry Editor shows the same behavior. Executing from within a command prompt window
%WinDir%\regedit.exe
results in opening Windows Registry Editor, but next command can be immediately entered in command prompt window. But using this command in a batch file results in halting batch processing until GUI window of Windows Registry Editor is closed by the user.
Therefore hybrid applications are used from within a batch file mainly with parameters to avoid the necessity of user interaction.
Okay, back to the question after this brief lesson about various types of applications.
First suggestion is using
Result\TextFileName.txt
instead of
Result/TextFileName.txt
as the directory separator on Windows is the backslash character, except the executables require forward slashes as directory separator because of being badly ported from Unix/Linux to Windows.
Second suggestion is finding out type of application. Is command start really necessary because the applications don't start itself in a separate process and need user interaction to terminate itself?
Note: Command start interprets first double quoted string as title string. Therefore it is always good to specify as first parameter "" (empty title string) on starting a GUI or hybrid application as a separate process. On starting a console application as a separate process it is in general a good idea to give the console window a meaningful title.
And last if the started applications really would need user interaction to terminate, it would be definitely better to either start and kill them after waiting 1 or more seconds between start and kill or start them all, wait a few seconds and finally kill them all at once.
Example for first solution with starting and killing each application separately:
#echo off
setlocal
set TimeoutInSeconds=3
call :RunApp IEPassView
call :RunApp MailPassView
call :RunApp MessenPass
call :RunApp RouterPassView
call :RunApp ProtectedStoragePassView
call :RunApp DialUpPassView
endlocal
goto :EOF
:RunApp
start "" "%~1.exe" /stext "Results\%~1.txt"
set /A RetryNumber=TimeoutInSeconds + 1
%SystemRoot%\System32\ping.exe 127.0.0.1 -n %RetryNumber% >nul
%SystemRoot%\System32\taskkill.exe /f /im "%~1.exe"
goto :EOF
It is also possible to use timeout instead of ping for the delay if the batch file is only for Windows Vista and later Windows versions.
Example for second solution with starting all applications, wait some seconds and kill them finally all:
#echo off
call :StartApp IEPassView
call :StartApp MailPassView
call :StartApp MessenPass
call :StartApp RouterPassView
call :StartApp ProtectedStoragePassView
call :StartApp DialUpPassView
%SystemRoot%\System32\ping.exe 127.0.0.1 -n 6 >nul
call :KillApp IEPassView
call :KillApp MailPassView
call :KillApp MessenPass
call :KillApp RouterPassView
call :KillApp ProtectedStoragePassView
call :KillApp DialUpPassView
goto :EOF
:StartApp
start "" "%~1.exe" /stext "Results\%~1.txt"
goto :EOF
:KillApp
%SystemRoot%\System32\taskkill.exe /f /im "%~1.exe"
goto :EOF
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
echo /?
endlocal /?
goto /?
ping /?
set /?
setlocal /?
start /?
taskkill /?
See also the Microsoft article about Using command redirection operators.
PS: The last two batch code blocks were not tested by me because of not having available the applications.
I suggest killing all tasks at once, at the end of the very end, possibly after a timeout command with a amount of time appropriate to your system's speed. That may help the issue.

How to run multiple programs using batch file

I like to run two programs using batch file, but the condition is, the second program must start only after the first program loaded, so is there any way to control using timer to control when the program starts.
I needed the same thing, and found out that following thing works as hoped:
start D:\Michal\Xming\Xming.exe -multiwindow
start D:\Michal\Xming\putty.exe
start D:\Michal\WinSCP\WinSCP.exe
And it all is saved in file Login.bat.
BTW, I am running Win7 but I doubt that this has any impact.
Basically, you could try this approach (not tested):
Run the first program using the start command.
Check the task list in a loop to see if the program has appeared there.
Impose some time limitation to the said loop.
Run the next program in case of success, exit with notification otherwise.
The scripting might look like this:
#ECHO OFF
START program1.exe
FOR /L %%i IN (1,1,100) DO (
(TASKLIST | FIND /I "program.exe") && GOTO :startnext
:: you might add here some delaying
)
ECHO Timeout waiting for program1.exe to start
GOTO :EOF
:startnext
program2.exe
:: or START program2.exe
Keep in mind that the timing is not precise, especially if you are going to insert delays between the task list checks.
I think this might be irrelevant here but would like share the following:
I 've created the following batch file and run it whenever I open my laptop in the office to open relevant programs at a single click.
Kept this file at Desktop and created a folder where I put all shortcuts for relevant programs.
So, I run these shortcuts in the batch file as follows:
#ECHO off
start C:\Users\User1\Desktop\Softwares\IE
start C:\Users\User1\Desktop\Softwares\Googletalk
start C:\Users\User1\Desktop\Softwares\YahooMessenger
start C:\Users\User1\Desktop\Softwares\Program4
start C:\Users\User1\Desktop\Softwares\Program5
I have also find a small hack to do it, just using a ping command with -n switch as follows:
start /d "C:\Program Files (x86)\Mobile Partner\" MobilePartner.exe
ping 127.0.0.1 -n 8
start /d "F:\Other Applcations\System Tools\OS Tweak\" dragfullwindows.exe
I wrote this answer as I am on Windows 10 and it is 2021.
And this provides a few more ideas for newage programs, that should run in the taskbar(in the background).
Here is my "Work Start.bat" batch file sitting on my desktop:
rem Work Start Batch Job from Desktop
rem Launchs All Work Apps
#echo off
rem start "Start VS Code" "C:\Users\yourname\AppData\Local\Programs\Microsoft VS Code\code.exe"
start "Start OneDrive" "C:\Users\yourname\AppData\Local\Microsoft\OneDrive\OneDrive.exe"
start "Start Google Sync" "C:\Program Files\Google\Drive\GoogleDriveSync.exe"
start "Start Clipboard" "C:\Program Files\Beyond Compare 4\BCClipboard.exe"
start "Start Cisco AnyConnect" "C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe"
start chrome
start firefox
start skype
start "Start Teams" "C:\Users\yourname\AppData\Local\Microsoft\Teams\current\Teams.exe" /MIN
start Slack
start Zoom
sleep 10
taskkill /IM "explorer.exe"
taskkill /IM "teams.exe"
taskkill /IM "skype.exe"
taskkill /IM "slack.exe"
taskkill /IM "zoom.exe"
taskkill /IM "cmd.exe"
#echo on
Some Apps would not start with a simple "start app" command, so I used the full path.
For some reason some were found in my user appdata folder and not in program files, I do not understand this behaviour of program storage, it makes no sense.
I used a time delay so that the apps could fully start before I sent them to the background using taskkill command
I killed explorer.exe because OneDrive opens explorer
I killed cmd.exe because it opened and stayed opened due to badly behaving apps.
The rest I killed so that they would just move to the background.
Here is my "Work End.bat" batch file to forceably close everything:
rem Work End Batch Job from Desktop
rem Forcibly Closes All Work Apps
#echo off
taskkill /f /IM OneDrive.exe
taskkill /f /IM GoogleDriveSync.exe
taskkill /f /IM BCClipboard.exe
taskkill /f /IM "vpnui.exe"
taskkill /f /IM "chrome.exe"
taskkill /f /IM "firefox.exe"
taskkill /IM "explorer.exe"
taskkill /f /IM "teams.exe"
taskkill /f /IM "skype.exe"
taskkill /f /IM "slack.exe"
taskkill /f /IM "zoom.exe"
#echo on
I do have to ensure I have saved all my work, and that files are no longer syncing.
Possibly I will need a batch file that kills everything except file sync.
The good thing about forceably killing Chrome and firefox is they ask to be restored on next start, so I can continue where I left off, assuming I saved everything.
If I don't forceably kill these they stay in the background, if I close using the Cross they do not offer me to start from where I left off.
I could then start my gaming files, in another batch file but this would be similar to the first file.
Now I can turn off the default "Start Up" Apps and use this "Work Start.bat" , because the Start Up Apps annoy me when I start my pc just to game.

Resources