如何暂时禁止暂停?

Zor*_*orn 10 command-line suspend automatic

我为此搜索了一些,似乎找不到任何有用的东西。

我将运行 Ubuntu 12.10 的 PC 设置为在闲置 30 分钟后暂停。我不想改变它,它在大多数情况下都很好用。

如果特定应用程序正在运行,我想要做的是禁用自动挂起。我怎样才能做到这一点?

到目前为止,我发现的最接近的事情是添加一个 shell 脚本,/usr/lib/pm-utils/sleep.d其中检查应用程序是否正在运行并返回 1 以指示应阻止挂起。看起来系统随后放弃了自动挂起,而不是再过 30 分钟后再次尝试。(据我所知,如果我移动鼠标,会再次重新启动计时器。)应用程序很可能会在几个小时后完成,如果我不使用,我宁愿我的 PC 自动挂起就在那个时候。(所以我不想在应用程序完成时添加对 pm-suspend 的调用。)

这可能吗?

编辑:正如我在下面的评论之一中指出的那样,我真正想要的是在我的 PC 通过 NFS 提供文件时禁止挂起;我只想关注问题的“挂起”部分,因为我已经知道如何解决 NFS 部分。使用其中一个答案中给出的“xdotool”想法,我想出了以下脚本,我每隔几分钟从 cron 运行一次。这并不理想,因为它也阻止了屏幕保护程序的启动,但它确实有效。我需要看看为什么“咖啡因”稍后不能正确地重新启用挂起,然后我可能会做得更好。无论如何,这似乎确实有效,所以我将它包括在这里以防其他人感兴趣。

#!/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
Run Code Online (Sandbox Code Playgroud)

编辑 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
Run Code Online (Sandbox Code Playgroud)

第二个是先前 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
Run Code Online (Sandbox Code Playgroud)

phi*_*hem 8

使您的计算机保持唤醒状态的程序是Caffeine。当你的原始代码被调用时,我会制作一个 .bash_aliases 文件来调用咖啡因。

alias newname="origcode && caffeine"
Run Code Online (Sandbox Code Playgroud)

根据您尝试让计算机保持唤醒状态的代码,您必须制作一个自定义脚本,其中包括在其他代码停止时杀死咖啡因。有关特定代码的更多详细信息会有所帮助。

更新:一种更简单的方法是运行xdotool,它可以通过sudo apt-get install xdotool. 您可以编写一个脚本,在打开目标代码时调用该脚本,然后使用该sleep命令 29 分钟,然后运行xdotool key a或执行任意操作以保持计算机处于唤醒状态。

  • 对于 xdo,最好选择一个本质上不会做某事的键。例如,按 shift 将唤醒屏幕,而无需在活动窗口上打字。 (2认同)

Pra*_*h S 5

如果

  1. /usr/lib/pm-utils/sleep.d 中的脚本可以检查应用程序是否正在运行并返回 1 以指示应阻止挂起。
  2. “然后系统放弃自动挂起,而不是再过 30 分钟后再次尝试”的问题通过移动再次重新启动计时器的鼠标来解决(我希望我已经正确理解了您的意思)

那么为什么不在应用程序终止后摇动鼠标指针呢?

总结一下:

  1. 使用 sleep.d 防止系统挂起。
  2. 编写一个使鼠标抖动一次的脚本。
  3. 调用“长时间运行的脚本&& mousejiggle”

这不会妨碍屏幕保护程序。

唯一的问题是,当系统挂起时,它会在进程终止后 30 分钟。您的“编辑”解决方案也是如此。

PS:当我从这个页面了解到 xdotool 时,我正在寻找类似问题的解决方案。那谢谢啦。希望这可以帮助。