Michael Blum

Developer from Chicago

Cron - Time is Relative


Motivation

Looking to create a twitter bot - I settled on a simple Python script for scaping some data and publishing it to a twitter account twice a day. This bot would scrape NOAA and the OpenWeatherMap API for the combined air and water temperatures. Scrapy is a fantastic library for this task. Looking to create a twitter bot - I settled on a simple Python script for scaping some data and publishing it to a twitter account twice a day. This bot would scrape NOAA and the OpenWeatherMap API for the combined air and water temperatures. Scrapy is a fantastic library for this task.

Here’s the source code for the curious: Source

Ready to deploy

After running some tests - our bot appears to be posting to Twitter manually. But we want to send out our tweets twice a day, not manually. Enter Crontab, the ubiquitous Unix task scheduler.

It was decided that we wanted to run at 7 AM and 4 PM respectively each day. Since cron runs on a 24-hour clock our expression looks something like this:

0 7,16 * * * cd /opt/python/lpbcbot && source setenv.sh && ./bin/python main.py

07 hours being 7 AM and 16 hours being 4 PM. Setting the crontab locally and waiting for the job to run we see our Twitter bot posting to Twitter automagickly:

@lpbcbot tweet Apparently the timestamp you see on the tweet is a bit off sometimes

Great! Lets deploy the bot to a server. The README.md found in the repo goes as to how to do this.

6 AM the next day

My phone buzzes and I see my Twitter bot hard at work telling me the weather. I look at my watch.. 6 AM? We told our bot to tweet at 7 AM…

After thinking about it for a bit I remembered that the server is a Digital Ocean instance in New York.. an hour ahead of Chicago.

map of IL to NY map from AM Charts

To confirm, I ssh into the DO box and lo and behold:

> sudo dpkg-reconfigure tzdata

Current default time zone: 'America/New_York'
Local time is now:      Wed Apr 13 01:28:18 EDT 2016.
Universal Time is now:  Wed Apr 13 05:28:18 UTC 2016.

Little did I know, crontab runs jobs on the local time of the server, not universal time.

Lesson learned - to fix it I just ticked forward my crontab settings to read:

0 8,17 * * * cd /opt/python/lpbcbot && source setenv.sh && ./bin/python main.py

since the VPC does live in a New York datacenter and this box supports several other applications, I decided changing the system clock was a bit drastic.