Scheduled batch script stuck on running - batch-file

I have a script while ran from a CMD runs normally, however when I run it from task scheduler although it does everything as intended, the script status hangs on "Running"
Here is the script:
#echo OFF
tasklist /FI "IMAGENAME eq utorrent.exe" 2>NUL | find /I /N "utorrent.exe">NUL
if "%ERRORLEVEL%"=="0" (echo UTorrent is running nothing to do) ELSE (
echo UTorrent is not running, starting Utorrent!
start C:\Users\Adonis\AppData\Roaming\uTorrent\uTorrent.exe
)
tasklist /FI "IMAGENAME eq steam.exe" 2>NUL | find /I /N "steam.exe">NUL
if "%ERRORLEVEL%"=="0" (echo Steam is running nothing to do) ELSE (
echo Steam is not running, starting Steam!
start X:\Games\SteamLibrary\Steam.exe
)
exit
Can anyone advise why this is happening? IE why is the script stuck in running state through scheduler?
The OS in question is windows 8.
It is set to run only when user is logged on and with highest privileges.
Thanks!

Try to launch the application with the /B parameter to don't stop the execution of he script.
Also I've improved the syntax of your code:
#echo OFF
(Tasklist /FI "IMAGENAME eq utorrent.exe" | find /I "utorrent.exe">NUL && (
Echo: UTorrent is running nothing to do)
) || (
Echo: UTorrent is not running, starting Utorrent!
Start /B "" "C:\Users\Adonis\AppData\Roaming\uTorrent\uTorrent.exe"
)
(Tasklist /FI "IMAGENAME eq steam.exe" | find /I "steam.exe">NUL && (
Echo: Steam is running nothing to do)
) || (
Echo: Steam is not running, starting Steam!
Start /B "" "X:\Games\SteamLibrary\Steam.exe"
)
Exit

Related

How can I make a batchfile that constantly checks if a program is open and if it is not, start it minimized

I am fairly new to coding and batch files, so bare with me.
The program I want to start that way is opera. But the batch file doesnt seem to find it. This is how far I got:
tasklist /FI "opera.exe" 2>NUL | find /I /N "opera.exe">NUL
if NOT "%ERRORLEVEL%" == "0" start "" "C:\Users\leonv\AppData\Local\Programs\Opera.exe"
PAUSE
I would take the verification check, just a little bit further. I would look to see if an ImageName of Opera.exe, with a Status of Running, and for the current UserName is returned:
#%SystemRoot%\System32\tasklist.exe /Fi "Status Eq Running" /Fi "ImageName Eq Opera.exe" /Fi "UserName Eq %UserDomain%\%UserName%" | %SystemRoot%\System32\find.exe "="
#If ErrorLevel 1 Start "" /Min "%LocalAppData%\Programs\Opera.exe"
You need to specify IMAGENAME eq processname like:
tasklist /fi "IMAGENAME eq opera.exe"
Additionally, no need to run if statements, you can use conditional operators && and ||
(tasklist /fi "IMAGENAME eq opera.exe"| findstr /I "opera.exe")>nul && echo It's running || start "" "%LocalAppData%\Programs\Opera.exe"
and to run it in a loop, checking every N of seconds:
#echo of
:loop
(tasklist /fi "IMAGENAME eq opera.exe"| findstr /I "opera.exe")>nul && echo It's running || start "" "%LocalAppData%\Programs\Opera.exe"
timeout /t 20
goto :loop

Batch script to tell if process is running or not

I am very new to batch files and am trying to create a script that checks if a process is running and alert if the process is/is not running. This is what I have gotten from google, but isn't working the way I am wanting.
tasklist /FI "IMAGENAME eq example.exe" 2>NUL | find /I /N "example.exe">NUL if "%ERRORLEVEL%"=="0" echo Program is running
If this is batch (as the attempt from the question indicates), according to [MS.Docs]: Using multiple commands and conditional processing symbols:
&& [...]
command1 && command2
Use to run the command following && only if the command preceding the symbol is successful. Cmd.exe runs the first command, and then runs the second command only if the first command completed successfully.
Example:
C:\>(tasklist /FI "IMAGENAME eq svchost.exe" 2>NUL | findstr /I /N "svchost.exe" >NUL) && (echo Program running)
Program running
C:\>(tasklist /FI "IMAGENAME eq svchost1.exe" 2>NUL | findstr /I /N "svchost1.exe" >NUL) && (echo Program running)
You are trying to over complicate it. Copy the below into a batch file. Also use findstr instead.
tasklist /fi "imagename eq example.exe" | findstr /i "example.exe" >nul
If %errorlevel%==0 echo example.exe running.
If %errorlevel%==1 echo example.exe Not running.
This is even less complicated, there's no need to filter by ImageName when you're using Find to filter too:
TaskList|Find /I "example.exe">Nul&&(Echo Running)||Echo Not running

Make a .batch file to start/stop a program?

I need a batch (.bat) file that opens a program if it's not open, and stops the program if it is open. I have a game where when the launcher is closed, it stays open in the background. And I have to end it with task manager or else I can't launch it because steam doesn't like it when an app is open two times (it doesn't allow it), so I would like a batch file that does this for me, then bind it to a macro.
To check if your programm is running : (Here an example with notepad.exe)
#echo off
Set "MyProcess=Notepad.exe"
tasklist | find /i "%MyProcess%">nul && echo %MyProcess% Is running || echo %MyProcess% Is not running
So you can do like that :
#echo off
Set "MyProcess=Notepad.exe"
tasklist | find /i "%MyProcess%">nul && Taskkill /F/IM "%MyProcess%" || start "%MyProcess%"
This is another way to do it:
#echo off
tasklist /fi "imagename eq Launcher.exe" |find "." > nul && taskkill /f /im "Launcher.exe" & goto :EOF
tasklist /fi "imagename eq Launcher.exe" |find "." > nul || start "" steam://rungameid/243870 & goto :EOF
So I figured this out:
#echo off
tasklist /fi "imagename eq Launcher.exe" |find ":" > nul
if errorlevel 0 taskkill /f /im "Launcher.exe" && exit
start steam://rungameid/243870
exit
The logic behind it is that if you can kill the process, then end the prompt before you can create a new session of the process. But if the process can't be killed, then make a new session of it, then end the prompt.

Need help adding not responding check to batch file

I am in need of help to adding a not responding check to my .bat and restarting a server application when it is not responding. This is what I have so far.
#echo on
tasklist /nh /fi "imagename eq javaw.exe" | find /i "javaw.exe" >nul && (
echo Server is running.
//
I need a not responding check right afterward here. If the program is not responding,
taskkill /f /IM server.exe
taskkill /f /IM javaw.exe
cd C:\Users\Administrator\Desktop
serverstart.jar
else
exit
//
exit
) || (
echo Server process not found.
taskkill /f /IM server.exe
taskkill /f /IM javaw.exe
cd C:\Users\Administrator\Desktop
serverstart.jar
)
pause>nul
It is necessary for me to kill both of these process because it doesn't end eachother when it crashes apparently.
You already managed to filter by imagename.
You can also filter by Status (Running / Not Responding / Unknown):
tasklist /nh /fi "imagename eq java.exe" /fi "status eq not responding"
will give you any java.exe, which does not respond.
EDIT
well - when I think about it: I would try:
tasklist /nh /fi "imagename eq javaw.exe" /fi "status eq running` |find /i "javaw.exe" >nul && (
echo Server is running
rem nothing to do
) || (
echo Server is not running or not responding
rem restart service
)

How to run an EXE when another exe is running?

How do I make an exe run automatically when another exe is launched?
Example :
EXE 1 is launched & running
EXE 2 will run automatically once it see that the EXE 1 has already launched.
What is the command do I use in .bat file or autorun.inf?
Here you go
start exe1.exe
:LOOP
tasklist /nh /fi "imagename eq exe1.exe" | find /i "exe1.exe" >nul && start exe2.exe || goto :LOOP
That will start exe1.exe and will keep looking to see if it has started before running exe2.exe.
#ECHO OFF
START /D "path" myexe1.exe
START /D "path" myexe3.exe
Taken from here.
Then save the file as "somename.bat"
Edit
Due to your last comment here's what I found
#echo off
tasklist /nh /fi "imagename eq chrome.exe" | find /i "chrome.exe" >nul && (
echo Windows Media Player is running
) || (
echo Chrome is not running
)
pause>nul
I just copied the code from the link, google can be very helpful.

Resources