简体   繁体   中英

Running batch script as windows service

We have a java application which run as server running on a remote windows system which is started though a batch script which includes some initialization configurations.

To avoid login into the system every time and starting / stopping the service I planned to add that batch script as a "Windows Service" and use it remotely through command prompt. After number of failed attempts I came to know there is no simple way of doing it without using third party software which I am not allowed to to use due software usage restrictions.

As a solution I have written a C / C++ program which can be added as a service and used. The program works file. Now I am trying to run a batch script [using system() method ] using this code but the batch script is not getting executed. Where as it works fine in stand alone mode.

Courtesy: http://www.devx.com/cplus/Article/9857

Kindly help me in rectifying the issue.

Batch Script:

batscr.bat


ECHO Error Log Open >C:\MyServices\ERR.LOG
ECHO Error 1 >>C:\MyServices\ERR.LOG
ECHO Message 1 >>C:\MyServices\ERR.LOG

================================================

Standalone C program to execute the batch script

BatchExe.cpp


#include 
#include 

void main()
{
    system("C:\\MyServices\\batscr.bat");
}

=======================

Program for the service

StartScript.cpp



#include 
#include 
#include 

#define SLEEP_TIME 5000
#define LOGFILE "C:\\MyServices\\memstatus.txt"

SERVICE_STATUS ServiceStatus;
SERVICE_STATUS_HANDLE hStatus;

void  ServiceMain(int argc, char** argv);
void  ControlHandler(DWORD request);
void InitService();
void main()
{
    SERVICE_TABLE_ENTRY ServiceTable[2];
    ServiceTable[0].lpServiceName = "StartScript";
    ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain;

    ServiceTable[1].lpServiceName = NULL;
    ServiceTable[1].lpServiceProc = NULL;
    // Start the control dispatcher thread for our service
    StartServiceCtrlDispatcher(ServiceTable);
}


void ServiceMain(int argc, char** argv)
{
    int error;

    ServiceStatus.dwServiceType        = SERVICE_WIN32;
    ServiceStatus.dwCurrentState       = SERVICE_START_PENDING;
    ServiceStatus.dwControlsAccepted   = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
    ServiceStatus.dwWin32ExitCode      = 0;
    ServiceStatus.dwServiceSpecificExitCode = 0;
    ServiceStatus.dwCheckPoint         = 0;
    ServiceStatus.dwWaitHint           = 0;

    hStatus = RegisterServiceCtrlHandler(
        "StartScript",
        (LPHANDLER_FUNCTION)ControlHandler);
    if (hStatus == (SERVICE_STATUS_HANDLE)0)
    {
        // Registering Control Handler failed
        return;
    }
    // Initialize Service
    InitService();

    // We report the running status to SCM.
    ServiceStatus.dwCurrentState = SERVICE_RUNNING;
    SetServiceStatus (hStatus, &ServiceStatus);



    return;
}

// Service initialization
void InitService()
{
    system("C:\\MyServices\\batscr.bat");
    return;
}

// Control handler function
void ControlHandler(DWORD request)
{
    switch(request)
    {
        case SERVICE_CONTROL_STOP:
            ServiceStatus.dwWin32ExitCode = 0;
            ServiceStatus.dwCurrentState  = SERVICE_STOPPED;
            SetServiceStatus (hStatus, &ServiceStatus);
            return;

        case SERVICE_CONTROL_SHUTDOWN:
            ServiceStatus.dwWin32ExitCode = 0;
            ServiceStatus.dwCurrentState  = SERVICE_STOPPED;
            SetServiceStatus (hStatus, &ServiceStatus);
            return;

        default:
            break;
    }

    // Report current status
    SetServiceStatus (hStatus,  &ServiceStatus);

    return;
}

Thanks and Regards ...

this may help you a little bit Link here

... it's quite a common problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM