CRON
CRON
CRON is a job scheduler on Unix and Unix-like systems.
If, for example, you have a python script that aggregates metrics from different parts of your business and store them in a database table, you can easily do this if you already have access a unix or linux server using cron.
There are other, similar tools you may have also heard of. Jenkins for continuous integration. Airflow developed at AirBnB for workflow management (which has the added benefit of a web interface for people with less comfort on the command line). When your team gets to a certain size you will need to move on to a more robust tool. But for small teams, or as I have seen at many organizations just a single person, cron works great.
So what is cron exactly? From a user perspective it is a text file, crontab, that holds instructions for the cron daemon. The instructions consist of a command and the time to run that command. Below is the format and an example:
* * * * * * | | | | | | | | | | | +-- Year (range: 1900-3000) | | | | +---- Day of the Week (range: 1-7, 1 standing for Monday) | | | +------ Month of the Year (range: 1-12) | | +-------- Day of the Month (range: 1-31) | +---------- Hour (range: 0-23) +------------ Minute (range: 0-59) 0 7 * * * * /home/user-1/database.py
This runs the python script database.py everyday at 7am.
0 7 1 * * * /home/user-1/database.py
This runs the python script database.py on the first of every month at 7am.
To edit your crontab from the command line type
$ crontab -e
Each user has their own crontab. So if you have multiple people modifying the crontab make sure you are modifying the same file. You can create a user named datascience then use the -u datascience parameter to specify which crontab you are editing.
Now that you are in the crontab you can edit it as you like. I recommend keeping a crontab.txt file in your git repo. That way you can just load it with one command any time you make a change and merge the updates into the repo's master branch:
$ crontab crontab.txt
If you are loading the crontab for a different user, let's say the datascience user referenced above, you will need to use the sudo command.
$ sudo -u datascience crontab crontab.txt
Comments
Post a Comment