how to taskkill an input output process - batch-file

Quite new to batch script, please help me out.
set Pathname="C:\S3Sync"
cd %Pathname%
S3Sync.exe -
timeout /t 10
taskkill /im S3Sync.exe
I want to run the following process and if it stops or goes into an infinite time period, kill the process after 10 seconds and also use try and catch to get the error. what are the possibilities of using such statement?

These commands may help you: Put them after the timeout command
set err=%errorlevel%
tasklist |find /i "s3sync.exe" >nul && taskkill /f /im S3Sync.exe
If your task hangs then the errorlevel will not be set correctly, of course.

Related

How to make a .bat file check for a program and relaunch if it isn't open, also restarting the program if it's been running for x amount of time?

This can be two separate .bat files if needed, but I would prefer if it's possible on one.
I need to run a certain program, but it sometimes crashes.
I have this for a .bat so far.
It works as far as resetting the program every hour, but it does crash sometimes in between restarts.
I'd like to just have a check, so it launches if the program isn't found.
Is that possible?
#echo off
:loop
start "xx" "xxpath"
timeout /t 3600 >null
taskkill /f /im "xx" >null
timeout /t 4 >null
goto loop
Here is a sample batch that can check if any instance of chrome.exe is running or not
If not, we start it !
#echo off
Color 0A
Set WaitTimeSeconds=20
Set App_Path=C:\Program Files\Google\Chrome\Application\chrome.exe
for %%i in (%App_Path%) do set App_Name=%%~nxi
Title Checking if any "%App_Name%" instance is running ...
:Loop
Rem Clear the screen
cls
Rem Kill any application that have a status equal to not responding !
Taskkill /f /fi "status eq not responding">nul 2>&1
Rem Check if any application instance is running ; if not we start it !
TASKLIST | FINDSTR %App_Name% || START "" "%App_Path%"
Timeout /t %WaitTimeSeconds% /nobreak>nul
Goto Loop

Batch script to find if program is not responding and then start another application else exit batch job

I want a batch script that checks if the program is in running or not responding state. If it is in running state exit the batch script, else kill the program start the other application.
I tried using the following code but it is executing opening application even though the program is in running state. Please help me in finding a solution
#echo off
TASKLIST/IM jusched.exe /FI "STATUS eq NOT RESPONDING">nul /T /F && start "" notepad.exe
You appear to be mixing up the TaskKill and TaskList syntax. (Enter TaskList/? and TaskKill/? at the command prompt for usage information). Also I think TaskList output is always successful so it would probably need to be checked using something which does register if unsuccessful, e.g. Find.exe.
Here is a basic step by step example for you.
#Echo Off
Rem Setting name of target process
Set "TP=jusched.exe"
Rem Setting name of new process
Set "NP=notepad.exe"
Rem Setting TaskList filters to reduce line length
Set "F1=ImageName eq %TP%"
Set "F2=Status eq Not Responding"
Rem Check target process status and exit if no match
TaskList /FI "%F1" /FI "%F2%" /NH|Find /I "%TP%">Nul||Exit/B
Rem Kill unresponsive process and wait a little
TaskKill /IM "%TP%" /T /F>Nul 2>&1&&Timeout 3 /NoBreak>Nul
Rem Open new process
Start "" "%NP%"

How to get pid in batch for cmd with parameters?

I'm noobie in MS batch (bat). I need to save some streaming via VLC to file.
This script working greate for me:
#echo off
"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe" "http://STREAM" --sout=#transcode{vcodec=h264,vb=1400,scale=1,acodec=mpga,ab=192,channels=2,deinterlace}:file{dst="D:\SOMEFILE.mp4"}
But I need to stop recording after N seconds. So I think the best way is to get pid of this proccess and run
timeout /t 120 /nobreak
taskkill /t /pid %PID%
For this I found some solution:
#echo off
set "exe=C:\Program Files (x86)\VideoLAN\VLC\vlc.exe"
set "source=http://STREAM"
set "save-cmd=--sout=#transcode{vcodec=h264,vb=1400,scale=1,acodec=mpga,ab=192,channels=2,deinterlace}:file{dst="D:\SOMEFILE.mp4"}"
for /f "tokens=2 delims==; " %%a in ('wmic process call create '"%exe%" "%source%" %save-cmd%' ^| find "ProcessId"') do set PID=%%a
echo "%PID%"
timeout /t 120 /nobreak
taskkill /t /pid %PID%
I'm getting PID, But VLC openning in normal way (don't do anything), as I just run C:\Program Files (x86)\VideoLAN\VLC\vlc.exe
How can I run programm with parameters and get PID of it?
Thanks!
According to the VLC forums, you can use --run-time and --stop-time parameters to close VLC after a specified amount of time.
For example adding --run-time 120 --stop-time=120 vlc://quit will exit after 2 hours
Updated script:
#echo off
"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe" "http://STREAM" --sout=#transcode{vcodec=h264,vb=1400,scale=1,acodec=mpga,ab=192,channels=2,deinterlace}:file{dst="D:\SOMEFILE.mp4"} --run-time 120 --stop-time=120 vlc://quit

can I implement a kill switch into a .bat fork bomb?

I want to show a friend how big the impact of a fork bomb can be on performance, but without needing to restart the computer afterwards.
Assuming the following fork bomb:
%0|%0
Is there a way to add a kill switch to this which, with one button press, will stop all running copies of this file, stop any new from being created and hopefully save the machine? I'm not really familiar with the command prompt syntax, so I'm not sure.
Two ideas:
a) limit the depth your "bomb" is going to fork:
#echo off
set args=%*
if "%args%" EQU "" (set args=0) else set /a args=%args%+1
if %args% LSS 8 start /min thisfile.bat
(this will produce 2^9 -1 command windows, but only your main window is open.)
b) kill the cmd.exe process in the main batch file
#echo off
SET args=%*
:repeat
start /min thisfile.bat.bat some_arg
if "%args%" NEQ "" goto repeat
pause
taskkill /im cmd.exe
pressing any key in the initial batch file will instamntly kill all cmd windows currently open.
These solutions were tested with some restrictions, so if they work in a "hot" forkbomb, please let me know.
hope this helps.
EDIT:
c)implement a time switch in the original bat:
(still stops even if you can't get past pause)
#echo off
set args=%*
:repeat
start /min thisfile.bat some_arg
if "%args%" NEQ "" goto repeat
timeout /T 10
taskkill /im cmd.exe
or, using the smaller "bomb":
#echo off
set args=%*
if "%args%" NEQ "" (%0 some_arg|%0 some_arg) else start /min thisfile.bat some_arg
timeout /T 10
taskkill /im cmd.exe
If you're out to just show your friend fork bombs why not go for a silent but deadly approach? The fork bomb is in vbs, the cleanup is in batch.
Do note, the vbs fork bomb does nothing turning your pc off/on wont fix, it just floods your session proccess's.
The fork bomb:
Do until true = false
CreateObject("Wscript.Shell").Run Wscript.ScriptName
Loop
Source
Cleanup:
title=dontkillme
FOR /F "tokens=2 delims= " %%A IN ('TASKLIST /FI ^"WINDOWTITLE eq
dontkillme^" /NH') DO SET tid=%%A
echo %tid%
taskkill /F /IM cmd.exe /FI ^"PID ne %tid%^"
Source
If it runs in an accessible directory, you could try
IF EXIST kill.txt (exit /b) ELSE (%0|%0)
and make a file called kill.txt in the directory to stop the bomb.

How can I keep a bat file repeating constantly?

sorry if I am using wrong terms, I am new to this stuff. so I have been trying to get this command (taskkill /f /t /im (the process)) to keep repeating because the process that I am trying to kill keeps coming back. so I thought if i can get the command to keep repeating it will keep the process closed. please I would like some help, thank you.
Just loop through
#echo off
:loop
taskkill /f /t /im (the process)
goto loop
Or add a delay using ping or timeout
#echo off
:loop
taskkill /f /t /im (the process)
timeout /t 30 /nobreak >nul rem Change out 30 for the number of seconds needed in the delay
goto loop
As #MrLister said in the comments above, address the problem directly. Ask yourself "did i get this program from a reliable source?" if not, it could be malicious. If it is safe, then why is this behavior occurring? Could it be scheduled with taskscheduler? Best of luck.

Resources