运行批处理脚本作为窗口服务 [英] Running batch script as windows service
问题描述
我们有哪些运行作为启动虽然批处理脚本,其中包括一些初始化配置的远程Windows系统上运行的服务器的Java应用程序。
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.
要避免登录到系统中,每次和启动/停止我计划增加该批处理脚本作为Windows服务,并远程使用它通过命令提示符的服务。失败的尝试次数后,我才知道有这样做,而无需使用第三方软件,我不允许使用的软件,由于使用限制没有简单的方法。
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.
正如我已经写了可以添加为服务,并使用了C / C ++程序的解决方案。该项目工程文件。现在我试图运行一个批处理脚本[使用system()方法]使用此code,但没有得到执行批处理脚本。其中,因为它在单机模式下的罚款。
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.
礼貌: http://www.devx.com/cplus/Article/9857
请帮我整顿的问题。
批处理脚本:
ECHO Error Log Open >C:\MyServices\ERR.LOG
ECHO Error 1 >>C:\MyServices\ERR.LOG
ECHO Message 1 >>C:\MyServices\ERR.LOG
=============================================== =
================================================
独立C程序来执行批处理脚本
Standalone C program to execute the batch script
#include
#include
void main()
{
system("C:\\MyServices\\batscr.bat");
}
=======================
=======================
程序的服务。
#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.
这篇关于运行批处理脚本作为窗口服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!