📜 ⬆️ ⬇️

Cuckoo on bash do it yourself

Hi,% username%.

To organize my work, it is convenient to see or hear notifications that an hour has passed, half an hour, etc. I need this for orientation in the temporary space and proper planning of my work.
Since I own Ubuntu, the article will be about how to make such notifications using standard Ubuntu tools.


So, I need:

In the process of searching I tried several applications. Some of them were not bad, but without the functionality I needed, some were terribly inconvenient, and something didn’t work at all. Almost all programs suffered greatly reliability.
I did not find a normal tool, and therefore I decided to implement it myself. Writing your program is a long time, and therefore it is easier to make a script on your lap.

Ubuntu notifications


As you know, notify -send is used for notifications in Ubuntu. In the latest versions of Ubuntu, notify-send is used as the primary notification tool. It really is very convenient when all messages are displayed monotonously. Use it like this:
$ notify-send title message 

By running this simple command in the console, after some time you will see a notification with the title and message. It looks like this to me:
notification example
Great, we already know how to display messages, it remains to learn how to show them on a schedule.
')

Scheduled tasks in linux


For scheduled tasks there is an excellent crontab tool in linux
 crontab -e 

the editor comes off, we drive a line into it:
 0 */1 * * * DISPLAY=:0.0 notify-send "" " 1 " 

Save, admire the result. If you want to play, then you can put the notification once a minute, then the debug entry will look something like this:
 */1 * * * * DISPLAY=:0.0 notify-send "" " 1 " 

Now at regular intervals we can output something to the user:
sample notification every minute
For users who can not configure crontab I can advise online generator. For example, such .
I note separately that I did not bother with the definition of the display number. Yes, it can change, but after several months of work with me, it has never changed, because I still have a desktop, and the script is still done on the knee.
I will share another recipe. Using crontab -e is not very convenient, since The default editor opens. Usually it is nano. I think he's terrible. I usually do this:
 crontab -l > /tmp/crontab.file && cp /tmp/crontab.file /tmp/crontab.file.backup && gedit /tmp/crontab.file && crontab /tmp/crontab.file 

The file opens in a convenient gedit, edited, saved or not saved. After closing the gedit window, the settings go to crontab.
Or make out the whole thing in the script and put, for example, on the desktop:
 #!/bin/sh # _Nicolay # file editCrontab.sh crontab -l > /tmp/crontab.file && cp /tmp/crontab.file /tmp/crontab.file.backup && gedit /tmp/crontab.file && crontab /tmp/crontab.file rm /tmp/crontab.file 

Dialogs in linux scripts


Now, part of the problem has been resolved. But only partially, because I don’t always look at the monitor screen and can skip the message. The zenity utility came to my aid. She was already in the system. Write boldly in crontab
 0 */1 * * * DISPLAY=:0.0 zenity --info --title="" --text="  " 

or debug entry:
 */1 * * * * DISPLAY=:0.0 zenity --info --title="" --text="  " 

The result was something like this:
dialogue: one minute passed
For some time it was convenient to work, but over time the notification window began to get me. This fact made me upgrade my cron scripts. As a result, I came across the 3rd useful utility: ESpeak .
It allows you to synthesize voice messages in different languages.
Add a new line to crontab:
 0 */1 * * * espeak -vru -s130 " 1 " 

or debug line
 */5 * * * * espeak -vru -s130 " 5 " 

In the end, that's what happened


The speech synthesizer spoke Russian horribly. It was disgusting to listen, and so I switched to English and made some other improvements:

The final functionality of the notifications completely suited me. I did not have to put anything, everything was lightweight and reliable.
My crontab now looks like this:
 # crontab file # _Nicolay #     0 */1 * * * /usr/local/scripts/hours.sh #    23 12,16 * * * DISPLAY=:0.0 zenity --info --title="" --text="  !!!" 

And the hours.sh file:
 #!/bin/sh # hours.sh # _Nicolay DISPLAY=:0.0 notify-send "" "`date +%k` " espeak -ven -s130 "`date +%l` hours" 

I rendered the scripts in the hours.sh file due to the fact that the crontab script does not support processing the typewritten inverse apostrophe . If you specify a typewritten reverse apostrophe in crontab, the script will not work at the specified time.

Well, at the end of the screenshots of how it looks from me.

Every hour a message is displayed for how long:
23 hours
The message is duplicated by sound. If I'm near the computer, I'll hear it. And twice a day I’m sent a reminder that I still need to take a break.


UPD: Improved the script based on tips and comments.

Special thanks to lionsimba for commenting about the festival .

As a result, the new version of hours.sh has turned out
 #!/bin/sh # hours.sh # _Nicolay #HOURS="`date +%l`" HOURS="`date +%k`" HOURS_TXT=" ?" HOURS_TXT_SAY=$HOURS_TXT if [ "$HOURS" -eq 11 ]; then HOURS_TXT="$HOURS " HOURS_TXT_SAY="$HOURS " elif [ "$HOURS" -eq 12 ]; then HOURS_TXT="$HOURS " HOURS_TXT_SAY="$HOURS " elif [ "$HOURS" -eq 13 ]; then HOURS_TXT="$HOURS " HOURS_TXT_SAY="$HOURS " elif [ "$HOURS" -eq 14 ]; then HOURS_TXT="$HOURS " HOURS_TXT_SAY="$HOURS " elif [ "$(($HOURS % 10))" -eq 0 ]; then HOURS_TXT="$HOURS " HOURS_TXT_SAY="$HOURS " elif [ "$(($HOURS % 10))" -eq 1 ]; then HOURS_TXT="$HOURS " HOURS_TXT_SAY="$HOURS " elif [ $(($HOURS % 10)) -lt 5 ]; then HOURS_TXT="$HOURS " HOURS_TXT_SAY="$HOURS a" else HOURS_TXT="$HOURS " HOURS_TXT_SAY="$HOURS " fi DISPLAY=:0.0 notify-send -i "typing-monitor" "" "$HOURS_TXT" #espeak -ven -s130 "`date +%l` hours" echo "$HOURS_TXT_SAY" | festival --tts --language russian 


The script quite reasonably reports in Russian about the time.

That's all, thank you for your attention.

Source: https://habr.com/ru/post/126849/


All Articles