标题: [系统相关] [已解决]批处理命令被跳过,没有执行? [打印本页]
作者: zhaokeistc 时间: 2015-7-2 15:33 标题: [已解决]批处理命令被跳过,没有执行?
- @echo off
- sc stop LazyStopService
- ping -n 12 127.0.0.1 >nul 2>nul
- for /f %%a in ('wmic SERVICE where name^="LazyStopService" get ProcessId^|findstr "[0-9]"') do (
- tasklist /fi "pid eq %%a" |find "%%a" 2>nul
- if %errorlevel% == 0 (
- echo one error
- sc stop LazyStopService
- ping -n 12 127.0.0.1 >nul 2>nul
- tasklist /fi "pid eq %%a" |find "%%a" 2>nul
- if %errorlevel% == 0 (
- echo two error
- sc stop LazyStopService
- ping -n 10 127.0.0.1 >nul 2>nul
- tasklist /fi "pid eq %%a" |find "%%a" 2>nul
- if %errorlevel% == 0 (
- echo three error
- taskkill /F /PID %%a
- )
- )
- )
- )
- echo haha
复制代码
作者: zhaokeistc 时间: 2015-7-2 15:35
LazyStopService这个服务是我特意为测试代码写的一个延时20秒停止的一个服务
根据执行结果看 第10行代码完全没有执行啊 这是什么原因
作者: DAIC 时间: 2015-7-2 16:40
把以下代码删掉:
@echo off
2>nul
再执行一次,结果发出来看看。
作者: flyinnet9 时间: 2015-7-2 16:45
本帖最后由 flyinnet9 于 2015-7-2 16:47 编辑
2,7,12,17行- @echo off
- setlocal enabledelayedexpansion
- sc stop LazyStopService
- ping -n 12 127.0.0.1 >nul 2>nul
- for /f %%a in ('wmic SERVICE where name^="LazyStopService" get ProcessId^|findstr "[0-9]"') do (
- tasklist /fi "pid eq %%a" |find "%%a" 2>nul
- if !errorlevel!== 0 (
- echo one error
- sc stop LazyStopService
- ping -n 12 127.0.0.1 >nul 2>nul
- tasklist /fi "pid eq %%a" |find "%%a" 2>nul
- if !errorlevel! == 0 (
- echo two error
- sc stop LazyStopService
- ping -n 10 127.0.0.1 >nul 2>nul
- tasklist /fi "pid eq %%a" |find "%%a" 2>nul
- if !errorlevel! == 0 (
- echo three error
- taskkill /F /PID %%a
- )
- )
- )
- )
- echo haha
复制代码
这样应该就可以了,不过System Idle Process服务的PID貌似是0,wmic取PID的时候,如果服务未启动,返回也是0,最好判断一下
作者: zhaokeistc 时间: 2015-7-2 17:19
回复 4# flyinnet9
确实可以了 不过那个echo madan 我没去掉还是直接跳到echo haha了 echo是不占输出的!errorlevel!判断吗? 不应该是进入if才对吗
作者: flyinnet9 时间: 2015-7-2 17:37
!相对于%是起到变量延迟的作用,所以在前面加setlocal enabledelayedexpansion启用延迟,不启用,errorlevel只会取第一次的值,后面的判断是无效的
我眼神不好,实在是没有发现 echo madan在哪一行
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |