Skip to content Skip to sidebar Skip to footer

Check If Application Is Already Running Or Restart

check if application is already running or restart We can check and restart if a application service is running within SQL. This check can also be done with a SQL JOB. The applicat

Solution 1:

This batch code demonstrates usage of tasklist to find out if an application is running.

@echo off
for /F "skip=2" %%I in ('%SystemRoot%\System32\tasklist.exe /FI "IMAGENAME eq Notepad.exe" 2^>nul') do (
    if /I "%%I" == "Notepad.exe" goto AppRunning
)
echo Notepad is not running.
goto :EOF

:AppRunning
echo Notepad is running.

For details on the used commands, open a command prompt window, and run there following commands to get help for each command output in the window for reading.

  • for /?
  • goto /?
  • if /?
  • tasklist /?

Command tasklist produces a pretty printed output. If the file name of the executable has not the typical 8.3 format, but is much longer, the file name of the executable in list is truncated.

For example the output of tasklist is on German Windows XP

Abbildname                  PID Sitzungsname      Sitz.-Nr. Speichernutzung
========================= ===== ================ ========== ===============
NotePad012345678901234567  3088 Console                   0         1.984 K

on checking for NotePad012345678901234567890123456.exe with the command line

%SystemRoot%\System32\tasklist.exe /FI "IMAGENAME eq Notepad*"

Note: Wildcard * is used in image name filter specification.

The long file name of the executable is truncated to 25 characters.

The following batch code works around the truncation by just checking if the first 7 characters of any running application is Notepad.

@echo off
setlocal EnableDelayedExpansion
for /F "skip=2" %%I in ('%SystemRoot%\System32\tasklist.exe /FI "IMAGENAME eq Notepad*" 2^>nul') do (
    set "ImageName=%%I"
    if /I "!ImageName:~0,7!" == "Notepad" goto AppRunning
)
echo Sorry, but there is no Notepad* application running.
endlocal
goto :EOF

:AppRunning
echo There is a Notepad* application running.
endlocal

It is of course possible to use first batch code with just first 25 characters of the file name of the executable.

Note: Command for as used in both batch codes assigns only the string from beginning of third line to first space or tab character to loop variable I which is important to know in case of executable contains 1 or more space characters.

Solution 2:

You could use a windows scheduled task to execute a powershell script with the below code inside.

Save to file AutoStart-MyApplicationName.ps1

if (!(Get-Process | Where-Object {$_.Name -eq "<Your exe Process name>"})) {Start-Process <path to your exe>}

Set your windows scheduled task to run at whatever interval is best for your needs. The "action" step of your task should be powershell with arguments of "-NoProfile -NoLogo -File c:\AutoStart-MyApplicationName.ps1"

Solution 3:

You can use a simple powershell script which is scheduled to run depending on your needs:

$process = Get-Process -Name chrome

if ($process -eq $null)
{
    Start-Process -FilePath 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
}

replace the chrome part with your process name / file path

Solution 4:

You can use TASKLIST DOS command like below if the exe is running or not

tasklist /FI "IMAGENAME eq notepad.exe" /FI "STATUS eq running"

With the comment of @UnknownOctopus, you can use errorlevel to see if the tasklist command have returned. you can do something like below to achieve what you are after (a sample code ... in your case you can just run the notepad.exe if it's not running)

@echo off

tasklist /FI "IMAGENAME eq notepad.exe" /FI "STATUS eq running" > nullif errorlevel 0 ( 
       echo notepad running 
) else (
    echo notepad not running
    )

Post a Comment for "Check If Application Is Already Running Or Restart"