Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Batch File is program running

Posted on 2013-06-06
11
Medium Priority
?
510 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 1000 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 71

Accepted Solution

by:
Qlemo earned 1000 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 71

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

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…
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Screencast - Getting to Know the Pipeline
Suggested Courses

597 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