Solved

Batch File is program running

Posted on 2013-06-06
11
501 Views
Last Modified: 2013-08-07
Dear Experts,

I have been struggling with a batch file for a while now and I was wondering if you can help.
I need to find out if an application is running. The application is a C# console app housed in the folder

C:/Program Files/CustomApps/Portal.Console.Integrate.exe

I have the following .bat file which sort of works.

@echo off
tasklist /FI "IMAGENAME eq Portal.Console.Integrate.exe" 2>NUL 
if "%ERRORLEVEL%"=="0" (
    echo Program is Running
) else (
    echo Program is not running
)
pause

Open in new window


However, the error level always return as 0 irrespective of the application running or not?
It does detect the process is running and prints out the process info and it also detects if the app isn't running but still prints "Program is running"

Can anyone help?
0
Comment
Question by:S-Unit
11 Comments
 
LVL 84

Assisted Solution

by:oBdA
oBdA earned 250 total points
ID: 39225215
Pipe the result through "find":
@echo off
tasklist /FI "IMAGENAME eq Portal.Console.Integrate.exe" 2>NUL | find /i "Portal.Console.Integrate.exe"
if "%ERRORLEVEL%"=="0" (
    echo Program is Running
) else (
    echo Program is not running
)
pause

Open in new window

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39225232
tasklist doesn't return an errorlevel as you have found.  You could run it through find,e.g.

@echo off
tasklist /FI "IMAGENAME eq Portal.Console.Integrate.exe" 2>NUL | FIND "Portal.Consol.Integrate.exe"
if "%ERRORLEVEL%"=="0" (
    echo Program is Running
) else (
    echo Program is not running
)
pause

Open in new window


orusing && to run if errorlevel is 0 and || otherwise:

@echo off
tasklist /FI "IMAGENAME eq Portal.Console.Integrate.exe" 2>NUL | FIND  "Portal.Consol.Integrate.exe" && (
  echo Program is Running
) || (
    echo Program is not running
)
pause

Open in new window


Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39225236
Typing at the same time there, sorry oBdA
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 69

Accepted Solution

by:
Qlemo earned 250 total points
ID: 39225262
It is not very useful having to repeat the exe name in find, so I would tend to search for something common instead (and dismiss the ouput of find):
@echo off
tasklist /FI "IMAGENAME eq Portal.Console.Integrate.exe" 2>NUL | FIND  "=" >nul && (
  echo Program is running
) || (
    echo Program is not running
)
pause

Open in new window

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39225284
True... can lead to typos like my typo of his exe name!

Steve
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 39225617
A findstr alternative:
@echo off
tasklist | findstr /i %1 2>nul 1>nul && (
  echo Program %1 is Running
) || (
  echo Program %1 is not running
)
pause

Open in new window

Save as checkprg.cmd and call with program name to check between "", like this:

checkprg.cmd "Portal.Console.Integrate"
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39226146
gerwinjansen,

That has several disadvantages:
Not that it matters, but findstr will read a dot as "arbitrary character" (one-char wildcard). Adding /L will prevent that.
"One two" as parameter will find hits of "one" or "two", instead doing an exact match. We should use /C:"%~1" instead of %1 for that reason.
findstr is (usually) slower than find, because it tries to apply regular expressions.
In general, the integrated filter mechanism of utilities should be used, instead of doing it with a pipe. It doesn't matter in most cases, but it is good to have that in mind at all times.
There is no reason to use findstr here ;-).
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 39233097
S-Unit

You were almost correct... A simple approach is as follows:

@echo off

tasklist | find /i "Portal.Console.Integrate.exe" >NUL

if ERRORLEVEL 1 (
    echo Program is not Running
) else (
    echo Program is running
)

pause

Open in new window

0
 

Author Comment

by:S-Unit
ID: 39388673
I've requested that this question be deleted for the following reason:

Experts,

Sorry for the non response. Unfortunately the problem I was working on doesn't exist anymore. It was pulled and therefore I didn't have the opportunity to test any of the solutions as I was "redeployed"
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39388674
I would say to PAQ it because some useful techniques there for anyone else searching...
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Scheduled task not running 5 80
how to blend these txt lines in a random way? 8 49
xcopy loop 13 61
Command prompt output to variable? 9 59
Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question