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 :
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 :
-
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
To create new job or edit crontab for user
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
Now if you again type crontab -l
, The output will be something like this:
*/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:
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.