Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a process is running or not?

I want to check if a process is running or not ? if the process is not running, then i execute it (in this example I took the calculator with process name = calc.exe) I started the batch script, but I have a syntax problem I believe !

@echo off
Set MyProcess=calc.exe
echo %MyProcess%
pause
for /f "tokens=1" %%i In ('tasklist /NH /FI "imagename eq %MyProcess%"') do set ff=%%i
echo %ff%
If /i %ff%==%MyProcess% (Echo %ff% est en cours d^'execution) Else (Start %MyProcess%)
pause
like image 984
Hackoo Avatar asked Dec 19 '22 15:12

Hackoo


2 Answers

Here's another way to do it using your code as a base:

@echo off
Set "MyProcess=calc.exe"
echo "%MyProcess%"
tasklist /NH /FI "imagename eq %MyProcess%" 2>nul |find /i "%MyProcess%" >nul
If not errorlevel 1 (Echo "%MyProcess%" est en cours d^'execution) else (start "" "%MyProcess%")
pause
like image 178
foxidrive Avatar answered Feb 07 '23 21:02

foxidrive


You may try like this:

tasklist /FI "IMAGENAME eq calc.exe" 2>NUL | find /I /N "calc.exe">NUL
if "%ERRORLEVEL%"=="0" 
echo Running
like image 44
Rahul Tripathi Avatar answered Feb 07 '23 22:02

Rahul Tripathi