我已经搜索了一下,似乎找不到任何帮助。
我将运行Ubuntu 12.10的PC设置为在30分钟不活动后挂起。我不想更改它,它在大多数情况下都有效。
我要做的是如果正在运行特定的应用程序,则禁用自动挂起。我怎样才能做到这一点?
到目前为止,我找到的最接近的内容是添加一个shell脚本,在/usr/lib/pm-utils/sleep.d
其中检查应用程序是否正在运行,并返回1表示应防止挂起。但是看起来系统然后放弃了自动挂起,而不是再等待30分钟再尝试一次。(据我所知,如果我移动鼠标,那将再次重新启动计时器。)很有可能,应用程序将在几个小时后完成,我宁愿我的PC 在不使用时自动挂起。在那个时候。(因此,我不想在应用程序完成时向pm-suspend添加调用。)
这可能吗?
编辑:正如我在下面的评论之一中指出的那样,我真正想要的是在PC通过NFS提供文件时禁止挂起;我只想关注问题的“挂起”部分,因为我已经有了解决NFS部分的想法。使用答案之一给出的“ xdotool”想法,我想出了以下每隔几分钟从cron运行一次的脚本。这是不理想的,因为它也会阻止屏幕保护程序启动,但确实可以。我需要看看为什么'caffeine'之后无法正确重新启用暂停功能,所以我可能会做得更好。无论如何,这似乎确实可行,因此,如果有人对它感兴趣,我将其包括在这里。
#!/bin/bash
# If the output of this function changes between two successive runs of this
# script, we inhibit auto-suspend.
function check_activity()
{
/usr/sbin/nfsstat --server --list
}
# Prevent the automatic suspend from kicking in.
function inhibit_suspend()
{
# Slightly jiggle the mouse pointer about; we do a small step and
# reverse step to try to stop this being annoying to anyone using the
# PC. TODO: This isn't ideal, apart from being a bit hacky it stops
# the screensaver kicking in as well, when all we want is to stop
# the PC suspending. Can 'caffeine' help?
export DISPLAY=:0.0
xdotool mousemove_relative --sync -- 1 1
xdotool mousemove_relative --sync -- -1 -1
}
LOG="$HOME/log/nfs-suspend-blocker.log"
ACTIVITYFILE1="$HOME/tmp/nfs-suspend-blocker.current"
ACTIVITYFILE2="$HOME/tmp/nfs-suspend-blocker.previous"
echo "Started run at $(date)" >> "$LOG"
if [ ! -f "$ACTIVITYFILE1" ]; then
check_activity > "$ACTIVITYFILE1"
exit 0;
fi
/bin/mv "$ACTIVITYFILE1" "$ACTIVITYFILE2"
check_activity > "$ACTIVITYFILE1"
if cmp --quiet "$ACTIVITYFILE1" "$ACTIVITYFILE2"; then
echo "No activity detected since last run" >> "$LOG"
else
echo "Activity detected since last run; inhibiting suspend" >> "$LOG"
inhibit_suspend
fi
编辑2:上面的脚本有效,但是由于下面的另一条评论,我现在正在使用这对脚本,它们的优点是在禁止挂起时允许屏幕保护程序启动。第一个是/usr/lib/pm-utils/sleep.d/000nfs-inhibit,如果存在禁止文件,它将阻止挂起尝试:
#!/bin/sh
LOG="/home/zorn/log/nfs-suspend-blocker.log"
INHIBITFILE="/home/zorn/tmp/nfs-suspend-blocker.inhibit"
echo "$0: Started run at $(date), arguments: $*" >> "$LOG"
if [ "$1" = "suspend" ] && [ -f "$INHIBITFILE" ]; then
echo "$0: Inhibiting suspend" >> "$LOG"
exit 1
fi
exit 0
第二个是先前nfs-suspend-blocker脚本的修改版本,仍应从cron运行。现在,它遵循以下注释中概述的策略:
#!/bin/bash
# This works in tandem with /usr/lib/pm-utils/sleep.d/000nfs-inhibit, which
# will prevent a suspend occurring if $INHIBITFILE is present. Once it prevents
# a suspend, it appears that it requires some "user activity" to restart the
# timer which will cause a subsequent suspend attempt, so in addition to
# creating or removing $INHIBITFILE this script also jiggles the mouse after
# removing the file to restart the timer.
# If the output of this function changes between two successive runs of this
# script, we inhibit auto-suspend.
function check_activity()
{
/usr/sbin/nfsstat --server --list
}
# Slightly jiggle the mouse pointer about; we do a small step and reverse step
# to try to stop this being annoying to anyone using the PC.
function jiggle_mouse()
{
export DISPLAY=:0.0
xdotool mousemove_relative --sync -- 1 1
xdotool mousemove_relative --sync -- -1 -1
}
LOG="$HOME/log/nfs-suspend-blocker.log"
ACTIVITYFILE1="$HOME/tmp/nfs-suspend-blocker.current"
ACTIVITYFILE2="$HOME/tmp/nfs-suspend-blocker.previous"
INHIBITFILE="$HOME/tmp/nfs-suspend-blocker.inhibit"
echo "$0: Started run at $(date)" >> "$LOG"
if [ ! -f "$ACTIVITYFILE1" ]; then
check_activity > "$ACTIVITYFILE1"
exit 0;
fi
/bin/mv "$ACTIVITYFILE1" "$ACTIVITYFILE2"
check_activity > "$ACTIVITYFILE1"
if cmp --quiet "$ACTIVITYFILE1" "$ACTIVITYFILE2"; then
echo "$0: No activity detected since last run" >> "$LOG"
if [ -f "$INHIBITFILE" ]; then
echo "$0: Removing suspend inhibit file and jiggling mouse" >> "$LOG"
/bin/rm "$INHIBITFILE"
jiggle_mouse
fi
else
echo "$0: Activity detected since last run; inhibiting suspend" >> "$LOG"
touch "$INHIBITFILE"
fi