Uncategorized
admin  

Automate to Liberate: Cron Jobs Unleashing Productivity in Project Management

One thing I loathe is to do mundane things. Repetitive things bore me like crazy. As a Technical Project Manager[TPM] and in my previous role as Solutions Architect[SA] there are so many boring and uninteresting things that come or came my way.  There is no escape from that either. To address these problems over a period of time I managed to build several systems at my work to free up my work time from doing mundane things and rather use it productively.  Any platform that offers APIs is my love interest. I would love to explore the APIs and get things done programmatically.  There are numerous products I managed to learn via APIs.

This week I want to talk about cron jobs and how it has helped me immensely in solving various business problems.

What is cron anyway?
Cron jobs – a time-based job scheduler in Unix-like operating systems, offering a streamlined solution for automating routine processes. They enable users to automate repetitive tasks by scheduling commands or scripts to run periodically at fixed intervals, such as every minute, hourly, daily, weekly, or monthly. The term “cron” itself refers to the cron daemon, a background process that runs on Unix systems and executes scheduled tasks.

How do they work?
Cron jobs work by utilizing the cron daemon, which constantly checks a file called the “crontab” for scheduled tasks. The crontab file contains entries specifying the timing and command or script to be executed. When the specified time arrives, the cron daemon triggers the associated task.

Following are some top use cases I have been working on :

  1. Sev1 and Sev2 Details Puller: I have set up a cron job that executes daily at 9 am to pull all the sev1s and sev2s that are open along with description/severity/assigned to/time elapsed and pushes the report to the team’s Webex space. This helps the team be aware of what is open and what is of priority. I use Akamai Case Management APIs to pull the cases opened.
  2. Event Monitor: During various live events on Akamai, visibility into various aspects like bandwidth consumption, and utilization is a key metric to make real-time decisions to ensure live events run smoothly.  We have cron jobs scheduled to run every minute and each time the script is scheduled to run will check if there are live events happening and if any, what should it monitor and where should the data be posted.  We have used Webex space and Slack channels to post such real-time data.
  3. Data Archiving: Log/data retention support from the platforms is generally for a smaller duration. It is very important that data of your interest example events need to be archived for future purposes like planning for similar events or summarizing.  We have cron jobs where data will be pulled from sources after events[daily] and collect various metrics and store them in a database. You can use Google sheet APIs to store it for easier access to all the stakeholders.
  4. Storage Objects Monitor:  Storage services like S3, Azure storage, and Netstorage can’t have an infinite number of objects. You need to effectively shard between buckets/storage groups. For Akamai’s Netstorage 200M is the limit on the number of objects per storage group. I have set up a cron job that runs weekly once to check the number of files in the storage group and if it is nearing 200M, it alerts all the relevant stakeholders in the webex. 
  5. Effective CMS:  There was a use case where we helped with the script to delete the files in Netstorage which was older than 2 years.  This cron job was scheduled to run daily and if the file was older than 2 years, trigger an API to delete the file from NS.
  6. News Scraper: I have set up a cron job for every 4 hours to pull the news from a public news API and I push the top 5 news descriptions to my telegram channel.

Crontab Syntax ? You can use https://crontab.guru/ to set up the cron jobs

* * * * * command_to_be_executed 

Example:
* * * * * python3 demo.py
* * * * * rm -rf temp.txt

How to Set Up?

$crontab -e

# 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
SHELL=/bin/bash
* 1 * * *  sudo rm -rf /var/mail/achuth
* * * * * cd /u0/achuth/ && sudo python3.8 sev12.py
* * * * * cd /u0/achuth/ && sudo python3.8 weather.py

How do you check the cron logs :

$sudo tail -f /var/log/syslog | grep CRON
achuth@machine:~$ sudo tail -f /var/log/syslog | grep CRON
Nov 13 14:11:01 machin CRON[13735]: (achuth) CMD (cd /u0/achuth/ && sudo python3.8 sev12.py)
Nov 13 14:11:01 machin CRON[13736]: (achuth) CMD (cd /u0/achuth/ && sudo python3.8 bandwidth.py)
Nov 13 14:11:01 machin CRON[13738]: (achuth) CMD (cd /u0/achuth/ && sudo python3.8 cpcode.py)
Nov 13 14:11:01 machin CRON[13739]: (achuth) CMD (cd /u0/achuth/ && sudo python3.8 weather.py)
Nov 13 14:11:01 machin CRON[13743]: (achuth) CMD (sudo rm -rf /var/mail/achuth)

Overall cronjobs have been very helpful in managing my productivity as Technical Project Manager. Please let us know if there are any usecases you are working on to automate your tasks and make your life easier.

Leave A Comment