Solved

Batch File is program running

Posted on 2013-06-06
11
505 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 85

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
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 
LVL 70

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 70

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

729 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