当我们运行诸如 source compile 或 brew upgrade 之类的命令时,我们通常会做其他事情,例如网络搜索,而不是一直观察命令运行。
命令完成后有什么可能的方法可以通知我吗?
brew upgrade | afplay /System/Library/Sounds/Submarine.aiff -v 10
我在Mac上尝试了afplay,但问题是a)由于管道而没有显示stdout,b)它在命令开始运行后立即播放声音,而不是等到“brew升级”完成。
请问有什么建议吗?
brew upgrade && say brew upgrade done
利用say命令并获得自定义语音通知
一般示例:
YOUR_COMMAND && say YOUR_COMMAND done
我想你需要
brew upgrade && afplay /System/Library/Sounds/Submarine.aiff -v 10
如果 brew 成功完成,则 afplay 将运行。
作为解释说明,您要求外壳程序确定两个语句(“brew”和“afplay”)是否为真 - 因此有义务在执行之前完全评估“brew”语句(即等待它完成) “afplay”,以确定这两个陈述是否正确。
Thanks Mark. You gave me a great idea.
I haven't noticed &&, || can mean logical operators on shell also. It appears '&& afplay ..' works only when 'brew upgrade' returns 0 which happens most of the time, and '|| afplay ..' works only when 'brew upgrade' returns 1 (or any non-zero) meaning error. I decided to use semicolon ';' so afplay can play sound alarm whatever 'brew upgrade' reterns after job done.
brew upgrade ; afplay /System/Library/Sounds/Submarine.aiff -v 10
EDIT: Now I use the following code in .profile (or .bashrc), which utilizes the advanced notifier tool, terminal-notifier.
function brew { caffeinate -s brew $@; terminal-notifier -title 'Homebrew' -subtitle 'Finished' -message brew' '$1' '$2' '$3 -sound 'recv_mail' -contentImage '/Download/Any/Icon/beer_icon.png';}
Please note:
caffeinate is used to prevent Mac from falling into sleep mode while brew works for a long time. If you remove 'caffeinate -s', you should rename the function name also.要直接回答这个问题,无需安装任何额外的东西,只需系统警报即可完成。
运行“命令”后播放系统提示音:
command; printf "\a"
Giorgio 的回答也不错,如"
command; say command is done
喜欢连续报警的想法;我会做这样的事情来每 10 秒重复一次警报:
command; while :; sleep 10; do say command is done; done
或者
command; while :; sleep 2; do printf "\a"; done
这是我添加到我的一个小功能.zshrc
noti() {
if [[ $? = 0 ]]; then
say OK
else
say error
fi
}
从 shell执行some command ; noti将根据命令结果播放“OK”或“错误”。
我发现它是一种更好的替代方法,&&因为它会同时通知成功和错误。
sleep 30m; for i in {1..10}; do afplay /System/Library/Sounds/Purr.aiff -v 10; done
OP要求报警。此代码以最大音量重复声音 Purr.aiff 10 次,以确保您能听到它(如闹钟)。替换sleep30m为您想要的命令。如果您想要更长的时间,请10在 for 循环中替换为10000.
如果您想要无限警报(按CTRL-C退出),请尝试:
sleep 30m; while :; do afplay /System/Library/Sounds/Purr.aiff -v 10; done
如果您想要其他声音,您可以尝试替换Purr.aiff为:
Purr.aiff并且Blow.aiff是我个人的最爱。
~/.bash_profile在您的(或等效的)中创建一个别名。
# Play sound
alias notify='afplay /System/Library/Sounds/Submarine.aiff -v 10'
来源您的~/.bash_profile.
source ~/.bash_profile
运行需要很长时间的命令。您可以notify在同一行使用别名,也可以notify在命令已经开始运行后键入。
brew update; notify
# or
brew update
# oops, I forgot to add "; notify". No worries, just type it now
notify
一旦brew update运行完毕,notify就会执行,播放声音。
旧线程,但在 macOS Catalina 上适用于我的简单本机命令:
$ echo -e "\007"
或附加到命令:
$ long_running_program && echo -e "\007"
这实质上与 BEL(贝尔)ASCII 字符相呼应,该字符会发出原生 macOS 终端通常发出的哔声。