Getting to Know Cronjobs

About a week ago was my first run-in with cronjobs.

Cronjobs are regularly scheduled tasks that are set to run automatically at a given time and date. Upon further reading, it seems cronjobs is a Linux utility

Struggles with Cron:

I had a couple of issues with getting crons to run properly and here they are below:

  1. Messed up local dev environment.
    It’s not surprise that many developers spend their first day/week on doing installs, because your dev environment matters. In this instance, the crons run properly when deployed but I couldn’t get the crons to run locally. Eventually, I realized it’s because I’ve been working on a messed up local environment, where installs were all over the place.

    Even though I’ve spent significant time trying to fix the environment, this process has been placed on halt for now, as all I have to verify locally is that the cron functions run. This can be done by manually running the function I want.

  2. Making cronjobs with the Whenever gem
    The codebase is structured where cronjobs were made from using the Whenever gem. Although after reading about crons and Whenever, it seems that this gem isn’t required to edit and update our crontabs. I will admit though, using the Whenever gem in a Rails app sure makes thing a lot more streamline.

    There are two parts to getting the Whenever gem to make a cron. First is to write the code in your schedule.rb(default file for Whenever), and to run whenver -w to write to your crontab. We can then confirm with crontab -l to see if the crontab has been updated.

  3. Crons with load-balancing
    We recently got two instances of our web application, and use a load-balancer. The problem with crons and multiple instances are that we only want the cron to run once and with multiple instances, they will run X amount of times with a number of instances we have.

    To fix this problem, we only have one instance with a crontab. I would imagine however that a better solution would be to add locks to our cronjobs. This means if two crons execute, the one that executes first would put a lock on the function it’s executing, preventing the other cron from running.

 

Overall though, I can see how cronjobs are popular in many applications and do have a use for things like daily/monthly/yearly reporting, emailers, and reminders.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s