Python in Plain English

New Python content every day. Follow to join our 3.5M+ monthly readers.

Follow publication

Acquire Daily Earthquakes Data Using Cron

Chuan Zhang
Python in Plain English
8 min readOct 30, 2021

What is Cron Job?

$ pwd
/etc
$ ls -l cron*
-rw------- 1 root root 5 Dec 18 2020 cron.allow
-rw------- 1 root root 6 Nov 13 2019 cron.deny
-rw------- 1 root root 255 Nov 13 2019 crontab
cron.d:
total 20
-rw-r--r-- 1 root root 513 Aug 20 09:19 mysched_mystat
-rw-r--r-- 1 root root 51 Dec 3 2020 my-dfmon
-rw-r----- 1 root root 287 Aug 31 11:45 my-drop_caches
-rw-r--r-- 1 root root 0 Aug 30 14:48 my-gsctools
-rw-r--r-- 1 root root 98 May 26 10:22 my-perfmon
-rw-r--r-- 1 root root 329 Aug 31 11:45 my-sa
cron.daily:
total 36
-rwxr-xr-x 1 root root 778 Jun 24 2020 mdadm
-rwxr-xr-x 1 root root 1924 Feb 16 2017 mlocate.cron
-rwxr--r-- 1 root root 983 Jan 7 2020 suse-clean_catman
-rwxr-xr-x 1 root root 1879 Jan 21 2020 suse.de-backup-rc.config
-rwxr-xr-x 1 root root 2059 Sep 11 2014 suse.de-backup-rpmdb
-rwxr-xr-x 1 root root 566 Sep 11 2014 suse.de-check-battery
-rwxr-xr-x 1 root root 371 Sep 11 2014 suse.de-cron-local
-rwxr--r-- 1 root root 1693 Jan 7 2020 suse-do_mandb
cron.hourly:
total 0
cron.monthly:
total 0
cron.weekly:
total 0
$
$ cat cron.allow
root
$ cat cron.deny
guest
$ cat crontab
SHELL=/bin/sh
PATH=/usr/bin:/usr/sbin:/sbin:/bin:/usr/lib/news/bin
MAILTO=root
#
# check scripts in cron.hourly, cron.daily, cron.weekly, and cron.monthly
#
-*/15 * * * * root test -x /usr/lib/cron/run-crons && /usr/lib/cron/run-crons >/dev/null 2>&1
$
$ useradd -m -g users demo
$ passwd demo
New password:
BAD PASSWORD: it is too short
Retype new password:
passwd: password updated successfully
$
$ pwd
/home
$ ls -l
total 16
drwxr-xr-x 7 demo users 4096 Oct 29 15:06 demo
drwxr-xr-x 7 dscuser users 4096 Dec 31 2020 dscuser
$
$ vi /etc/sudoers
71 ##
72 ## Runas alias specification
73 ##
74
75 ##
76 ## User privilege specification
77 ##
78 root ALL=(ALL) ALL
79 demo ALL=(ALL) ALL <=== add the new user here
80
81
82 ## Uncomment to allow members of group wheel to execute any command
83 # %wheel ALL=(ALL) ALL
84
"/etc/sudoers" [readonly] 91L, 3433C
$ sudo su demo
demo@localhost:/home>
demo@localhost:/home> ls -l
total 16
drwxr-xr-x 7 demo users 4096 Oct 29 15:06 demo
drwxr-xr-x 7 dscuser users 4096 Dec 31 2020 dscuser
demo@localhost:/home> cd demo
demo@localhost:~> ls -l
total 8
drwxr-xr-x 2 demo users 4096 Jun 27 2017 bin
drwxr-xr-x 2 demo users 4096 Dec 18 2020 public_html
demo@localhost:~>
# ┌─────────── minute (0 - 59)
# │ ┌─────────── hour (0 - 23)
# │ │ ┌─────────── day of the month (1 - 31)
# │ │ │ ┌─────────── month (1 - 12)
# │ │ │ │ ┌─────────── day of the week (0 - 6) (Sunday to Saturday;
# │ │ │ │ │ 7 is also Sunday on some systems)
# │ │ │ │ │
# │ │ │ │ │
# * * * * * <command to execute>
Usage:
crontab [options] file
crontab [options]
crontab -n [hostname]
Options:
-u <user> define user
-e edit user's crontab
-l list user's crontab
-r delete user's crontab
-i prompt before deleting
-n <host> set host in cluster to run users' crontabs
-c get host in cluster to run users' crontabs
-s selinux context
-x <mask> enable debugging

Python Script for Acquiring and Uploading Earthquakes data

demo@localhost:~> docker pull postgres:latest
latest: Pulling from library/postgres
7d63c13d9b9b: Pull complete
cad0f9d5f5fe: Pull complete
ff74a7a559cb: Pull complete
c43dfd845683: Pull complete
e554331369f5: Pull complete
d25d54a3ac3a: Pull complete
bbc6df00588c: Pull complete
d4deb2e86480: Pull complete
cb59c7cc00aa: Pull complete
80c65de48730: Pull complete
1525521889be: Pull complete
38df9e245e81: Pull complete
79300c1d4f7a: Pull complete
Digest: sha256:db927beee892dd02fbe963559f29a7867708747934812a80f83bff406a0d54fd
Status: Downloaded newer image for postgres:latest
docker.io/library/postgres:latest
demo@localhost:~> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
postgres latest 317a302c7480 9 days ago 374MB
demo@localhost:~>
demo@localhost:~>
demo@localhost:~> docker volume create postgres-volume
postgres-volume
demo@localhost:~> docker volume ls
DRIVER VOLUME NAME
local postgres-volume
demo@localhost:~>
demo@localhost:~> docker run -d --name=pgdb_staging -p 5432:5432 -v postgres-volume:/home/demo/postgres/data -e POSTGRES_PASSWORD=dbc postgres
a3cfee411b31568559ef041e9abe762e9dceacd1791861d5dbac0609deb3602b
demo@localhost:~> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
be9bfb767fac postgres "docker-entrypoint.s…" 2 seconds ago Up 1 second 0.0.0.0:5432->5432/tcp, :::5432->5432/tcp pgdb_staging
demo@localhost:~>
demo@localhost:~> psql -h localhost -p 5432 -d postgres -U postgres
Password for user postgres:
psql (14.0)
Type "help" for help.
postgres=#
postgres=#
postgres=# CREATE DATABASE demo;
CREATE DATABASE
postgres=# SELECT datname, dattablespace FROM pg_catalog.pg_database;
datname | dattablespace
-----------+---------------
postgres | 1663
template1 | 1663
template0 | 1663
demo | 1663
(4 rows)
postgres=#
postgres=# CREATE USER dbc WITH PASSWORD 'dbc';
CREATE ROLE
postgres=#
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
dbc | | {}
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
postgres=#
postgres=# \q
demo@localhost:~> psql -h localhost -p 5432 -d demo -U dbc
Password for user dbc:
psql (14.0)
Type "help" for help.
demo=> \dt
Did not find any relations.
demo=>
demo=> \q
demo@localhost:~>

Scheduling Cron Job

demo@localhost:~> 
demo@localhost:~> sudo crontab -u demo -e
no crontab for demo - using an empty one
crontab: installing new crontab
demo@localhost:~>
demo@localhost:~>
demo@localhost:~> sudo crontab -l
no crontab for root
demo@localhost:~> sudo crontab -u demo -l
# cron job (demo): executing the python script for acquiring and uploading earthquake data to staging database
0 1 * * * python /home/demo/earthquakes_daily_report.py
demo@localhost:~>
demo@localhost:~> psql -h localhost -p 5432 -d demo -U dbc
Password for user dbc:
psql (14.0)
Type "help" for help.
demo=> \dt
List of relations
Schema | Name | Type | Owner
--------+-------------+-------+-------
public | earthquakes | table | dbc
(1 row)
demo=> SELECT COUNT(*) FROM earthquakes;
count
-------
210
(1 row)
demo=> SELECT * FROM earthquakes WHERE mag > 5;
id | time | place | mag | coordinates | detail
------------+-------------------------+-----------------------------------------------+-----+---------------------------+------------------------------------------------------------------------------------
us7000frfx | 2021-11-04 16:36:53.368 | 298 km SW of Bluff, New Zealand | 5.5 | {165.2157,-48.2606,10} | https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us7000frfx&format=geojson
us7000frfu | 2021-11-04 16:25:05.135 | 35 km SW of Finschhafen, Papua New Guinea | 5.2 | {147.626,-6.7899,46.39} | https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us7000frfu&format=geojson
us7000frem | 2021-11-04 15:46:38.965 | Maug Islands region, Northern Mariana Islands | 5.3 | {145.0334,19.0284,564.21} | https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us7000frem&format=geojson
us7000frcu | 2021-11-04 08:57:06.817 | 65 km ESE of Nikolski, Alaska | 5.2 | {-167.9814,52.7003,35.82} | https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us7000frcu&format=geojson
us7000fraj | 2021-11-04 02:42:44.037 | 63 km NE of Amahai, Indonesia | 5.7 | {129.3383,-2.9492,18} | https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us7000fraj&format=geojson
(5 rows)
demo=>
demo=> \q
demo@localhost:~>
demo@localhost:~>
demo@localhost:~>
demo@localhost:~> sudo crontab -u demo -r
[sudo] password for demo:
demo@localhost:~> sudo crontab -u demo -l
no crontab for demo
demo@localhost:~>

Summary

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response