Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

start - close - start -close a program script each 3 minutes

Posted on 2007-10-21
23
Medium Priority
?
316 Views
Last Modified: 2010-04-16
Hi,
Im looking for a script that when i run it,  
a program i specify should run - (get minimized not nessosory) and after 3 minutes it close and start again, and so on. until i stop the script.

is that possible?   i never made a script b4 so im not sure if this can be done or not
any suggestions are welcome

regards,
Kimos
0
Comment
Question by:kimos123
  • 12
  • 11
23 Comments
 
LVL 9

Expert Comment

by:MSE-dwells
ID: 20122869
What is the program you want to run?  
How is the program typically terminated?
Is it always the same?
0
 

Author Comment

by:kimos123
ID: 20125133
i want to run a program that i have, lets say that i just want to put the location of the .exe in the script.
the script should start and close the program every 3 minutes.
yes the program is always the same.  the program is typically terminated when clicking on the X, or when its in the system tray, then right mouse click and chosing exit.

i know it sounds stupid but i want to know if its possible
0
 
LVL 9

Expert Comment

by:MSE-dwells
ID: 20125207
It's certainly possible.  The more cumbersome piece is ensuring we kill the right process.  Is it likely or even possible that more than instance of this program will be running at once?
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 

Author Comment

by:kimos123
ID: 20125264
thank you for replaying  MSE-dwells
no its not possible to run more instance. how /where can i find/make such a script?
0
 
LVL 9

Expert Comment

by:MSE-dwells
ID: 20126104
Try this but note that you must replace the 'set RUNcommand' value with your desired program, you can provide just the filename assuming the program exists within the system's path.  

It's also worth mentioning that there's something [albeit limited] of a race-condition potential whereby the script could perceive the wrong PID for the process it started which will result in it killing another instance of your program other than the one it executed (it's unlikely though) -

:: SCRIPT
@echo off

setlocal ENABLEDELAYEDEXPANSION

echo/
echo + Working [enter an 'X' to abort and exit] ...
echo/

:: Define operating environment
set RUNcommand=mspaint.exe
set waitTIMER=180

:: Determine full path to specified executable
for %%e in ("%RUNcommand%") do (
      if "%%~$PATH:e"=="""" (
            echo # ERROR - executable not located within the path
            echo          = %%e
            goto :END
      ) else (
            set RUNcommand="%%~$PATH:e"
            set RUNcommand=!RUNcommand:"=!
      )
)

:: Verify that an instance is not currently running
wmic process get executablepath | find /i "%RUNcommand%" && (
      echo # ERROR - An instance of the specified program is already running; aborting^^!
      echo          = %RUNcommand%
      goto :END
)

:LOOPER
set FOUND=0
set PID=

echo   + executing '%RUNcommand%' on %DATE:~4,10% @ %TIME:~0,8%
start /min "" %RUNcommand%

:: Enumerate process list
wmic /output:"%TEMP%\%~n0.$$$" process get executablepath^,processid /value
for /f "tokens=2 delims==" %%P in ('type "%TEMP%\%~n0.$$$" ^| find "="') do (
      if "!FOUND!"=="1" (
            if "!PID!"=="" (
                  set PID=%%P
            )
      )
      if /i "%%P"=="%RUNcommand%" (
            set /a FOUND+=1
            if !FOUND! GTR 1 (
                  echo # ERROR - !FOUND! instances of the program are running; aborting^^!
                  echo          = %RUNcommand%
                  goto :END
            )
      )
)

choice /t %waitTIMER% /c yx /d y >nul
if errorlevel 2 goto :END
for /f "tokens=2 delims=:" %%K in ('taskkill -pid %PID%') do echo     - KILLING:%%K
echo/
ping -n 2 127.0.0.1 1>nul 2>&1

goto :LOOPER

:END
del %TEMP%\%~n0.$$$ 2>nul
if not "%PID%"=="" (
      for /f "tokens=2 delims=:" %%K in ('taskkill -pid %PID%') do echo     - KILLING:%%K
)
echo/
echo - Completed
:: END SCRIPT
0
 
LVL 9

Expert Comment

by:MSE-dwells
ID: 20126219
FWIW, I tweaked the output and logic a little here since some of it was untidy -

::SCRIPT
@echo off

setlocal ENABLEDELAYEDEXPANSION

echo/

:: Define operating environment
set RUNcommand=mspaint.exe
set waitTIMER=5
set ERROR=0

:: Determine full path to specified executable
for %%e in ("%RUNcommand%") do (
      if "%%~$PATH:e"=="""" (
            echo # ERROR - executable not located within the path
            echo          = %%e
            goto :END
      ) else (
            set RUNcommand="%%~$PATH:e"
            set RUNcommand=!RUNcommand:"=!
      )
)

:: Verify that an instance is not currently running
wmic process get executablepath | find /i "%RUNcommand%" >nul && (
      echo # ERROR - An instance of the specified program is already running; aborting^^!
      echo          = %RUNcommand%
      goto :END
)

echo + Working [enter an 'X' to abort and exit] ...
echo/


:LOOPER
set FOUND=0
set PID=

echo   + executing '%RUNcommand%' on %DATE:~4,10% @ %TIME:~0,8%
start /min "" %RUNcommand%

:: Enumerate process list
wmic /output:"%TEMP%\%~n0.$$$" process get executablepath^,processid /value
for /f "tokens=2 delims==" %%P in ('type "%TEMP%\%~n0.$$$" ^| find "="') do (
      if "!FOUND!"=="1" (
            if "!PID!"=="" (
                  set PID=%%P
            )
      )
      if /i "%%P"=="%RUNcommand%" (
            set /a FOUND+=1
            if !FOUND! GTR 1 (
                  echo/
                  echo # ERROR - !FOUND! instances of the program are now running; aborting^^!
                  echo          = %RUNcommand%
                  echo/
                  set ERROR=1
            )
      )
)

if "%ERROR%"=="1" goto :END
choice /t %waitTIMER% /c yx /d y >nul
if errorlevel 2 goto :END
for /f "tokens=2 delims=:" %%K in ('taskkill -pid %PID%') do echo     - KILLING:%%K
echo/
ping -n 2 127.0.0.1 1>nul 2>&1

goto :LOOPER

:END
del %TEMP%\%~n0.$$$ 2>nul
if not "%PID%"=="" (
      for /f "tokens=2 delims=:" %%K in ('taskkill -pid %PID%') do echo     - KILLING:%%K
)
echo/
echo - Completed
:: END SCRIPT
0
 

Author Comment

by:kimos123
ID: 20126238
thanks for te help.
it say that "wmic" cannot be found.

i saved this as test.bat and runned it throug cmd  
0
 
LVL 9

Expert Comment

by:MSE-dwells
ID: 20126255
Oh dear ... what OS are you running on?  Windows 2000?
0
 

Author Comment

by:kimos123
ID: 20126273
Win XP pro sp2
0
 
LVL 9

Expert Comment

by:MSE-dwells
ID: 20126344
WMIC.exe is part of the base installation media, type PATH on your machine and paste back what you
 get.
0
 

Author Comment

by:kimos123
ID: 20126360
C:\Documents and Settings\mohannad ismail>path
PATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\AT
I Technologies\ATI Control Panel;C:\Program Files\Common Files\Teleca Shared;C:\
Program Files\QuickTime\QTSystem\;"C:\Program Files\Zone Labs\ZoneAlarm\MailFron
tier"
0
 
LVL 9

Expert Comment

by:MSE-dwells
ID: 20126366
C:\>wmic
wmic:root\cli>exit

C:\>whereis wmic.exe

  TIMESTAMP : 08/04/2004 08:00 AM
  ATTRIBUTES: --a------
  FILE SIZE : 358912

    "C:\WINDOWS\system32\wbem\wmic.exe"
    C:\WINDOWS\system32\wbem\wmic.exe

C:\>ver

Microsoft Windows XP [Version 5.1.2600]

C:\>
0
 
LVL 9

Expert Comment

by:MSE-dwells
ID: 20126391
Can you paste the output when you ran the script please?

NOTE - WMIC.exe is in ' C:\WINDOWS\system32\wbem\'
0
 

Author Comment

by:kimos123
ID: 20126418
stange i cant find WMIC.EXE on my labtop
i just tried the script on my normal pc and it runned, i kept it to mspaint as a test and mspaint started but it didnt close here is the output:

C:\>test.bat

+ Working [enter an 'X' to abort and exit] ...

  + executing 'C:\WINDOWS\system32\mspaint.exe' on 10/22/2007 @ 22:07:04
'choice' is not recognized as an internal or external command,
operable program or batch file.
    - KILLING: The process with PID 2160 has been terminated.

- Completed
0
 
LVL 9

Expert Comment

by:MSE-dwells
ID: 20126515
1) As to why WMIC.exe ins't on your laptop - can't help there, I really don't know.  As far as I recollect, it's not a component that needs installation.

2) It appears that it did close since it identified the PID as 'terminated', are you sure?

3) Regarding the error you get with 'choice' -- that's entirely my mistake and/or assumption.  I wrote some of the script on Vista which has that tool built-in, although it can be downloaded, I've placed a modified version below that doesn't use choice.exe -

:: SCRIPT
@echo off

setlocal ENABLEDELAYEDEXPANSION

echo/

:: Define operating environment
set RUNcommand=mspaint.exe
set waitTIMER=5
set ERROR=0

:: Determine full path to specified executable
for %%e in ("%RUNcommand%") do (
      if "%%~$PATH:e"=="""" (
            echo # ERROR - executable not located within the path
            echo          = %%e
            goto :END
      ) else (
            set RUNcommand="%%~$PATH:e"
            set RUNcommand=!RUNcommand:"=!
      )
)

:: Verify that an instance is not currently running
wmic process get executablepath | find /i "%RUNcommand%" >nul && (
      echo # ERROR - An instance of the specified program is already running; aborting^^!
      echo          = %RUNcommand%
      goto :END
)

echo + Working [enter CTRL+C to abort and exit] ...
echo/


:LOOPER
set FOUND=0
set PID=
set /a realTIMER=%waitTIMER%+1

echo   + executing '%RUNcommand%' on %DATE:~4,10% @ %TIME:~0,8%
start /min "" %RUNcommand%

:: Enumerate process list
wmic /output:"%TEMP%\%~n0.$$$" process get executablepath^,processid /value
for /f "tokens=2 delims==" %%P in ('type "%TEMP%\%~n0.$$$" ^| find "="') do (
      if "!FOUND!"=="1" (
            if "!PID!"=="" (
                  set PID=%%P
            )
      )
      if /i "%%P"=="%RUNcommand%" (
            set /a FOUND+=1
            if !FOUND! GTR 1 (
                  echo/
                  echo # ERROR - !FOUND! instances of the program are now running; aborting^^!
                  echo          = %RUNcommand%
                  echo/
                  set ERROR=1
            )
      )
)

if "%ERROR%"=="1" goto :END
ping -n %realTIMER% 127.0.0.1 1>nul 2>&1
for /f "tokens=2 delims=:" %%K in ('taskkill -pid %PID%') do echo     - KILLING:%%K
echo/
ping -n 2 127.0.0.1 1>nul 2>&1

goto :LOOPER

:END
del %TEMP%\%~n0.$$$ 2>nul
if not "%PID%"=="" (
      if not "%ERROR%"=="1" (
            for /f "tokens=2 delims=:" %%K in ('taskkill -pid %PID%') do echo     - KILLING:%%K
      ) else (
            echo   - NOTE: instances are still running, no kill attempt will be made^^!
      )
)
echo/
echo - Completed
:: END SCRIPT
0
 

Author Comment

by:kimos123
ID: 20132822
I am sorry i coudnt answer yesterday.
this script works perfect, but now i tried to change from mspaint to  my program which is located in program files "C:\Program Files\PokerAce Hud\PAHud.exe"  
when i run this i get the msg
Windows cannot fine 'C:\Program.'. Make sure you typed the name correctly, and then try again.
0
 
LVL 9

Accepted Solution

by:
MSE-dwells earned 520 total points
ID: 20133291
That's due to the spaces in the program name and/or path and is also quite easy to correct.  Hopefully, this is the final version -

:SCRIPT
@echo off

setlocal ENABLEDELAYEDEXPANSION

echo/

:: Define operating environment
set RUNcommand=C:\Program Files\Internet Explorer\iexplore.exe
set waitTIMER=5
set ERROR=0

:: Determine full path to specified executable
for %%e in ("%RUNcommand%") do (
      if "%%~$PATH:e"=="""" (
            echo # ERROR - executable not located within the path
            echo          = %%e
            goto :END
      ) else (
            set RUNcommand="%%~$PATH:e"
            set RUNcommand=!RUNcommand:"=!
      )
)

:: Verify that an instance is not currently running
wmic process get executablepath | find /i "%RUNcommand%" >nul && (
      echo # ERROR - An instance of the specified program is already running; aborting^^!
      echo          = %RUNcommand%
      goto :END
)

echo + Working [enter CTRL+C to abort and exit] ...
echo/


:LOOPER
set FOUND=0
set PID=
set /a realTIMER=%waitTIMER%+1

echo   + executing '%RUNcommand%' on %DATE:~4,10% @ %TIME:~0,8%
start /min "" "%RUNcommand%"

:: Enumerate process list
wmic /output:"%TEMP%\%~n0.$$$" process get executablepath^,processid /value
for /f "tokens=2 delims==" %%P in ('type "%TEMP%\%~n0.$$$" ^| find "="') do (
      if "!FOUND!"=="1" (
            if "!PID!"=="" (
                  set PID=%%P
            )
      )
      if /i "%%P"=="%RUNcommand%" (
            set /a FOUND+=1
            if !FOUND! GTR 1 (
                  echo/
                  echo # ERROR - !FOUND! instances of the program are now running; aborting^^!
                  echo          = %RUNcommand%
                  echo/
                  set ERROR=1
            )
      )
)

if "%ERROR%"=="1" goto :END
ping -n %realTIMER% 127.0.0.1 1>nul 2>&1
for /f "tokens=2 delims=:" %%K in ('taskkill -pid %PID%') do echo     - KILLING:%%K
echo/
ping -n 2 127.0.0.1 1>nul 2>&1

goto :LOOPER

:END
del %TEMP%\%~n0.$$$ 2>nul
if not "%PID%"=="" (
      if not "%ERROR%"=="1" (
            for /f "tokens=2 delims=:" %%K in ('taskkill -pid %PID%') do echo     - KILLING:%%K
      ) else (
            echo   - NOTE: instances are still running, no kill attempt will be made^^!
      )
)
echo/
echo - Completed
:: END SCRIPT
0
 

Author Comment

by:kimos123
ID: 20134859
im very sorry MSE-dwells but its still not perfect :(
the progam i want runs, and it close, and when it try to run again, it says there is already an instance running, note that the program it self tell that msg, the script just stop and exit.

and also the time is very short not 3 minutes how can i make the time longer?

i am speechless for the help and the patient you are providing.

best regards,
Kimos
0
 

Author Comment

by:kimos123
ID: 20134868
its strage. i tried another program using that script and it worked.
0
 

Author Comment

by:kimos123
ID: 20134918
nevermind about the time i know how to make it longer then 5 seconds.
but the only error im getting is that it looks like it dosnt terminated, and the the script try to run it, it stop and the program it self give me the error msg that there is already an instance running
0
 
LVL 9

Assisted Solution

by:MSE-dwells
MSE-dwells earned 520 total points
ID: 20135167
If he program isn't closing, it's likely because it's prompting you since it was sent a very polite "close" request.  If this is the case as I suspect, locate the following line in the script -

for /f "tokens=2 delims=:" %%K in ('taskkill -pid %PID%') do echo     - KILLING:%%K

... and replace it with this -

for /f "tokens=2 delims=:" %%K in ('taskkill -f -pid %PID%') do echo     - KILLING:%%K

... note the addition of this ---------------------^ which sends a different type of kill signal to the app. that forces it to close.  I will mention that this can, depending upon the application, cause corruption in whatever data file the application has open at the time it was killed.
0
 

Author Comment

by:kimos123
ID: 20140878
thank your very much for your help MSE-dwells  the script is working perfect :)
0
 
LVL 9

Expert Comment

by:MSE-dwells
ID: 20143161
Phew .. ;0)
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

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

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

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