Answers:
这是我根据harrymc的回应整理的bash脚本。
#!/usr/bin/bash
# NAME
# nosleep - prevent sleep and hibernation while running a command
#
# SYNOPSIS
# nosleep COMMAND [ARG]...
# Make sure the power scheme gets restored, even if Ctrl-C happens
cleanup()
{
powercfg -setactive $SCHEME_GUID
powercfg -delete $TMP_GUID
return $?
}
trap cleanup SIGINT
# Disable sleep and hibernate timers
export SCHEME_GUID=`powercfg -getactivescheme | gawk '{ print $4 }'`
export TMP_GUID=`powercfg -duplicatescheme $SCHEME_GUID | gawk '{ print $4 }'`
if [[ -z $TMP_GUID ]]; then
echo "ERROR: could not duplicate the current power scheme"
exit 254
fi
powercfg -setactive $TMP_GUID
powercfg -changename $TMP_GUID nosleep "temporary scheme for disabling sleep and hibernation"
powercfg -change -standby-timeout-ac 0
powercfg -change -hibernate-timeout-ac 0
# Run the command
"$@"
powercfg -setactive $SCHEME_GUID
powercfg -delete $TMP_GUID
"$*"
到$*
。否则,当给出多个参数时,我会得到以下信息(例如$nosleep sleep 1
:/usr/bin/nosleep: line 31: sleep 1 command not found
"$@"
。"$*"
这是错误的,因为它连接了所有参数,而$*
错误的是它将参数分割开$IFS
并删除了空参数。
Loop { SoundPlay, %A_WinDir%\Media\ding.wav }
,无需wait
争论。
nosleep
Cygwin中现在有这样的命令。只需安装nosleep
软件包并以
nosleep myscript.sh
由Andrew E.Schulman在2011年撰写。请参阅https://cygwin.com/ml/cygwin/2011-09/msg00151.html
源在Launchpad上。它使用SetThreadExecutionState()
(就像已经提到的Insomnia),不会创建单独的电源方案。
Usage: nosleep [OPTION...] command [args]
Run a command while inhibiting computer sleep or hibernation.
-a, --awaymode Force away mode instead of sleep mode
-d, --display Keep the display on
-i, --ifacpower Following options only apply if AC power is on
-?, --help give this help list
--usage give a short usage message
-V, --version print program version
Report bugs to the Cygwin mailing list <cygwin@cygwin.com>.
请注意,它可以防止系统在空闲时自动进入睡眠状态,而不是根据用户的请求(例如,在关闭笔记本电脑盖时)使系统进入睡眠状态。
从建议这个答案由@LorenzCK上一个相关的问题:
从Windows Mobility Center使用演示模式(如果可用)
%WINDIR%\System32\PresentationSettings.exe "/start"
powercfg -requests
在命令提示符下检查。应该输出:
DISPLAY:
[PROCESS] \Device\HarddiskVolume2\Windows\System32\PresentationSettings.exe
SYSTEM:
[PROCESS] \Device\HarddiskVolume2\Windows\System32\PresentationSettings.exe
上行是在关机或重新启动时将其清除。现在您不必担心存储和恢复原始电源设置👍
它还可以防止监视器进入睡眠状态,但是您可以通过脚本中的命令来实现。简单但第三方的解决方案:NirCmd nircmd monitor off
或AutoHotkey SendMessage, 0x112, 0xF170, 2,, Program Manager
内置powercfg /requestsoverride process PresentationSettings.exe display
将无法运行。