中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-09-24 14:08
47,812 topics / 349,917 posts / today 0 new / 48,274 members
DOS批处理 & 脚本技术(批处理室) » [Help] How to use batch processing to start and stop services
Printable Version  2,423 / 22
Floor1 rcbblgy Posted 2007-01-23 07:41
初级用户 Posts 14 Credits 31
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.
Floor2 lxmxn Posted 2007-01-23 07:52
版主 Posts 4,938 Credits 11,386

  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.
Floor3 rcbblgy Posted 2007-01-23 07:55
初级用户 Posts 14 Credits 31
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)
Floor4 rcbblgy Posted 2007-01-23 07:59
初级用户 Posts 14 Credits 31
I just don't know how to use this value to make a judgment
Floor5 wiky Posted 2007-01-23 08:09
新手上路 Posts 1 Credits 0
How to do it right? I also want to know.
Floor6 lxmxn Posted 2007-01-23 08:22
版主 Posts 4,938 Credits 11,386
There is no vmauthdservice service in my system as you mentioned. Here I take the telnet service as an example:
Floor7 hangyug Posted 2007-01-23 09:53
初级用户 Posts 43 Credits 99
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 ]
Floor8 lxmxn Posted 2007-01-23 11:59
版主 Posts 4,938 Credits 11,386

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
Floor9 rcbblgy Posted 2007-01-23 23:50
初级用户 Posts 14 Credits 31
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.
Floor10 ec2049 Posted 2007-01-24 00:16
初级用户 Posts 25 Credits 57
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?
Floor11 lxmxn Posted 2007-01-24 01:01
版主 Posts 4,938 Credits 11,386

  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.
Floor12 jasonwang Posted 2007-01-24 01:17
初级用户 Posts 54 Credits 122
There is no SC command under 2000?
Floor13 lxmxn Posted 2007-01-24 01:21
版主 Posts 4,938 Credits 11,386

  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.
Floor14 jasonwang Posted 2007-01-24 03:22
初级用户 Posts 54 Credits 122
@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
Floor15 zerocq Posted 2007-01-24 05:50
中级用户 Posts 196 Credits 458
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
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023