Alex Shapiro
Alex Shapiro

Reputation: 31

Automatically Resume a Suspended Windows Process

I'm trying to write a windows batch file in order to resume a windows process that gets Suspended. I'm using pssuspend (from pstools) to resume the process. However, I'm trying to write windows batch file script that will continually get the status of a process (e.g. myExe.exe). If the script is not suspended, I would like for it to keep checking if it is suspended. If it is suspended, I would like it to run the pssuspend code. I'm unsure how to obtain the Suspend status. So far I have this:

if myExe.exe == "Suspend" (
    pssuspend -r myExe.exe
    suspend_fix.bat
) else (
    suspend_fix.bat
)

Thanks for your help!

Upvotes: 3

Views: 3254

Answers (1)

ryyker
ryyker

Reputation: 23208

Windows services (that are created with the right attributes) can be suspended, but I am not sure how an executable can be suspended, or what exactly you mean by that.

If you mean that the program has been stopped, and when it does, you want to restart it, then here are a couple of code blocks that I have used to determine if a program is running:

1) by checking to see if the exe name exists, i.e., is running.
By the way, I recommend this one from my interpretation of your post:

BOOL ExeExists(char *exe)
{
    HANDLE pss = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0); 
    PROCESSENTRY32 pe = { 0 };
    pe.dwSize = sizeof(pe);
    if (Process32First(pss, &pe)) 
    {
        do
        {
            if (strstr(pe.szExeFile,exe))
            {
                CloseHandle(pss);
                return TRUE;
            }
        }
        while(Process32Next(pss, &pe));
    } 
    CloseHandle(pss);

    return FALSE;
}  

2) by checking to see if the PID exists

BOOL PidExists(int pid)
{ 
    HANDLE pss = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0); 
    PROCESSENTRY32 pe = { 0 };
    pe.dwSize = sizeof(pe);
    if (Process32First(pss, &pe)) 
    {
        do
        {
            if (pe.th32ProcessID == pid)
            {
                CloseHandle(pss);
                return TRUE;
            }
        }
        while(Process32Next(pss, &pe));
    } 
    CloseHandle(pss);

    return FALSE;
}  

By the way this is used to get the process ID (it is defined in winbase.h)
of the application making the call.

int GetProcessIdApp(void)
{
    return GetProcessId(GetCurrentProcess());//defined in WinBase.h
}  

Inside WinBase.h

WINBASEAPI
DWORD
WINAPI
GetProcessId(
    __in HANDLE Process
    );  

In my scenario, An application broadcasts its PID at start up, such that
my monitoring program (the Windows service) can read it, then use it to make an ongoing determination of the application's status. If the app is discovered to be dead, and if other criteria indicate it should still be running, my service will start it back up.

Upvotes: 1

Related Questions