Solved

Batch File is program running

Posted on 2013-06-06
11
503 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

730 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