简体   繁体   English

在Windows命令提示符下,如果没有输入,如何在一定时间内运行一个命令,如果没有输入,又如何运行?

[英]In Windows Command prompt, how can I run one command if there is no input, and another if there is, within a certain time?

I wasn't sure how to phrase the question, but here's what I want to do. 我不确定如何表达问题,但这是我想做的。

If a server process closes (determined by " ping 1.1.1.1 -n 1 -w 3000 >nul "), I want to prompt the user to press any key to close the server, but if the user doesn't press a key have the server run again. 如果服务器进程关闭(由“ ping 1.1.1.1 -n 1 -w 3000 >nul ”确定),我想提示用户按任意键来关闭服务器,但是如果用户不按任何键,服务器再次运行。 Here is my current script: 这是我当前的脚本:

@echo off
:start
echo (%time%) Server started.
"C:\jdk1.7.0_72\bin\java.exe" -server -d64 -Xmx4G -Xms1G -XX:+UseConcMarkSweepGC -XX:-UseAdaptiveSizePolicy -XX:MaxGCPauseMillis=50 -XX:+DisableExplicitGC -XX:+AggressiveOpts -Xmn512M -jar spigot.jar nogui
echo (%time%) WARNING: Server closed or crashed, restarting.
ping 1.1.1.1 -n 1 -w 3000 >nul
timeout 15
goto start

As you see, I have the restart function already, and it works perfectly. 如您所见,我已经具有重启功能,并且可以正常运行。 So to clarify: if the user presses a key, the server closes and does not restart. 因此,需要澄清一下:如果用户按下某个键,则服务器将关闭并且不会重新启动。 If the user does not press a key, after 15 seconds the server starts again. 如果用户未按任何键,则15秒钟后服务器将再次启动。

It is possible to detect if TIMEOUT counted down to 0, or if the user pressed a key. 可以检测“超时”是否倒计时为0,或者用户是否按下了键。 As long as the timeout interval is >= 10, then the last two characters printed will always be " 0" if TIMEOUT finished counting down. 只要超时间隔> = 10,那么如果TIMEOUT结束倒数,则打印的最后两个字符将始终为“ 0”。 So you simply need to capture the output with a FOR /F command and test the last two characters. 因此,您只需要使用FOR / F命令捕获输出并测试最后两个字符。

The only potential problem is that you will no longer see the countdown on the screen. 唯一的潜在问题是您将不再在屏幕上看到倒数计时。

@echo off
for /f "delims=" %%A in ('timeout 15') do set "test=%%A"
if "%test:~-2%" equ " 0" (
  echo The user did NOT press a key
) else (
  echo The user pressed a key
)

If the timeout interval is < 10, then you only check the last character to see if it is 0. 如果超时间隔小于10,则仅检查最后一个字符是否为0。

@echo off
for /f "delims=" %%A in ('timeout 5') do set "test=%%A"
if "%test:~-1%" equ "0" (
  echo The user did NOT press a key
) else (
  echo The user pressed a key
)

Regarding how to incorporate this into your code - I'm not positive, because I don't fully understand what your code is supposed to do. 关于如何将其合并到您的代码中-我不是很肯定,因为我不完全了解您的代码应该做什么。

Your PING command cannot possibly determine if the server is down, nor can it shut the server down. 您的PING命令可能无法确定服务器是否已关闭,也无法关闭服务器。 All it does is introduce a 3 second delay into your script. 它所做的就是在脚本中引入3秒的延迟。

If you want to loop back to :start if TIMEOUT completed without the user pressing a key, then all you need is: 如果您想在没有用户按下键的情况下完成超时而返回到:start,那么您所需要做的就是:

for /f "delims=" %%A in ('timeout 15') do set "test=%%A"
if "%test:~-2%" equ " 0" goto start

timeout does not return an errorlevel Request for inclusion in W10 here hence using timeout , the only apparent way would be to save the %time% before and see whether the %time% after is 15 sec later - and that's fraught with problems. timeout不会在此处返回错误errorlevel W10中的包含错误请求,因此使用timeout ,唯一明显的方法是保存%time% before and see whether the %time% %time% before and see whether the 15秒后-这就充满了问题。

timeout appears to count the period from invocation to the end of that second as the first second the actually terminate the "second" at ~150ms into the next second (may depend on CPU load.) timeout似乎是将从调用到第二秒结束的时间计为第一秒,实际上是在大约150ms到下一秒之间实际上终止了“第二”(可能取决于CPU负载)。

Consequently, the timining is, to use a tachnical term, "hairy." 因此,使用技术性术语“刺激”是一种刺激。 That first "second" can be anthing from 150mSec or so to 1150mSec, just add 1000mS for each extra second it's given to timeout (15 sec = 14.15Sec..15.15Sec, for instance) 第一个“秒”可能在150毫秒左右到1150毫秒之间,每增加一秒就会增加1000毫秒(例如15秒= 14.15秒钟.15.15秒钟)

Possibly a better method would be to use choice - reappearing on W7+ AFAIAA. 可能更好的方法是使用choice -重新出现在W7 + AFAIAA上。 Essentially, that restricts the "interrupting" keyset to alphamerics amd some others - Space for instance isn't available as a "choice" IIRC. 从本质上讲,这将“中断”键集限制为字母数字和其他一些字母-例如, 空格不能用作“选择” IIRC。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以在Windows服务中运行另一个命令提示符/ GUI进程吗? - Can I run another Command Prompt/GUI process in a Windows Service? 在本机窗口中运行两个命令提示符终端,如何将一个输出重定向到另一个输入? - Running two command prompt terminals in native windows, how can I redirect the output of one to the input of the other? 如何在 Windows 中的命令提示符启动时运行命令 - How to run a command on command prompt startup in Windows 如何在Windows命令提示符下运行此命令 - How to run this command in windows command prompt 如何将时间附加到此命令提示符date命令 - How can I append time to this command prompt date command 在 Windows cmd 中,如何提示用户输入并在另一个命令中使用结果? - In Windows cmd, how do I prompt for user input and use the result in another command? Windows命令提示符为什么可以运行此文件? - Why can Windows command prompt run this file? 如何在所有子目录中搜索并删除Windows命令提示符下与文件类型不匹配的所有文件? - How can I search within all subdirectories and delete all files that do not match the filetype in windows command prompt? 我不知道如何通过 windows 命令提示符输入 - I don't know how to input through windows command prompt 如何在Windows中没有命令提示符的情况下运行nodejs? - How to run nodejs without command prompt in windows?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM