?
Solved

Batch script - If process exists then exit

Posted on 2012-08-30
3
Medium Priority
?
4,372 Views
Last Modified: 2012-09-04
Hey Experts,

I have tried to create a batch script, but using the tasklist, it gives me processes for all users, this won't work.

I need to run a per user script on a terminal server, here is what I need:
I need a batch file that will check to see if a process if running for that user. If exists, then exit. If it doesn't exists, then start....

Here is what I have thus far:


@echo off
 
if tasklist /FI "IMAGENAME eq CPOPM06.exe" | find /i "CPOPM06.exe" && set PROCEXIST=YES
 
else
 
set %PROCEXIST%=NO
 
goto IFSTATE
 
:IFSTATE
if /I "%PROCEXIST%"=="YES" goto :EXEC
if /I "%PROCEXIST%"=="NO" goto EXIT
 
:EXEC
start http://servername/centricityps
exit

:EXIT
exit
0
Comment
Question by:Glenn M
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
oBdA earned 2000 total points
ID: 38350976
The logic you've implemented in the script is the reverse of what you're describing in your question; the script (if it would work) would start the browser if the process exists.
This one starts the browser if the process is not found, and makes sure that only the user's processes are queried:
@echo off
setlocal
tasklist.exe /FI "IMAGENAME eq CPOPM06.exe" /FI "USERNAME eq %UserName%" | find /i "CPOPM06.exe" >NUL
if errorlevel 1 (
	set ProcExists=NO
) else (
	set ProcExists=YES
)
if /i "%ProcExists%"=="NO" (
	start http://servername/centricityps
)

Open in new window

0
 
LVL 2

Assisted Solution

by:Glenn M
Glenn M earned 0 total points
ID: 38351007
I figured it out:


@echo on
 
tasklist /FI "USERNAME ne NT domain\%username%" /FI "STATUS eq running" /FI "IMAGENAME eq CPOPM06.exe" | find /i "CPOPM06.exe" && set PROCEXIST=YES
 
IF ERRORLEVEL 2 GOTO FoundCPS
IF ERRORLEVEL 1 GOTO StartCPS
 
:FoundCPS
goto EXIT
 
:StartCPS
start http://servername/centricityps/cps/
goto EXIT
 
:EXIT
Exit
0
 
LVL 2

Author Closing Comment

by:Glenn M
ID: 38362890
Both scripts accomplish the goal!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…

569 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