HMIF ITB Tech
Published in

HMIF ITB Tech

Introduction to Program Automation: The Double-Edged Sword! 🗡️

Source

The Problem

Let’s start this topic with the problem that I was facing. In the last few weeks, I joined Bukalapak as an engineer intern and was working on the Mitra Bukalapak apps, which was one of Bukalapak products that focuses on improving ‘warung kelontongvalues. When I joined the squad, Serbu Seru at the Mitra Bukalapak app didn’t generate its winner automatically, unlike its counterpart in the Bukalapak application. It still needed an administrator to smash the ‘Generate Winner’ button every time the event ended, even though the winner itself was chosen purely random by the program. So, here’s the problem:

How to automate generate winner process on Serbu Seru event?.

Well, that’s my problem, being jomblo (single -red) for years

The Approach

Well of course, Automate it! And to do it, let me introduce you to our savior, give your best hello to the….. *drum roll plz*…. “Cron Job!”

GIF from GIPHY

YES, 💯 for you. Cron Job will be our main topic for this article. Let’s start with popping the biggest question.

W̸i̸l̸l̸ ̸y̸o̸u̸ ̸m̸a̸r̸r̸y̸ ̸m̸e̸?̸

What is a Cron Job?

According to Wikipedia, Cron is a cereal grain first domesticated by indigenous peoples in southern Mexico about 10,000 years ago. The leafy stalk of the plant produces pollen inflorescence and separate ovuliferous inflorescence called ears that yield kernels or seeds, which are fruits. 🌽🌽🌽

Whoops, wrong reference. Back to our main topic. In short:

A Cron automatically runs tasks for you at very specific times.

Based on an article called “Newbie: Intro to Cron” published by Unixgeeks.org, Cron is the name of program that enables unix users to execute commands or scripts (groups of commands) automatically at a specified time/date. A common use for it today would be connecting to the internet and downloading your email. The name cron itself comes from the Greek word for time, χρόνος (chronos).

How to write Cron’s Script?

The script varies depending on the programming language or technology being used, but the greatest similarity is in the scheduling format. It’s called Crontab, which stands for Cron Table.

Crontab is a configuration file that specifies shell command to run periodically on a given schedule. The file location varies by operating systems.

Crontab File Location Table

What about its syntax?

Crontab’s syntax follow this rule:

Crontab format

The syntax of each line expects a cron expression made of five fields. As seen above, the first line represents minute, second line represents hour, and so on. For example, if you want to run a task that runs daily at midnight, you need to write 0 0 * * * mytask.py.

If you’re not sure with your scheduling, you can always check it on some online websites, like this one:

If you want to dig more about Cron Expression, you can read here:

If you want to try writing one, you can start with one of these tutorials:

Is there any good reason why I should use this?

There are a lot of reasons to automate your task. But on a side note: not all tasks can be automated.

  1. Cut the work hours — If you do it right, cron jobs can slice thousands hours of work into an hour or even a minute.
  2. Run tasks at specific time — It’s hard for a human being to run something and always on time, but not machine right?
  3. Memory — When it’s not running, it’s not occupying memory in system.
  4. etc.

How about why I shouldn’t?

This is the reason why I called this as a double-edged sword. Beside its advantages, there are many disadvantages as well. Here’s a few cons with cron:

  1. Error handling — If a job fails, what should happen? Cron can’t alert you if your job fail or has never started.
  2. Logging — Cron doesn’t log, unless the developer made them to.
  3. Cron runs on system level — Cron is a system level process, not an application process. A good problem example is timezone: if a person changes the timezone of a server, the cron may run at different time than expected.

Not enough? You can read more at this thread:

Conclusion

Using cron can give you many benefits as long as you do it right. There are lots of tools that you can use to prevent any of those cons. Not every task that you have needs to be automated, so do it wisely. Let me close this post with a quote.

“Automation is good, so long as you know exactly where to put the machine. “ — Eliyahu Goldratt

Note:

Every suggestion or critic of this article is very much welcomed and appreciated, just leave one of those in the comment section or send it to my email: ridwanhady31@gmail.com. Thanks!

GIF from GIPHY

References:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ridwan Hady

Ridwan Hady

Just a normal mas-mas from Indonesia. Any Social media : rid1hady