Unix tools make the best alarm

added on 2021-11-05

This is the first post, in my new series where I show how I use the commandline, for casual daily stuff, mostly unrelated to my daily work. I wanted to start the first post, with something very casual and simple, an alarm clock…. I dont have any gui app, for setting my alarms, I just use the two standard tools available in all my machines.

sleep and notify-send,

Yes, When I’m working on pc , whether that’s coding, reading a book, or something else, often times I need to set reminders/alarm, 2-3 hours from then. I usually dont feel like having to open a bloated slow gui app, for something as simple as that, just to often get a boring alarm ring, I’d rather have the flexibility of the sleep command. I just spin up a terminal and type the following

sleep 2h 30m ; notify-send -u "critical" "go buy some eggs, before shops close"

Its simple, it’s a oneliner, and for most of my readers, it’s rather self-explanatory. It pops up a notification on my machine, after 2 hours and 30 minutes have elapsed. If I dont want to keep that terminal open in the background, I can just have scheduled it instead in background,

(sleep 2h 30m ; notify-send -u "critical" "go buy some eggs, before shops close") &

Now, I can close the terminal down, without having to worry about terminating that alarm. To some of my readers, the question would arise, why bother ?. You could have just used an alarm app, like most people. Except, most alarm apps are not flexible, and when I’m used to using these tools daily for my job, why pick yet another one ? This does the job, just fine. Besides, once you get the hang of this, you can start exploring more options and make things more flexible, maybe you’re playing a song in the background using mpd, and you want to stop it automatically after an hour.

Just do,

sleep 1h ; mpc stop # where mpc is the client being used to interface with mpd

Done, you’re playlist will stop itself, when 1h has elapsed. Let’s say you have an alarm that you want to run each week at a specific time, cool !. Just use cron or fcron with notify-send / sendmail / or any other cli tool of your liking, to give you a notification, at that time. Ofcourse, this will only work, if you’re machine is on at that moment. Over time you can even account for error handling, Let’s say you want to schedule an alarm in a way, where if one method fails, it should try another one. Example,

sleep 2h 30m && ./sendemail.sh || (notify-send -u "critical" "Couldn't reach you by mail, 2h 30m is over, cheers" ; mpc stop)

Ok, this was a long one hehe, but nonetheless, its still a simple example, here:
The alarm,

These are just a few simple examples, to some people, this feels obvious, but it took me a year of getting used to running linux daily, to realise the true power of this.
It is just the tip of the iceberg, the commandline helps me run my own internal homesite to keep track of my notes, a financial ledger to keep track of my finances, bash scripts to automatically backup my machines to cloud and my homeserver, scripts to autogenerate my invoices end of week and email it to my clients and to automatically notify me incase of blocks or delayed payment, or to add it to my accounts, etc.
Just a few simple tools, like rsync, gpg, sleep, bash, notify-send, fcron, grep, ssmptp, ledger, git, xsv, gnuplot, go a long way to open up all sorts of possibilities, in just simple oneliners.

Other notes

If most of the tools, mentioned above sounded unfamiliar to you, you can search them up in archwiki and once you’re used to the basics, pickup a book like Unix Power Tools to get yourself comfortable with some of the default tools in unix-like systems.
After that, It’s now simply a matter of exploring what commandline tools are available to you, and how you can mix and match them to build cool stuff. Happy Scripting !

Contact/etc

If you think there’s some other tool, which you feel I should try, let me know at teito@teitoklien.com. If you disagreed with some part of this article, let me know :D

I’ll add a link here to my next post in this series, when It’s ready,

Appended on 2022-08-29:

Here it is Taco Bell Programming

Go back to Home