【发布时间】:2020-07-01 16:04:32
【问题描述】:
我想创建和安装一个应该在后台运行的服务,直到它不应该暂停或停止服务。
所以我创建了以下测试脚本:
use Win32::Daemon;
my $ServicePath = 'C:\Strawberry\perl\bin\perl.exe';
my $ServiceParams = 'C:\ila\bin\ilad.pl';
my %service_info = (
name => 'ilad',
display => 'intelligent Lightweight Agent (ilad)',
path => $ServicePath,
description => 'ilad',
parameters => $ServiceParams,
service_type => SERVICE_WIN32_OWN_PROCESS,
start_type => SERVICE_AUTO_START);
## create ilad service ##
if(Win32::Daemon::CreateService( \%service_info))
{
print "successfully added \n";
}
else
{
print "failed to add service: " . Win32::FormatMessage( Win32::Daemon::GetLastError());
}
# Tell the OS to start processing the service...
Win32::Daemon::StartService(\%service_info))
print "Waiting...\n";
# Wait until the service manager is ready for us to continue...
print "SERVICE_NOT_READY " . SERVICE_NOT_READY . "\n";
print "SERVICE_STOPPED " . SERVICE_STOPPED . "\n";
print "SERVICE_RUNNING " . SERVICE_RUNNING . "\n";
print "SERVICE_PAUSED " . SERVICE_PAUSED . "\n";
print "SERVICE_START_PENDING " . SERVICE_START_PENDING . "\n";
print "SERVICE_STOP_PENDING " . SERVICE_STOP_PENDING . "\n";
print "SERVICE_CONTINUE_PENDING " . SERVICE_CONTINUE_PENDING . "\n";
print "SERVICE_PAUSE_PENDING " . SERVICE_PAUSE_PENDING . "\n";
while( SERVICE_START_PENDING != Win32::Daemon::State() )
{
sleep( 1 );
print Win32::Daemon::State() . "\n";
}
print "Running...\n";
# Now let the service manager know that we are running...
Win32::Daemon::State( SERVICE_RUNNING );
# Okay, go ahead and process stuff...
unlink( glob( "c:\\temp\\*.tmp" ) );
# Tell the OS that the service is terminating...
Win32::Daemon::StopService();
我有这个:
successfully added
Waiting...
SERVICE_NOT_READY 0
SERVICE_STOPPED 1
SERVICE_RUNNING 4
SERVICE_PAUSED 7
SERVICE_START_PENDING 2
SERVICE_STOP_PENDING 3
SERVICE_CONTINUE_PENDING 5
SERVICE_PAUSE_PENDING 6
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
1
1
1
1
Terminating on signal SIGINT(2)
因此它永远不会达到 SERVICE_RUNNING 状态并获取整数值并且不会获取当前的服务状态。
我正在使用草莓 perl。那么有人有什么想法吗?
【问题讨论】:
-
服务参数
C:\ila\bin\ilad.pl是什么? -
请注意这一行有语法错误:
Win32::Daemon::StartService(\%service_info))。)太多,末尾缺少分号 -
如果我在修复上面的语法错误后尝试运行它(并使用简单的测试脚本
ilad.pl),我会收到以下错误:failed to add service: Access is denied. -
我发现我需要以管理员身份运行命令提示符,现在它会按照您的描述输出一系列
1 -
@Håkon Hægland 已回答问题
标签: perl