如何检查从批处理脚本服务的启动类型? (在Windows 7) [英] How to check service startup type from batch script? (in windows 7)
问题描述
我要开始从批处理文件服务(使用资深大律师开始XXX
),但只有当它配置了自动启动类型。
I need to start a service from batch file (using sc start XXX
), but ONLY if it is configured with an automatic start up type.
我读 SC /?
的指示,我想先打电话到 SC QC XXX
命令来查询它的配置,然后使用FINDSTR的结果,但我之后得到了以下错误 SC QC XXX
命令:
I read the instructions of sc /?
and I tried calling first to sc qc XXX
command in order to query for it's configuration and then use findstr on the result, but I got the following error after sc qc XXX
command:
[SC] QueryServiceConfig FAILED 122:
The data area passed to a system call is too small.
[SC] GetServiceConfig needs 718 bytes
指定的服务不存在作为安装服务。
The specified service does not exist as an installed service.
这是奇怪,因为我能够调用 sc配置XXX
和停止/命令行启动它。
Which is odd, because I am able to call sc config XXX
and stop/start it from command line.
我缺少的东西吗?有没有更好的办法做到这一点?
Am I missing something? Is there a better way to do it?
推荐答案
好吧,我就想通了。
首先,我必须道歉,因为原来的错误竟是:
First, I must apologize , since the original error was actually:
[SC] QueryServiceConfig FAILED 122:
The data area passed to a system call is too small.
[SC] GetServiceConfig needs 718 bytes
而不是
[SC] OpenService FAILED 1060:
我先说。
显然,我只好一个缓冲区大小,明确添加到我的服务:
SC QC XXX的 1000
Apparently, I had to explicitly add a buffer size to my service : sc qc XXX 1000
这样做之后,我注意到,BINARY_PATH_NAME领域是极长的XXX,所以我想这默认的内存分配是不够的。
After doing that, i noticed that BINARY_PATH_NAME field was extremely long for XXX, so I guess that default memory allocation was not enough.
现在,因为我基本上欠计算器我的事业,我将发布我的全code:)
Now, since I basically owe StackOverflow my career, I will post my full code :)
rem start a service, but only if it is configured as automatic, and only if it isn't running already
for /F "tokens=3 delims=: " %%H in ('sc qc %xxx% 1000^| findstr "START_TYPE"') do (
if /I "%%H" EQU "AUTO_START" (
rem check if service is stopped
for /F "tokens=3 delims=: " %%H in ('sc query %xxx% ^| findstr "STATE"') do (
if /I "%%H" EQU "STOPPED" (
echo net start %xxx%
net start %xxx%
) else (
echo %xxx% is already running
)
)
) else (
echo Skipping %xxx% since it's not defined as automatic start
)
)
这篇关于如何检查从批处理脚本服务的启动类型? (在Windows 7)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!