Introduction to Bash Scripting
Alex Scriven
Data Scientist
There are many situations where scheduling scripts can be useful:
Scheduling scripts with cron
is essential to a working knowledge of modern data infrastructures.
Cron has been part of unix-like systems since the 70's. Humans have been lazy for that long!
The name comes from the Greek word for time, chronos.
It is driven by something called a crontab
, which is a file that contains cronjobs
, which each tell crontab
what code to run and when.
You can see what schedules (cronjobs
) are currently programmed using the following command:
crontab -l
crontab: no crontab for user
Seems we need to make a schedule (cronjob) then!
This great image from Wikipedia demonstrates how you construct a cronjob
inside the crontab
file. You can have many cronjobs
, one per line.
*
means 'every'Let's walk through some cronjob examples:
5 1 * * * bash myscript.sh
*
, so every day and month
15 14 * * 7 bash myscript.sh
*
(Every day of month, every month of year). Last star is day 7 (on Sundays).If you wanted to run something multiple times per day or every 'X' time increments, this is also possible:
15,30,45 * * * *
will run at the 15,30 and 45 minutes mark for whatever hours are specified by the second star. Here it is every hour, every day etc.*/15 * * * *
runs every 15 minutes. Also for every hour, day etc.Let's schedule a script called extract_data.sh
to run every morning at 1.30am. Your steps are as follows:
crontab -e
to edit your list of cronjobs.nano
is an easy option and a less-steep learning curve than vi (vim).30 1 * * * extract_data.sh
3. Exit the editor to save it
If this was using nano
(on Mac) you would use ctrl
+ o
then enter
then ctrl
+ x
to exit.
You will see a message crontab: installing new crontab
4. Check it is there by running crontab -l
.
30 1 * * * extract_data.sh
Nice work!
Introduction to Bash Scripting