China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-07-06 06:40
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Help] How to use batch processing to start and stop services View 2,089 Replies 22
Original Poster Posted 2007-01-23 07:41 ·  中国 黑龙江 哈尔滨 联通
初级用户
Credits 31
Posts 14
Joined 2007-01-20 03:32
19-year member
UID 77089
Gender Male
Status Offline
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.
Floor 2 Posted 2007-01-23 07:52 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

  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.
Floor 3 Posted 2007-01-23 07:55 ·  中国 黑龙江 哈尔滨 联通
初级用户
Credits 31
Posts 14
Joined 2007-01-20 03:32
19-year member
UID 77089
Gender Male
Status Offline
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)
Floor 4 Posted 2007-01-23 07:59 ·  中国 黑龙江 哈尔滨 联通
初级用户
Credits 31
Posts 14
Joined 2007-01-20 03:32
19-year member
UID 77089
Gender Male
Status Offline
I just don't know how to use this value to make a judgment
Floor 5 Posted 2007-01-23 08:09 ·  中国 北京 宽捷网通信技术有限公司
新手上路
Credits 0
Posts 1
Joined 2007-01-20 14:53
19-year member
UID 77153
Gender Male
Status Offline
How to do it right? I also want to know.
Floor 6 Posted 2007-01-23 08:22 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
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
Floor 7 Posted 2007-01-23 09:53 ·  中国 广东 广州 天河区 电信
初级用户
Credits 99
Posts 43
Joined 2007-01-12 23:05
19-year member
UID 76317
Gender Male
Status Offline
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 ]
Floor 8 Posted 2007-01-23 11:59 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

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
Floor 9 Posted 2007-01-23 23:50 ·  中国 黑龙江 哈尔滨 联通
初级用户
Credits 31
Posts 14
Joined 2007-01-20 03:32
19-year member
UID 77089
Gender Male
Status Offline
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.
Floor 10 Posted 2007-01-24 00:16 ·  中国 河北 石家庄 电信
初级用户
Credits 57
Posts 25
Joined 2007-01-21 10:01
19-year member
UID 77223
Gender Male
Status Offline
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?
Floor 11 Posted 2007-01-24 01:01 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

  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.
Floor 12 Posted 2007-01-24 01:17 ·  中国 北京 联通
初级用户
★★
Credits 122
Posts 54
Joined 2006-11-03 04:24
19-year member
UID 69380
Gender Male
Status Offline
There is no SC command under 2000?
Floor 13 Posted 2007-01-24 01:21 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

  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.
Floor 14 Posted 2007-01-24 03:22 ·  中国 北京 联通
初级用户
★★
Credits 122
Posts 54
Joined 2006-11-03 04:24
19-year member
UID 69380
Gender Male
Status Offline
@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
Floor 15 Posted 2007-01-24 05:50 ·  中国 浙江 温州 电信
中级用户
★★
Credits 458
Posts 196
Joined 2006-10-05 12:04
19-year member
UID 64614
Status Offline
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
Forum Jump: