任何人都知道为什么我不能让我的循环批处理文件工作? [英] Anyone know why I can't get my looped batch file to work?

查看:68
本文介绍了任何人都知道为什么我不能让我的循环批处理文件工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个批处理文件,它将服务的启动类型更改为自动(延迟启动),现在附加了一个脚本,用于检查服务是否正在运行,如果没有启动它,但是在测试时它们不会似乎工作。



我的脚本服务重启部分: [ ^ ]



我的脚本去了如下:

 @ECHO OFF 
sc \\127.0.0.1 \ config< Service Here> start = delayed-auto
SET SvcName =< Service Here>

rem监控服务
sc queryex%SvcName%|找到状态| FIND / vRUNNING> NUL&& (
echo%SvcName%未运行
echo start%SvcName%

net start%SvcName%> NUL ||(
echo%SvcName %不会启动
echo%date %% time%:%SvcName%未运行>> log.txt
GOTO:B

echo%SvcName% 开始是
GOTO:B
)|| (
echo%SvcName%正在运行
echo%date %% time%:%SvcName%正在运行>> log.txt
:B
timeout / t 5
rem这是一个定时器,用于设置1800小时内检查服务的频率
GOTO:A





我尝试了什么:



我创建了一个脚本,运行上面提到的两个脚本然而另一个也不起作用。



我只是不明白为什么他们分开工作但不能一起工作。(任何人都知道如何解决这个问题。) div class =h2_lin>解决方案

DOS超时命令通常有问题,我建议使用PowerShell脚本,这也允许你等待一个进程完成。

#等待以Unins开头的所有流程完成
#1分钟后超时
写主机等待...
等待流程 - name Unins * -Timeout 60
Read-Host -PromptDone,按Enter键退出


删除脚本 SET SvcName 部分的引号。

完成任务

I've created a batch file that changes the startup type of a service to automatic (delayed start) and now have attached a script which checks that the service is running and if not starts it, however when testing they don't seem to work at all.

source of service restart portion of my script: windows - How to check if a service is running via batch file and start it, if it is not running? - Stack Overflow[^]

My Script goes as follows:

@ECHO OFF
sc \\127.0.0.1\ config "<Service Here>" start= delayed-auto
SET SvcName="<Service Here>"
:A
rem monitering of the service
sc queryex "%SvcName%" | FIND "STATE" | FIND /v "RUNNING" > NUL && (
    echo %SvcName% is not running 
    echo start %SvcName%

    net start "%SvcName%" > NUL || (
        echo "%SvcName%" wont start 
	echo %date% %time% : %SvcName% is not running >> log.txt
	GOTO:B
    )
    echo "%SvcName%" is started
    GOTO:B
) || (
    echo "%SvcName%" is running
    echo %date% %time% : %SvcName% is running >> log.txt
    :B
    timeout /t 5
    rem this is the timer which sets how frequently the service is checked 1800 for an hour
    GOTO :A
)



What I have tried:

I've created a script that runs the two scripts mentioned above one after the other however that doesn't work either.

I just don't understand why they work separately but not together.(Anyone know how to resolve this).

解决方案

The DOS timeout command is often problematic, I would advise to use a PowerShell script, this also allows you to wait for a process to finish.

# Wait for all processes starting with Unins to finish
# Time out after 1 minute
Write-Host "Waiting ..."
wait-process -name Unins* -Timeout 60
Read-Host -Prompt "Done, press Enter to exit"


Remove quotation marks at the SET SvcName part of the script.
Mission accomplished.


这篇关于任何人都知道为什么我不能让我的循环批处理文件工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆