Solved

Batch File is program running

Posted on 2013-06-06
11
497 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 83

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
 
LVL 68

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 37

Expert Comment

by:Gerwin Jansen
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 68

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now