0

Run a Python script via cron

Welcome to post 4 of my 100 day challenge. Checkout my introduction for some background.

Today I will explain how you can run a Python script via cron. This approach can be useful from a system administration perspective, when you want to run your scripts on an interval but don’t want to be present.

A cron job is a time based method of scheduling tasks on Unix based operating systems. In a Windows environment these are called Scheduled Tasks. If you want to know more about what cron is and what other things you can do with it take a look here.

Python is a high level general purpose scripting language. It is usually included as standard in many Linux distributions and can therefore be utilised to perform many administrative tasks. You can find out more about Python here.

I have assumed you are familiar with cron and able to write simple Python scripts in order to understand this post. If you are not please consider reading the two links above.

Here is a simple Python script from the Python Wiki. I have called it fibonacci.py and it’s job is to print a fibonacci sequence to standard output:

#!/usr/bin/python
# -*- coding: utf-8 -*-
parents, babies = (1, 1)
while babies < 100:
    print 'This generation has {0} babies'.format(babies)
    parents, babies = (babies, parents + babies)

Create a file fibonacci.py, paste in the code then make it executable:

cat <<EOT >> ~/fibonacci.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
parents, babies = (1, 1)
while babies < 100:
    print 'This generation has {0} babies'.format(babies)
    parents, babies = (babies, parents + babies)
EOT

chmod +x fibonacci.py

Then run the script and you should see the output as below:

./fibonacci.py
This generation has 1 babies
This generation has 2 babies
This generation has 3 babies
This generation has 5 babies
This generation has 8 babies
This generation has 13 babies
This generation has 21 babies
This generation has 34 babies
This generation has 55 babies
This generation has 89 babies

So we can run the script any time we want which is great, but lets say we want to run this every evening and capture the output to a log file? We can use cron to schedule this at a certain time and use redirection to store the output in a file.

Below is an example crontab entry for running fibonacci.py each night at 11PM and redirecting the output to a file:

crontab -l
0 23 * * * /opt/fibonacci.py >> /var/log/fibonacci.log 2>&1

The below is the output of /var/log/fibonacci.log after the crontab entry has run:

cat /var/log/fibonacci.log
This generation has 1 babies
This generation has 2 babies
This generation has 3 babies
This generation has 5 babies
This generation has 8 babies
This generation has 13 babies
This generation has 21 babies
This generation has 34 babies
This generation has 55 babies
This generation has 89 babies

So there you have it. I hope this article sheds some light on how you can use cron to schedule your Python scripts to perform useful functions and log their outputs. If you have any interesting implementations of this in production I’d be interested to hear about them.

Subscribe to my feed either by E-mail or by RSS to receive updates as they happen.

Can you improve on any of the tips I’ve discussed here? If you can let me know in the comments.

Jason Edwards