如果命令需要超过 1 分钟才能完成,则 bash 发出哔哔声

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/743219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 20:48:42  来源:igfitidea点击:

bash to beep if command takes more than 1 minute to finish

bashexec

提问by vrdhn

I'm looking for my bash to beep, if the command i execute take more than a certain wall time ( say 1 minute ).

我正在寻找我的 bash 发出哔哔声,如果我执行的命令花费的时间超过一定的时间(比如 1 分钟)。

if it keeps on beeping every few minutes there after till i hit enter or something .. that's greate.

如果它每隔几分钟就会发出哔哔声,直到我按 Enter 或其他什么……那太好了。

Any clever ideas ?

任何聪明的想法?

I can use screen's monitor function as last resort. (I'm on cygwin, but that shouldn't matter ).

我可以使用屏幕的监控功能作为最后的手段。(我在 cygwin 上,但这应该无关紧要)。

Let me clarify:

让我澄清一下:

 $ ls    
   < output appears >   
 $ rsync foo bar 
   < since this takes lot of time, i switch to other console
      and forget this .. .and remember only after some time >
    < so when this finished, i want it to beep >

回答by adamwong246

A humble suggestion: If you have a command that you know will take a while, simply follow the command by a semi-colon, then beep.

一个谦虚的建议:如果您知道某个命令需要一段时间,只需在该命令后面加上一个分号,然后发出哔声。

rsync foo bar; beep

回答by paxdiablo

There are commands in Linux that will beep for you. Check out the source code for beep available with Ubuntu (and probably other distros) or have a look at http://www.johnath.com/beep/beep.cfor another source (it's the same code, I believe).

Linux 中有一些命令会为您发出哔哔声。查看 Ubuntu(可能还有其他发行版)可用的 beep 源代码,或者查看http://www.johnath.com/beep/beep.c以获取其他源代码(我相信这是相同的代码)。

It allows you to control frequency, length and repetitions (among other things) with ease.

它使您可以轻松控制频率、长度和重复次数(除其他外)。

To actually get it to work, you'll need a script which starts your actual workerprogram in the background, then goes into a "sleep 60"loop, beeping until it finishes.

要真正让它工作,你需要一个脚本,它在后台启动你的实际工作程序,然后进入一个"sleep 60"循环,发出哔哔声直到它完成。

The following script should be a good start. You run it with something like:

下面的脚本应该是一个好的开始。你用类似的东西运行它:

beeper sleep 3600

and it runs the sleep 3600in the background, beeping every minute until it finishes.

sleep 3600在后台运行,每分钟发出哔声直到它完成。

#!/bin/bash
$* &
pid=$!
oldmin=$(date +%M)
exf=$pid
while [[ $exf == $pid ]] ; do
    min=$(date +%M)
    if [[ $oldmin != $min ]] ; then
        beep
        oldmin=$min
    fi
    exf="$(ps -ef | awk '{print }' | grep $pid)"
    sleep 2
done

This is how it works. It runs the arguments as a background process and saves the process ID. Then it loops every couple of seconds, checking for when the minute changes and beeping when that's the case (so the first beep here may be anything up to a minute).

这就是它的工作原理。它将参数作为后台进程运行并保存进程 ID。然后它每隔几秒钟循环一次,检查分钟何时改变并在这种情况下发出哔哔声(所以这里的第一次哔哔声可能长达一分钟)。

The exit from the loop happens when the process disappears from the output of ps.

当进程从 的输出中消失时,就会退出循环ps

You can even do beeper sleep 3600 & to put the whole lot in the background. To stop the beeping, either kill off the beeper process (if you want the work to continue without beeps) or kill of the worker process itself (which will stop both the work and the beeping).

你甚至可以做 beeper sleep 3600 & 把所有的东西都放在后台。要停止发出哔哔声,请终止哔哔进程(如果您希望工作在没有哔哔声的情况下继续进行)或终止工作进程本身(这将停止工作和哔哔声)。

回答by ninesided

I'm sure I've used something like:

我确定我使用过类似的东西:

echo "
echo "
#! /bin/bash

if [[ "$#" != "1" ]]
then
  echo "Usage: 
root@tower:/usr/src/linux # beep --help
<command>" echo "Will wait till the command finishes and starts beeping once a minute" exit 1 fi CMD="" SECS_BETWEEN_BEEPS="60" # echo commmand and start time: echo "@@ Command: '${CMD}'" echo "@@ Starting at `date`" # execute and wait till finish: "${CMD}" wait # echo finish time: echo "@@ Finished at `date`" # beep once in SECS_BETWEEN_BEEPS: while ((1)) do echo -en '\a' sleep "${SECS_BETWEEN_BEEPS}" done
7"
7"

don't have a *nix shell handy to test.

没有方便测试的 *nix shell。

回答by Drakosha

command && echo ^G
  • beeps for me.

    $!

  • returns the pid of the last process ran in the background in bash, so you can sleep on it and verify whether it's still running.

  • 为我蜂鸣。

    $!

  • 返回在 bash 后台运行的最后一个进程的 pid,因此您可以在它上面休眠并验证它是否仍在运行。

To be more clear, capture the pid using $! and sleep in endless loop and check whether pid above is still alive.

为了更清楚,使用 $! 并在无限循环中睡眠并检查上面的pid是否还活着。

After (hopefully) understanding what you want, here's the code:

在(希望)理解你想要的之后,这是代码:

# bof [command] [args...] - Beep on Finish
bof() {
    "$@"; local r=$?

    printf '\a'
    return $r
}

回答by Tim Post

I like the answer provided by Pax. Have a look at what 'beep' can do:

我喜欢 Pax 提供的答案。看看“哔”能做什么:

# bot [command] [args...] - Beep on Timeout
bot() {
    { sleep 60; printf '\a'; } &

    "$@"; local r=$?

    kill $!
    return $r
}

Usage: beep [-f freq] [-l length] [-r reps] [-d delay] [-D delay] [-s] [-c] [-e device]

beep [Options...] [-n] [--new] [Options...] ... beep [-h] [--help] beep [-v] [-V] [--version]

用法:beep [-f freq] [-l length] [-r reps] [-d delay] [-D delay] [-s] [-c] [-e device]

beep [选项...] [-n] [--new] [选项...] ... beep [-h] [--help] beep [-v] [-V] [--version]

So, you could oscillate the beeps (making them louder, longer, or both) as the program continues to run longer than expected.

因此,您可以在程序继续运行比预期更长的时间时振荡哔哔声(使它们更响亮、更长或两者兼而有之)。

If you work at it, you can have the script play MUSAC versions of everyone's favorite 80's hits, which is surely going to draw someone to the terminal, if for no other reason than making it stop.

如果你努力工作,你可以让脚本播放每个人最喜欢的 80 年代热门歌曲的 MUSAC 版本,这肯定会吸引一些人到终端,如果没有别的原因,只是让它停止。

回答by Brian

I don't know offhand if it works on bash, but with many shells something like

我不知道它是否适用于 bash,但是有很多 shell,比如

# bem [command] [args...] - Beep every Minute
bem() {
    { while sleep 60; do printf '\a'; done; } &

    "$@"; local r=$?

    kill $!
    return $r
}

(ctrl-g) will beep after successful completion of the command. This is often useful when you want to execute a long-running command and then go work in another window but get notified when the first thing finished. (I'm unclear if that's your goal or not.)

成功完成命令后 (ctrl-g) 将发出哔哔声。当您想要执行长时间运行的命令然后在另一个窗口中工作但在第一件事完成时收到通知时,这通常很有用。(我不清楚这是否是你的目标。)

回答by lhunath

You're asking two entirely different things here. First you ask what to do to get beeps after one minute, then you ask to get beeps after the command finishes. These are two things that are approached entirely different.

你在这里问两个完全不同的事情。首先,您询问如何在一分钟后发出哔哔声,然后在命令完成后询问如何发出哔哔声。这是完全不同的两件事。

# bofem [command] [args...] - Beep on Finish every Minute
bofem() {
    "$@"; local r=$?

    until read -t 60 -n 1; do printf '\a'; done
    return $r
}

This function runs a command and then beeps one time once the command's done, while making sure that the exit code of the command is the exit code of the function.

此函数运行一个命令,然后在命令完成后发出一声哔哔声,同时确保命令的退出代码是该函数的退出代码。

bof rsync foo bar:      # Beep when rsync finishes.
bot ssh foo 'ls bar/'   # Beep if the 'ssh' process takes too long to run.

This function beeps once after a certain time unless the command completed before that time (here, 60seconds, one minute).

除非命令在该时间之前完成(此处为60秒,一分钟),否则此功能会在一段时间后发出哔声。

##代码##

This is a simple adaption of the earlier function which beeps everyminute for as long as your command is still alive.

这是对早期功能的简单改编,只要您的命令仍然有效,该功能分钟都会发出哔声。

##代码##

And finally, a function that beeps everyminute but only after the command has finished. Keeps beeping until you hit a key to stop it. Then, the function returns with the exit code of your command.

最后,一个分钟发出哔哔声的功能,但只有在命令完成后。一直发出哔哔声,直到您按下一个键将其停止。然后,该函数返回您的命令的退出代码。

I think that'll cover about all the bases you could have intended with your question.

我认为这将涵盖您可能对问题提出的所有基础。

Use (and combine) them like so:

像这样使用(并组合)它们:

##代码##