Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am looking for a software or BAT/CMD script that could check if a program's instance is running or not. If the software or BAT/CMD script detects that the program is not running, it should run that program, else it should not do anything.

Note: The program I'm gonna be running (uTorrent) doesn't have detection to check if an instance of itself is already running, before running itself.

I am asking this because I am currently using a software (Networx) which runs uTorrent if it detects that my internet connection's bandwidth has not been used for some time (in my case I set it to run uTorrent if it detects that 50 KB or less has been downloaded and uploaded within a 5 minute-period).

However, it doesn't have a feature to check if a program is already running before calling it. If it runs uTorrent while uTorrent is already running, uTorrent's window goes to the front of the screen, stealing window focus, which could be distracting if you are working on a window and do not want another window going to the front of the screen.

The uTorrent /MINIMIZED argument only works correctly if uTorrent is not already running.

Or does anyone know of another software that can do what the software I'm currently using does, except that it will check if uTorrent is already running, before calling it?

Edit: I would like to add that it is preferable if the software or BAT/CMD script runs minimized or hidden and not go to the foreground or steal window focus. Also, the software or BAT/CMD script should exit itself after it has done its task.

share|improve this question
    
SO to the rescue: stackoverflow.com/questions/162291/… –  Craig H Apr 1 '11 at 6:16

2 Answers 2

up vote 3 down vote accepted

Something like this, perhaps?

tasklist | find "programname.exe"
if errorlevel 1 c:\path\to\programname.exe
share|improve this answer
    
Is it correct if I put this in a .CMD file, and not a .BAT file? Also, is there a way to run this CMD/BAT file minimized (window shouldn't appear on the foreground or steal focus when run)? –  galacticninja Apr 1 '11 at 7:34
    
It'll function in either a .bat or .cmd file. After tinkering a bit, I think you'll probably want to use the start command like so: if errorlevel 1 start "" "c:\path to\programname.exe" so that the batch will terminate rather than linger. If your launcher program can launch a task minimized, the command interpreter shouldn't have a problem with it. –  Jason Sherman Apr 1 '11 at 15:05
    
Turns out I could make a shortcut to the .BAT file and just set it to run 'Minimized' in Properties and run that shortcut instead. I also would like to ask: what is the proper syntax if I would like to run my program with the /MINIMIZED argument (this runs uTorrent minimized) –  galacticninja Apr 3 '11 at 4:14

It is almost certainly possible to do in a batch file but (like anything non-trivial) it won't be easy.

However PowerShell makes this easy:

if (-not (Get-Proceess "Name")) {
  #run new instance here
}

where Name is replaced by the process name (this is usually the file name without .exe).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.