「紙糊」的語音提醒助手

記得喝水,記得動一動php

<!--more-->linux

用cron定時播放語音,哈哈~btw,我實驗的環境是Linuxmint 19.bash

生成語音

用百度帳號登陸,在語音廣播開放平臺生成簡短語音,並重命名保存。例如:session

xx@xx:~$ ll /opt/remindme/
total 280K
-rw-r--r-- 1 root root  11K 17:29 xiabandaka.mp3
-rw-r--r-- 1 root root 5.5K 17:29 shuijiao.mp3
-rw-r--r-- 1 root root 122K 17:29 penguin.png
-rwxr-xr-x 1 root root  738 17:29 notify-send-call
-rw-r--r-- 1 root root 7.2K 17:29 jiayou.mp3
-rw-r--r-- 1 root root 8.3K 17:29 heshui.mp3
-rw-r--r-- 1 root root  11K 17:29 dongyidong.mp3
-rwxr-xr-x 1 root root  261 17:33 showtip

安裝播放器

apt install mplayer

建立cron

#remindme wushui
45 12 * * * /opt/remindme/showtip "該午睡啦!" shuijiao >> /home/oliver/bdsay/rmd.log 2>&1

#remindme jiayou
15 9 * * * /opt/remindme/showtip "你是最棒的,加油!" jiayou >> /home/oliver/bdsay/rmd.log 2>&1

#remindme heshui
38 * * * * /opt/remindme/showtip "該喝水啦!" heshui >> /home/oliver/bdsay/rmd.log 2>&1

#remindme dongyidong
45 * * * * /opt/remindme/showtip "伸伸胳膊伸伸腿,扭扭脖子扭扭腰!" dongyidong >> /home/oliver/bdsay/rmd.log 2>&1

#remindm xiabandaka
*/15 17-19 * * * /opt/remindme/showtip "快下班啦,記得打卡!" xiabandaka >> /home/oliver/bdsay/rmd.log 2>&1

#remindme xiabandaka jiaban
*/10 19-21 * * * /opt/remindme/showtip "記得打卡!" xiabandaka >> /home/oliver/bdsay/rmd.log 2>&1

#remindme shuijiao
*/5 21-23 * * * /opt/remindme/showtip "你該睡覺啦!" shuijiao >> /home/oliver/bdsay/rmd.log 2>&1

嘿嘿嘿,老帶勁了……socket

腳本實例

資源都放在 /opt/remindme 下, showtip :ide

#!/bin/bash
home_remindme=/opt/remindme
echo $home_remindme
$home_remindme/notify-send-call $1 -i $home_remindme/penguin.png -u critical
/usr/bin/paplay /usr/share/sounds/LinuxMint/stereo/phone-incoming-call.ogg
/usr/bin/mplayer -ao alsa $home_remindme/$2.mp3

卡殼講解

在桌面環境下,打開一個terminal,而後使用notify-send是正常的,可是cron下直接使用notify-send就不行了。你能夠簡單的理解爲,這是由於crontab沒有桌面環境。ui

找答案,至少還得是bing……實在不行就搜英文關鍵詞……再不行就上論壇,果真,論壇找到了答案。this

notify-send communicates over the dbus session bus but cron jobs are
not part of any graphical user session; are not provided the session
bus address of whichever graphical session runs the desktop notifier
to be contacted. For Mint 19 and supposedly Ubuntu 18.04 this is
easily remedied by manually providing the cron job with the
DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$(id -u)/bus"
environment variable but that socket does not in fact exist on Mint 18
and supposedly Ubuntu 16.04.

最優解

經過腳本初始化DBUS_SESSION_BUS_ADDRESS,而後調用notify-send.unix

論壇有人提供了一個腳本,也就是上文引用的 notify-send-call ,Linuxmint 18/19 都適用。code

#!/bin/sh
if [ -z "$DBUS_SESSION_BUS_ADDRESS" ]; then 
    if [ -z "$LOGNAME" ]; then
        EUID=$(id -u)
    else
        EUID=$(id -u "$LOGNAME")
    fi
    [ -z $EUID ] && exit

    if [ -S /run/user/$EUID/bus ]; then
        export DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$EUID/bus"
    else
        SESSION=$(loginctl -p Display show-user "$LOGNAME" | cut -d= -f2)
        [ -z "$SESSION" ] && exit
        LEADER=$(loginctl -p Leader show-session "$SESSION" | cut -d= -f2)
        [ -z $LEADER ] && exit
        OLDEST=$(pgrep -o -P $LEADER)
        [ -z $OLDEST ] && exit
        export $(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$OLDEST/environ)
        [ -z "$DBUS_SESSION_BUS_ADDRESS" ] && exit
    fi
fi
notify-send "$@"

參考文章

Linux下防沉迷、防熬夜猝死的代碼
[Mint 18, 19] Using notify-send from a user crontab

相關文章
相關標籤/搜索