Ano*_*non 10 notification command-line gnome-terminal
我sudo apt-get update
不时这样做,并且发现必须检查它何时完成它的工作令人沮丧。无论如何,我是否可以让它在执行完成后提供通知弹出窗口或播放声音,以便我可以继续工作?
Mar*_*arc 16
声音我帮不了你。但是,如果您附加类似
&& notify-send "Task complete."
Run Code Online (Sandbox Code Playgroud)
执行命令后,您应该会在右上角看到一个通知。如果默认情况下未安装所需的软件包,sudo apt-get install libnotify-bin
则应为您获取。&&
如果第一个命令成功完成,将执行第二个命令。
你想要的是sudo apt-get update; alert
. 接下来是解释和理由。
&&
或分隔它们;
。它们有两个不同的含义:command-a && command-b
意味着 command-a 将首先运行,然后,如果成功,command-b 将运行;command-a; command-b
意味着 command-a 将首先运行,然后 command-b 将运行,无论 command-a 是否成功。您可能需要分号:您希望在更新或升级停止运行时收到通知,无论它是否成功退出。notify-send
,它接受两个参数:标题和内容:(sudo apt-get update; notify-send update finished
如果任一参数包含空格,请将它们用引号引起来)。更快,更容易输入比notify-send title content
IS alert
。默认情况下,至少在某些版本的 Ubuntu 中,此别名内置于 bash 中,如果您的别名中缺少它,则很容易添加(用于type alert
检查它是否存在)。你应该得到回应,
alert is aliased to `notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e 's/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//')"'
Run Code Online (Sandbox Code Playgroud)
这意味着它会简单地拉出最后一个命令并将其发送给 notify-send
,并且伴随的图标将是终端图像(对于成功的命令)或错误图像(对于失败的命令)。
生活现在变得更加简单:command; alert
打字非常快速和容易。它将警告命令是否成功,并且警告将包含命令是否成功的指示。
顺便说一句,我无法将输出通过管道传输到 有时让我很恼火notify-send
,所以我写了一个小函数来为我做这件事:
notify ()
{
while read input; do
notify-send -i $(ls /usr/share/icons/gnome/32x32/emotes | sed 's/\(.*\)\..*/\1/' | shuf -n1) "message" "$input";
done
}
Run Code Online (Sandbox Code Playgroud)
这允许我输入command | notify
. 然而,这种情况下的图标是随机的。此外,您会收到每一行输出的通知,这对于apt-get update
.
要通过扬声器播放自定义声音,您可以运行
aplay <path_to_sound_file>
Run Code Online (Sandbox Code Playgroud)
*<声音文件路径> = 声音文件的路径
播放所选的声音文件;然后您可以添加playsound
别名~/.bashrc
:
alias playsound='aplay <path_to_sound_file>'
Run Code Online (Sandbox Code Playgroud)
*<声音文件路径> = 声音文件的路径
这样您就可以在跑步结束sudo apt-get update && playsound
后播放所选的声音。sudo apt-get update
要通过扬声器播放自定义声音,您可以运行
pacmd list-sinks | grep -E 'index|device.product.name'
Run Code Online (Sandbox Code Playgroud)
列出所有pulseaudio
可用接收器索引及其相应的设备;然后你可以运行
pacmd play-file /usr/share/sounds/ubuntu/stereo/bell.ogg <sink_index> > /dev/null
Run Code Online (Sandbox Code Playgroud)
*<sink_index> =pulseaudio
的接收器索引
/usr/share/sounds/ubuntu/stereo/bell.ogg
通过选定的水槽玩耍。(当然你可以播放任何你想要的声音);然后您可以添加playsound
别名~/.bashrc
:
alias playsound='pacmd play-file /usr/share/sounds/ubuntu/stereo/bell.ogg <sink_index> > /dev/null'
Run Code Online (Sandbox Code Playgroud)
*<sink_index> =pulseaudio
的接收器索引
这样您就可以在跑步结束sudo apt-get update && playsound
后播放所选的声音。sudo apt-get update
我写了一些脚本来实现它。脚本位于github。
这是安装指南:
下载或克隆存储库
$ git clone git@github.com:c0rp-aubakirov/notify_after_command_executed.git
Run Code Online (Sandbox Code Playgroud)
然后将脚本来源添加postexec_notify
到您的.bashrc
.
这是git clone
安装示例:
$ cd ~
$ git clone git@github.com:c0rp-aubakirov/notify_after_command_executed.git
$ cd ~/notify_after_command_executed
$ echo "source $(pwd)/postexec_notify" >> ~/.bashrc
Run Code Online (Sandbox Code Playgroud)
现在重新启动您的终端。重新启动后,您应该会看到以下消息:
preexec.sh is not exist
Trying to download it from github
Run Code Online (Sandbox Code Playgroud)
现在尝试测试是否出现通知:
$ sleep 6
Run Code Online (Sandbox Code Playgroud)
如果有不清楚的地方或者您有任何问题,请告诉我。