skip to content
Dhruv Garg

Blink reminder using cron

6 min read

How to create a simple blink reminder in Linux using cron?

I have recently started getting more into Linux and was studying about cron jobs. So I though about making a simple blink reminder using it. Apparantly not very unique Idea but let’s start with it. That’s why I am writting this article in which I will tell you how you can make a simple blink reminder in Linux with just using cron and simple bash script. I will also explain what problem I faced and how to resolve it.

What is cron?

Cron is a time-based job scheduling daemon found in Unix-like operating systems, including Linux distributions. Cron runs in the background and tasks scheduled with cron, referred to as “cron jobs,” are executed automatically, making cron useful for automating several tasks.

Cron jobs are recorded and managed in a special file known as a crontab. Each user profile on the system can have their own crontab where they can schedule jobs, which is stored under /var/spool/cron/crontabs/. We can also define cron as super user. If we use sudo with crontab commands but I won’t recommend that.

The Format in which cronjobs are defined inside crontab :

minute hour day_of_month month day_of_week command_to_run
example: 0 12 * * * run backup # will run backup everyday at 12 PM

for more info visite this link

from wikipedia :

The algorithm used by this cron:

  • On start-up, look for a file named .crontab in the home directories of all account holders.
  • For each crontab file found, determine the next time in the future that each command must run.
  • Place those commands on the Franta–Maly event list with their corresponding time and their “five field” time specifier.
  • Enter main loop:
    • Examine the task entry at the head of the queue, compute how far in the future it must run.
    • Sleep for that period of time.
    • On awakening and after verifying the correct time, execute the task at the head of the queue (in background) with the privileges of the user who created it.
    • Determine the next time in the future to run this command and place it back on the event list at that time value.

Additionally, the daemon responds to SIGHUP signals to rescan modified crontab files and schedules special “wake up events” on the hour and half-hour to look for modified crontab files. Much detail is omitted here concerning the inaccuracies of computer time-of-day tracking, Unix alarm scheduling, explicit time-of-day changes, and process management, all of which account for the majority of the lines of code in this cron. This cron also captured the output of stdout and stderr and e-mailed any output to the crontab owner.

The resources consumed by this cron scale only with the amount of work it is given and do not inherently increase over time, with the exception of periodically checking for changes.

Here’s the Script and Instructions

Script to show notification :

#!/bin/sh
# Notifies the user about Blinking eyes
pid=$(pgrep gnome-session | head -n 1)
dbus=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$pid/environ | sed 's/DBUS_SESSION_BUS_ADDRESS=//' )
export DBUS_SESSION_BUS_ADDRESS=$dbus
export DISPLAY=:0
/usr/bin/notify-send 'Blink Reminder' "Remember to Blink your eyes"
  • We need a PID of gnome session for finding out dbus info and we have used head -n 1 to return onlyone PID as it can return more than 1 PIDs. In my case it was returning 3 PIDs. pgrep is a tool we can use to look up or signal processes based on name and other attributes.

  • I have explaine about DBUS later in article (This was the problem, I mentioned in intro).

  • export DISPLAY=:0 was not required by my system (ubuntu 20.04), but many other people required it to work properly. That’s why I have added it. check if your display is really :0, by typing “echo $DISPLAY` in a terminal.

  • only notify-send was working for me but again some users might have problem so I have included full path.

reference : https://askubuntu.com/questions/298608/notify-send-doesnt-work-from-crontab

Here’s how to set cronjob :

it will show if you have any cronjob setuped already, if not it will tell no job present

crontab -l

To create new job or edit crontab for user

crontab -e

It might ask, to choose editor. If it’s first time using cron. choose whatever you want. Add this at the end of file which opens in editor

*/20 * * * * bash /full_path_to_script/blinkReminder.sh >/dev/null 2>&1

Now if you again type crontab -l, The output will be something like this:

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
#This will show reminder for Blinking eyes in every 20 mins
*/20 * * * * bash /home/dhruv/Programs/bashScripts/blinkReminder.sh >/dev/null 2>&1

*/20 means message will show every 20th minutes, you can change it according to your liking. for eg. */x will run cron every xth minute.

Now if anyone is still here, I will explain why that scripts work?

If you will open terminal and simply type:

notify-send 'title' 'message'

It will work perfectly, then why other lines are added? and what are they doing? Why I spent half a day to get it to work?

The problem is we can’t simply run notify-send from cron as it is runnning in background in seperate process. So that’s why DBUS_SESSION_BUS_ADDRESS environment variable is accessable from background and it is requred to send message from cron process to desktop process.

DBUS

It is an IPC mechanism which is used for Inter Process Communication(IPC), with maintaining protection and isolation b/w processes. It is a type of message bus daemon to allow processes to communicate with each other. You can think of it as a common buffer where processes can read from and to, to communicate with other processes.

Conclusion

Hope you liked this article and it helped you to learn something new. Feel free to let me know if anything is wrong in comments, also tell in comments if you liked it.

You can connect with me on twitter or linkedin

Thanks for reading this article. Best of luck in your Learning.