Monday, 9 June 2014

Scheduling Python Programs using Cron

Cron has become one of my favourite utilities in Linux. It allows you to schedule tasks to automatically happen at certain times. It is one of those tools that is so simple and so powerful, but all too often overlooked, or not known about.



I first stumbled across Cron when I decided to set up and administer a wiki server for the company I work for. There is a lot of data on the wiki and it is important that it gets backed up regularly.

Initially I would take a dump of the MySQL database, and then copy the files over on a daily basis by hand.

I then wrote a Python script which carried out these tasks for me. I just had to remember to run the script on a daily basis.

Finally I automated this using Cron.
  • Now every morning (Monday - Friday) at 1:00 am the company wiki is automatically backed up to two locations.
  • Secondly on the 1st of every Month at 2:00 am the wiki is automatically backed up to two different locations.

All this is done without me ever having to worry if it has been done. Cron looks after it all for me.

What is more is it is very simple to use.

This tutorial will help you get to grips with Cron and have your Raspberry Pi or any other Linux computer working away for you in the background.

Firstly Cron does require the time on your Linux computer to be correct, otherwise tasks will not happen when you expect them to! Not that such a basic mistake would ever catch me out while writing this tutorial...

So if you are using a Raspberry Pi, it might be worth setting the time at this point.

type

sudo raspi-config

into the command line.

Then choose

Internationalisation Options > Change Timezone > Europe > London



for those of you in the UK. Obviously everyone else choose a location to suit your local timezone!

For the purposes of this blog post we will assume you are wanting to run a Python program called test.py saved on your Desktop. If you want an example Python program to run then save the following as a Python program called test.py. When run this will create a new folder called test on your Desktop.

import os

os.chdir("/home/pi/Desktop")
os.makedirs("test")

Now again in your command line we will edit a file called crontab, this is short for Cron Tables, a place where you store all your actions.

To edit crontab type the following into the command line.

sudo crontab -e

This will bring up a text file which looks as follows.


The majority of the text in this file briefly explains Cron and how to use it. The first thing you will notice is that all lines start with a #. This means these lines are ignored by Cron, they are for your reference only.

While there are some basic instructions included in this file the most useful line is the bottom line.

# m h dom mon dow command

These are all explained in the file to mean the following.
  • m - minute
  • h - hour
  • dom - day of month
  • mon - month
  • dow - day of week
  • command - This is the command you want to run.

Lets start with the command, as that's the easy bit.

If you want to automatically run a Python program the command is made up of two parts.

On the Raspberry Pi, if you are using Python 2.7, the first part of this will be as follows.

/usr/bin/python

or for Python 3

/usr/bin/python3

The second part is a link to the Python program. For a program called test.py saved on your desktop you would need to have

/home/pi/Desktop/test.py

These two parts are separated by a space to make up the command.

/usr/bin/python /home/pi/Desktop/test.py

These other parameters (m h dom mon dow) are what you will use to specify when your task will run. For each of them you can specify a number or if you want to use all possible values a * is used to signify this.
  • The minutes can be numbers from 0 - 59 and indicates the minute in the hour you want the task to run.
  • The hours can be from 0 - 23 and indicates the hour you want the task to run.
  • Day of the month, 1-31, indicates the date within the month in which you would like to run the task.
  • Month, 1-12, indicates in which month you would like to run the task.
  • Day of the Week, 0-6, indicates on which day you would like to run the task. 0 is Sunday, 1 is Monday etc. 
The secret to setting this correctly is to use a combination of these to define the schedule.

If you wanted to run your python program every hour, then you would only have to supply a value for the minutes, specifying at what point in the hour you wanted your program to run. For example to run your program every 35 minutes past the hour you would enter the following into your crontab.

35 * * * * /usr/bin/python /home/pi/Desktop/test.py

The hours and minutes are easy to work out. If you want to run something everyday at 4:35 you would simply create a line in your crontab which would say.

35 4 * * * /usr/bin/python /home/pi/Desktop/test.py

Making this run on the 12th of the month only you would need to put the figure 12 into the day of the month field also.

35 4 12 * * /usr/bin/python /home/pi/Desktop/test.py

Taking this further you could specify that you only want your program to run at 4:35 on the 12th March.

35 4 12 3 * /usr/bin/python /home/pi/Desktop/test.py

Alternatively you may want to to run at 4:35 every Monday, so would need to use the Day of the week field. The numbers from 0 - 6 represent the days of the week. 0 is a Sunday, 1 is Monday etc.

35 4 * * 1 /usr/bin/python /home/pi/Desktop/test.py

We have put the Day of the Month and the Month back to being a * for this to happen, as we don't want to limit when our program to run on the 12th March only. Leaving these values in would only run the program on the 12th March at 4:35 if that day actually happened to be a Monday! The * implies it can run any day or month.

Easy huh?

There are a few other special characters which help you define when your program should run.

A comma can be used to allow you to add more values into a field. For instance

35 4 * * 1,4 /usr/bin/python /home/pi/Desktop/test.py

The 1,4 indicates the program will run on Monday and Thursday.

Using a - allows you to choose a series of values.

35 4 * * 1-4 /usr/bin/python /home/pi/Desktop/test.py

The 1-4 indicates the program will run Monday - Thursday. ie. Monday, Tuesday, Wednesday and Thursday.

Finally a / denotes increments of ranges.

/15 * * * * /usr/bin/python /home/pi/Desktop/test.py

This will run the program every 15 minutes.

One final useful command is

@reboot /usr/bin/python /home/pi/Desktop/test.py

Will run the program when you reboot your Raspberry Pi or Computer.

From these few examples you have seen that Cron gives you a huge amount of flexibility on when you schedule tasks.

I hope you have found this blog post useful and that it will help you get started automating your programs.






27 comments:

  1. Hiiii....Thanks for sharing Great information...Nice post...Keep move on....
    Python Training in Hyderabad

    ReplyDelete
  2. Hi Trevor, thanks for much. Keep up the great work!

    ReplyDelete
  3. NAGAQQ: AGEN BANDARQ BANDARQ ONLINE ADUQ ONLINE DOMINOQQ TERBAIK

    Yang Merupakan Agen Bandarq, Domino 99, Dan Bandar Poker Online Terpercaya di asia hadir untuk anda semua dengan permainan permainan menarik dan bonus menarik untuk anda semua

    Bonus yang diberikan NagaQQ :
    * Bonus rollingan 0.5%,setiap senin di bagikannya
    * Bonus Refferal 10% + 10%,seumur hidup
    * Bonus Jackpot, yang dapat anda dapatkan dengan mudah
    * Minimal Depo 15.000
    * Minimal WD 20.000

    Memegang Gelar atau title sebagai Agen BandarQ Terbaik di masanya

    Games Yang di Hadirkan NagaQQ :
    * Poker Online
    * BandarQ
    * Domino99
    * Bandar Poker
    * Bandar66
    * Sakong
    * Capsa Susun
    * AduQ
    * Perang Bacarrat (New Game)

    Tersedia Deposit Via pulsa :
    Telkomsel & XL

    Info Lebih lanjut Kunjungi :
    Website : NagaQQ
    Facebook : NagaQQ Official
    Kontakk : Info NagaQQ
    linktree : Agen Judi Online
    WHATSAPP : +855977509035
    Line : Cs_nagaQQ
    TELEGRAM : +855967014811


    BACA JUGA BLOGSPORT KAMI YANG LAIN:
    agen bandarq terbaik
    Winner NagaQQ
    Daftar NagaQQ
    Agen Poker Online

    ReplyDelete
  4. CrownQQ Agen DominoQQ BandarQ dan Domino99 Online Terbesar

    Yuk Buruan ikutan bermain di website CrownQQ
    Sekarang CROWNQQ Memiliki Game terbaru Dan Ternama loh...

    9 permainan :
    => Poker
    => Bandar Poker
    => Domino99
    => BandarQ
    => AduQ
    => Sakong
    => Capsa Susun
    => Bandar 66
    => Perang Baccarat (NEW GAME)

    => Bonus Refferal 20%
    => Bonus Turn Over 0,5%
    => Minimal Depo 20.000
    => Minimal WD 20.000
    => 100% Member Asli
    => Pelayanan DP & WD 24 jam
    => Livechat Kami 24 Jam Online
    => Bisa Dimainkan Di Hp Android0619679319
    => Di Layani Dengan 5 Bank Terbaik
    => 1 User ID 9 Permainan Menarik

    Ayo gabung sekarang juga hanya dengan
    mengklick Agen BandarQ

    Link Resmi CrownQQ:
    - idcrownqq.com
    - idcrownqq.net
    - idcrownqq.org
    - idcrownqq.info

    Info Lebih lanjut Kunjungi :
    Website : CrownQQ
    Daftar CrownQQ : Poker Online
    Info CrownQQ : Kontakk

    WHATSAPP : +855882357563
    Line : CS CROWNQQ
    Facebook : CrownQQ Official
    Kemenangan CrownQQ : Agen BandarQ

    ReplyDelete
  5. SUPER BIG PROMO DI SITUS SAHABAT KARTU
    =============================================
    Hai Teman - Teman Smuanya, Yang Hobi Bermain Judi Online
    Anda Merasa Capek, DEPOSIT Teruss..Tapi Tidak Pernah Withdraw ?
    =============================================
    Nikmati Seluruh Game Berkualitas & Raih Kemenangan Anda Di SAHABAT KARTU!!
    Game Yang di Hadirkan SAHABAT KARTU Yaitu :
    * Poker Online
    * BandarQ
    * Domino99
    * Bandar Sakong
    * Sakong
    * BANDAR66
    * AduQ
    * Sakong
    * Perang Baccarat
    ==========================================
    SPECIAL PROMO KHUSUS MEMBER SETIA SAHABAT KARTU
    - Minimal DP & WD Cuma Rp. 20.000
    - BONUS CASHBACK 0.5% ( Setiap Hari Senin )
    - REFERRAL 15% ( Seumur Hidup )
    - 100% NON ROBOT & 100% TANPA ADMIN BERMAIN
    - 100% GAMPANG MENANG
    - JACKPOT HARIAN HINGGA JUTA RUPIAH
    - MENERIMA DEPOSIT VIA PULSA XL DAN TSEL TANPA POTONGAN
    ======================================
    AKSES KAMI DI LINK RESMI :
    - halokartu.com
    - halokartu.org
    - halokartu.info
    - halokartu.net
    =====================
    * Livechat : SAHABAT KARTU
    * LINE : Cs_sahabatkartu
    * Whatsapp : +85581734028

    Sahabatkartu: Situs Poker Online, DominoQQ, Domino99, BandarQ Terpercaya

    ReplyDelete
  6. sdomino99.org Merupakan Salah Satu Situs yang terpercaya Di Indonesia
    dominowin99 Memiliki Permainan Yang Mudah Dimainkan & 100% Mudah Menang Lohh..
    Cukup Dengan 1 USER ID Anda Bisa Bermain 9 GAME Berkualitas :
    * Poker
    * Domino99
    * AduQ
    * Capsa Susun
    * Sakong
    * Bandar Poker
    * BANDARQ ONLINE
    * BANDAR66 ONLINE
    +++++++++++++++++++++++++++++++
    AKSES LINK ALTERNATIF TERBARU :
    - sdomino99.com
    - sdomino99net
    - sdomino99.org
    - sdomino99.info

    < Contact Us >
    Info Lebih Lanjut Hubungi :
    W.A : +6285974599065


    Sahabatdomino : Situs QQ Online, Agen Domino99 dan BandarQ Online Terbesar Di Asia

    ReplyDelete
  7. Mau Dapatkan Uang Dengan Mudah...
    Yuk join bersama Rajabandarq Situs Bandarq, DominoQQ, Poker Online terbaik di asia dengan 9 game yang paling seru...

    Buruan Daftar Disini >>> Rajabandarq
    Klik Disini : Situs BandarQ
    Klik Disini : Poker Online

    Baca juga :

    - Blogger : 24berita harian
    - Blogger : Kisahselebindo
    - Blogger : myrenew22
    - Blogger : Panduan Bermain Game Judi Online

    ReplyDelete
  8. MEJAQQ: AGEN JUDI POKER DOMINOQQ BANDARQ ONLINE TERBESAR DI ASIA

    Yang Merupakan Agen Judi Poker DominoQQ BandarQ Online Terbesar di Asia Hadir Untuk Anda Semua Dengan Games dan Bonus Yang Menarik!

    Bonus yang Kami Berikan di MEJAQQ :
    * Bonus Rollingan 0.5% Kali 2
    * Bonus Referral 10% + 10% Seumur Hidup (X-tra Untuk Para Pencari Bonus Referral di Indonesia)
    * Dan Masih Banyak Bonus Menarik Lainnya yang Bisa Anda Dapatkan di MEJAQQ!
    *Dan Kami Juga Menyediakan Deposit Pulsa Tanpa Adanya Potongan Pulsa!
    Buruan Kunjungi Sekarang Juga ^^

    9 Games Yang di Hadirkan MEJAQQ:
    *- POKER
    *- DOMINO QQ
    *- CAPSA SUSUN
    *- BANDAR POKER
    *- BANDAR Q
    *- BANDAR 66
    *- SAKONG
    *- ADU Q
    *- PERANG BACCARAT

    Info Lebih lanjut Kunjungi :
    Website : Mejayes(.)org
    Line : Mejaqq_official
    WA 1 : +85515620767
    TELEGRAM : +85515620767

    Kunjungi Juga situs kami di :
    MEJAQQ
    MEJAYES
    BANDARQ ONLINE
    QQ ONLINE
    Informasi Terbaru

    ReplyDelete
  9. MEJAQQ: AGEN JUDI POKER DOMINOQQ BANDARQ ONLINE TERBESAR DI ASIA

    Yang Merupakan Agen Judi Poker DominoQQ BandarQ Online Terbesar di Asia Hadir Untuk Anda Semua Dengan Games dan Bonus Yang Menarik!

    Bonus yang Kami Berikan di MEJAQQ :
    * Bonus Rollingan 0.5% Kali 2
    * Bonus Referral 10% + 10% Seumur Hidup (X-tra Untuk Para Pencari Bonus Referral di Indonesia)
    * Dan Masih Banyak Bonus Menarik Lainnya yang Bisa Anda Dapatkan di MEJAQQ!
    *Dan Kami Juga Menyediakan Deposit Pulsa Tanpa Adanya Potongan Pulsa!
    Buruan Kunjungi Sekarang Juga ^^

    9 Games Yang di Hadirkan MEJAQQ:
    *- POKER
    *- DOMINO QQ
    *- CAPSA SUSUN
    *- BANDAR POKER
    *- BANDAR Q
    *- BANDAR 66
    *- SAKONG
    *- ADU Q
    *- PERANG BACCARAT

    Info Lebih lanjut Kunjungi :
    Website : Mejayes(.)org
    Line : Mejaqq_official
    WA 1 : +85515620767
    TELEGRAM : +85515620767

    Kunjungi Juga situs kami di :
    MEJAQQ
    MEJAYES
    BANDARQ ONLINE
    QQ ONLINE
    Informasi Terbaru

    ReplyDelete
  10. There are few content post set to work information for the common.
    Standards should be set like this to lift the work load a little higher and priortize information.
    However, we are kitten rearers and have a list to tell whom it may concern namely ;
    Kittens for sale
    Cats for sale
    Ragdoll kittens for sale
    Ragdoll cats for sale
    Ragamuffin for sale
    Ragamuffin cat for sale
    Ragamuffin kittens for sale
    Munchkin cats for sale
    Munchkin for sale
    Munchkin kittens for sale
    Thanks for the follow up.

    ReplyDelete

  11. All my Pomeranian puppies are family raised under foot in our home. I take great pride in producing healthy exotic Pomeranian for sale and for people to enjoy and love for many years to come. I breed to share the love I have for Pomeranian for sale and to better the breed! All my Pomeranian puppies are sold with limited (pet) AKC papers unless we agree that particular Pomeranian puppy can be sold with Full AKC (breeding/show) rights for an additional fee, up to date on shots/wormed, socialized and all puppies come with a bag full of goodies! If you are interested in one of my puppies please read the "Our Guarantee Limited AKC" or "Our Guarantee Full AKC" page because it's part of my contract that I will require you to read before making your final decision on one of my puppies.





    pomeranian puppies for sale near me
    pomeranian teddy bear puppies for sale
    cheap puppies for sale
    pomeranian puppies for sale in oregon
    pomeranian puppies
    pomeranian
    pomeranian puppies for sale
    cute
    il
    puppies for sale near me
    pomeranian puppies
    puppys
    teacup puppies
    pomeranian puppy
    pomeranian dog
    puppy for sale
    pomeranian for sale
    white pomeranian
    pomeranians
    pomeranian puppies for sale
    small dogs for sale
    black pomeranian
    dog for sale
    tea cup puppy
    akc puppy finder
    pomeranian mix
    puppy for sale near me

    ReplyDelete