Shell scripting to schedule your day with Crontab and notify-send

IEEE_TEMS BLOGS
4 min readAug 25, 2023

--

“Information is not knowledge, and knowledge is not wisdom. Without the first, the second cannot be. Without the second, the third has no basis.” — Daniel J. Boorstin

Introduction

Some days are a bit harder than others to plan and go through. You got a thousand tasks for the day to do which keep skipping away from your head. Although, if you’re a Linux user, reminding yourself of things to do can become way easier with a small piece of shell scripting :)

Before going into the project, let’s understand the basics of the two libraries individually.

Crontab

Crontab is a time-based job scheduling daemon in Unix-like operating systems, including Linux. It allows users to schedule tasks to run automatically at specific intervals or times. These tasks can range from simple commands to more complex scripts or programs. The crontab utility is used to manage these scheduled tasks.

The crontab file contains cron jobs which are the so-called scheduled tasks. Let’s run through some basic crontab commands

  • crontab -e - Edit the cron file to add or remove cron jobs
  • crontab -l - display all current cron jobs set
  • crontab -r - remove a particular cron job or cron file

Any cron job looks something like this — MIN HOUR DOM MON DAY <command>

  • Here, the five denotes a value of time.
  • MIN — minutes (Ranges from 0–59)
  • HOUR — hour (Ranges from 0–23)
  • DOM — day of the month (Ranges from 1–31)
  • MON — Month (Ranges from 1–12)
  • DAY — Day (Ranges from 0–6)
  • A * denotes a wildcard value. To understand this more clearly, we shall discuss some examples:

30 * * * * echo "Hello World" - At the 30th minute of every hour of every day of every month, run the command echo "Hello World"

30 12 6 5 * mkdir "Books" - At 12:30 minute on 6th May, run the command mkdir "Books"

/We can also pass a range of values or step values from an initial value such as:

30 12-15 * * * touch games.txt - At the 30th minute of every hour from 12 pm to 3 pm of every day of every month, run the command touch games.txt

0 */2 * * 1 rm a.exe -On Mondays, on minute 0 every two hours, run the command rm a.txt

Notify-send

Nofify-send is a simple command in Linux that enables the user to send desktop notifications. Try running notify-send "Hello there" in the terminal and try greeting yourself with a clean notification.

Shell Script

Now that we understand the required tools for this mini-project, let’s get into the actual code.

First, we’ll take the system’s current date to set our cron job. For this, we use the environment variable date and format it to YYYY:MM:DD. Then, we store the day of the month and the month number in separate variables by getting the substring of the previously stored data.

#!/bin/bash

datetime=$(date '+%Y-%m-%d')
curmonth=${datetime:5:2}
curdate=${datetime:8:2}

Then, we use the read command to get the number of tasks to be set.

echo "Set your To Do list for today!"
echo -n "Enter number of tasks: "
read n

Now we can run a simple for loop to set our cron jobs.

  • Take notification content from the user and store it as task
  • Take notification time from the user and store it as set_time
  • Append the cron job in the crontab file in the format:
    ${set_time:3:2} ${set_time:0:2} $curdate $curmonth * XDG_RUNTIME_DIR=/run/user/$(id -u) notify-send '$task'")
  • Note: XDG_RUNTIME_DIR=/run/user/$(id -u) enables the crontab utility to access the UI library and display the notification.
for  (( i=0; i<n; i++ ))
do
echo -n "Enter task $((i+1)): "
read task
echo -n "Enter time(HH:MM): "
read set_time
(crontab -l; echo "${set_time:3:2} ${set_time:0:2} $curdate $curmonth * XDG_RUNTIME_DIR=/run/user/$(id -u) notify-send '$task'") | crontab -
echo "Task added"
done

echo "Have a good day!"

Some Linux versions might need to modify the command set in the cron job by adding export DISPLAY=:0to enable the crontab library to display the notification.

(crontab -l; echo "${set_time:3:2} ${set_time:0:2} $curdate $curmonth * export DISPLAY=:0 XDG_RUNTIME_DIR=/run/user/$(id -u) notify-send '$task'") | crontab -

To verify your cron jobs, run crontab -e to view all scheduled cronjobs.

Making a Linux command

I’m pretty sure as a Linux user you’re run commands like ls or mkdir at least once in your life. When you run such the ls command, the system runs the script named ls in the /bin directory of the system and similarly for all other commands

You can make your todo list on the go from anywhere in the terminal by simply moving the script file to the /bin directory of your system. Now, todo is one of the commands alike any other terminal commands. However, moving the file cannot be done by simply copy-pasting it since a move into /bin requires administrative privileges. To do so, you can use mv with sudo to run the command with the required privileges.

sudo mv path/to/todo /bin

Conclusion:

To sum up, this shell scripting project enables Linux users to efficiently manage their to-do lists using crontab and notify-send commands. By automating task reminders through cron jobs and leveraging desktop notifications, important tasks can be scheduled and remembered seamlessly. The script’s integration into the /bin directory transforms it into a user-friendly command, exemplifying the adaptability and customization potential of Linux systems. This project showcases how a simple script can significantly enhance productivity and organization for Linux users.

It’s done! You’re all set to remind yourself of all the important tasks of the day :)

ABOUT THE AUTHOR

Hi, I’m Rishbh Arora. I’m really into technology and coding. When I’m not working on computer stuff, you’ll find me taking photos — I love freezing moments in pictures. I also enjoy writing. I write about tech to make it easier for everyone to understand.

Why do I write? Because I want people to feel comfortable with technology. It’s like helping friends navigate the digital world. Whether I’m coding, taking photos, or writing, my goal is to make things better, step by step.

--

--

IEEE_TEMS BLOGS
IEEE_TEMS BLOGS

Written by IEEE_TEMS BLOGS

IEEE_TEMS, a chapter of VIT UNIV, before we tell about us STOP! Make your fingers join forces with your mind to work hard.Let’s go to the glassy world of techs

No responses yet