|
rcbblgy
初级用户
 
积分 31
发帖 14
注册 2007-1-20
状态 离线
|
『楼 主』:
[求助]如何用批处理来开关服务
使用 LLM 解释/回答一下
我知道用sc start/stop 服务名 来开关服务,但是我现在想先判断服务是打开的还是关闭的,要是打开的就关闭服务,要是服务是关闭状态就打开服务,这个怎么来实现?是不是用到query这个参数啊,我刚学批处理,高手们指点一下。
I know that using sc start/stop service name to start and stop services, but now I want to first judge whether the service is on or off. If it's on, close the service; if the service is off, start the service. How to achieve this? Is it using the query parameter? I'm just learning batch processing, please experts give me some pointers.
|
|
2007-1-23 07:41 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
提示一下:先根据SC QUERY <SERVICENAME>的STATE值来判断,再SC CONFIG <SERVICENAME>配置服务,再来SC START/STOP启动停止服务。
Tip: First, judge according to the STATE value of SC QUERY <SERVICENAME>, then configure the service with SC CONFIG <SERVICENAME>, and then start and stop the service with SC START/STOP.
|
|
2007-1-23 07:52 |
|
|
rcbblgy
初级用户
 
积分 31
发帖 14
注册 2007-1-20
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
@echo off
sc query vmauthdservice >query.txt
find "state" query.txt |find "4"
if not errorlevel 4 (sc start vmauthdservice) else(sc stop vmauthdservice)
上面的写法只能开不能关,错在哪里?
The error is in the judgment condition. The correct way should be to check if the result is equal to 4. The current `if not errorlevel 4` is wrong. The correct code should be:
@echo off
sc query vmauthdservice >query.txt
for /f "tokens=3 delims=: " %%a in ('find "STATE" query.txt') do set "state=%%a"
if %state% equ 4 (sc stop vmauthdservice) else (sc start vmauthdservice)
|
|
2007-1-23 07:55 |
|
|
rcbblgy
初级用户
 
积分 31
发帖 14
注册 2007-1-20
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
我就是不知道怎么用这个值来判断啊
I just don't know how to use this value to make a judgment
|
|
2007-1-23 07:59 |
|
|
wiky
新手上路

积分 0
发帖 1
注册 2007-1-20
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
怎么做才对?我也想知道 。
How to do it right? I also want to know.
|
|
2007-1-23 08:09 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
我的系统里面没有你说的 vmauthdservice 服务,这里我以telnet服务为例子吧:
@echo off
for /f "delims=" %%i in ('SC QUERY TLNTSVR^|FIND "STATE"') do (
echo %%i|find /i "RUNNING" && (
sc stop tlntsvr
sc config tlntsvr start= disabled)||(
sc config tlntsvr start= auto
sc start tlntsvr)
)
exit/b
There is no vmauthdservice service in my system as you mentioned. Here I take the telnet service as an example:
@echo off
for /f "delims=" %%i in ('SC QUERY TLNTSVR^|FIND "STATE"') do (
echo %%i|find /i "RUNNING" && (
sc stop tlntsvr
sc config tlntsvr start= disabled)||(
sc config tlntsvr start= auto
sc start tlntsvr)
)
exit/b
|
|
2007-1-23 08:22 |
|
|
hangyug
初级用户
 
积分 99
发帖 43
注册 2007-1-12
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
请问lxmxn,那些"半个"括号是什么意思?还有一个括号占了一行,又是什么意思?可否讲解一下?括号是不是有特殊的用法?
Last edited by hangyug on 2007-1-22 at 08:54 PM ]
May I ask lxmxn, what do those "half" parentheses mean? And there's a parenthesis that takes up a whole line, what does that mean? Can you explain? Do parentheses have special usages?
Last edited by hangyug on 2007-1-22 at 08:54 PM ]
|
|
2007-1-23 09:53 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
这样发给你看,应该明白了吧?
@echo off
for /f "delims=" %%i in ('SC QUERY TLNTSVR^|FIND "STATE"') do (
echo %%i|find /i "RUNNING" && (
sc stop tlntsvr
sc config tlntsvr start= disabled)||(
sc config tlntsvr start= auto
sc start tlntsvr)
)
exit/b
Sent like this, should it be clear?
@echo off
for /f "delims=" %%i in ('SC QUERY TLNTSVR^|FIND "STATE"') do (
echo %%i|find /i "RUNNING" && (
sc stop tlntsvr
sc config tlntsvr start= disabled)||(
sc config tlntsvr start= auto
sc start tlntsvr)
)
exit/b
|
|
2007-1-23 11:59 |
|
|
rcbblgy
初级用户
 
积分 31
发帖 14
注册 2007-1-20
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
非常感谢,这个服务是虚拟机的服务。我平时把它设成手动,用虚拟机的时候再开服务,所以服务启动类型就不设了,全当知识学了吧,呵呵。
不过还有一些地方不太明白啊,比如"delims=" 、%%i、^,我以前只看过一些简单的批处理教程,只介绍了常用的命令,哪里有比较全面的讲解教程,我想学学。
Thank you very much. This service is for virtual machine. I usually set it to manual and start the service when using the virtual machine, so I won't set the service startup type. Just treat it as learning knowledge, hehe.
But there are still some places I don't understand, such as "delims=", "%%i", "^. I have only read some simple batch processing tutorials before, which only introduce common commands. Where is there a more comprehensive explanation tutorial? I want to learn.
|
|
2007-1-23 23:50 |
|
|
ec2049
初级用户
 
积分 57
发帖 25
注册 2007-1-21
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
lxmxn那个bat的作用:
如果服务已启动就让它停止;
如果服务已停止就让它启动;
是这样吗?
The function of that bat of lxmxn:
If the service has started, let it stop;
If the service has stopped, let it start;
Is it like this?
|
|
2007-1-24 00:16 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
Re rcbblgy:
到论坛的教学室去看看吧,里面很多教程的,搜索一下就有的。
Re ec2049:
是这样的。运行批处理就可以改变服务的开启状态了。
Re rcbblgy:
Go to the forum's teaching room to take a look, there are many tutorials inside, and you can find them by searching.
Re ec2049:
That's right. Running a batch script can change the startup status of services.
|
|
2007-1-24 01:01 |
|
|
jasonwang
初级用户
 
积分 122
发帖 54
注册 2006-11-3
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
2000下没有SC这个命令啊?
There is no SC command under 2000?
|
|
2007-1-24 01:17 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
那就没有办法了,可以搜索一下论坛里面关于配置系统服务的三方命令行工具,我记得有一个原创帖的。
Then there is no way. You can search for third-party command-line tools for configuring system services in the forum. I remember there is an original post about it.
|
|
2007-1-24 01:21 |
|
|
jasonwang
初级用户
 
积分 122
发帖 54
注册 2006-11-3
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
@echo off
for /f "delims=" %%i in ('SC QUERY TLNTSVR^|FIND "STATE"') do ( 这句看懂了,根据state来做DO后面的工作)
echo %%i|find /i "RUNNING" && ( 如果是"running"状态, &&是什么呢?
sc stop tlntsvr SC命令停止TELNET服务
sc config tlntsvr start= disabled)||( SC永久配置TELNET为不启用 但||是什么意思呢?
sc config tlntsvr start= auto SC启动TELNET为自动,启动TELNET
sc start tlntsvr)
)
exit/b
@echo off
for /f "delims=" %%i in ('SC QUERY TLNTSVR^|FIND "STATE"') do ( Understood this, do the work after DO according to STATE)
echo %%i|find /i "RUNNING" && ( If it is "running" state, what is &&?
sc stop tlntsvr SC command to stop TELNET service
sc config tlntsvr start= disabled)||( SC permanently configure TELNET to not enable But what does || mean?
sc config tlntsvr start= auto SC start TELNET to automatic, start TELNET
sc start tlntsvr)
)
exit/b
|
|
2007-1-24 03:22 |
|
|
zerocq
中级用户
  
积分 458
发帖 196
注册 2006-10-5
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
Originally posted by rcbblgy at 2007-1-23 23:50:
非常感谢,这个服务是虚拟机的服务。我平时把它设成手动,用虚拟机的时候再开服务,所以服务启动类型就不设了,全当知识学了吧,呵呵。
不过 ...
楼主和我一样,呵呵
我喜欢在用vmware才开启那些nat,dhcp服务,所以写了一个脚本检测来开关服务
如果你也是vmware的话很简单,因为这些服务在开启后会在产生进程
像vmnat.exe,只要tasklist|find确认就好了
net start|find也可以啊:D
Originally posted by rcbblgy at 2007-1-23 23:50:
Thanks a lot. This service is for the virtual machine. I usually set it to manual and start the service when using the virtual machine, so I don't set the service startup type. Just treat it as learning knowledge, heh heh.
But...
The owner is the same as me, heh heh
I like to start those nat, dhcp services only when using vmware, so I wrote a script to detect and switch services
If you are also using vmware, it's very simple because these services will generate processes after starting
Like vmnat.exe, just confirm with tasklist|find
net start|find can also work :D
|
|
2007-1-24 05:50 |
|