How to wake up a Python script, while you are in a sound sleep?


We all know that programmes die after execution.Data may be persistent if serialized.So consider the case that we need to backup all the logs,or delete something periodically.We need a scheduler to do that.One great scheduler available for Linux systems is CRON scheduler.There are two things having subtle importance here.

1) How to use CRON scheduler to execute any command on a Linux computer.

2)How to use APScheduler to run some functions in a python script for a particular time.


The crontab (cron derives from chronos, Greek for time; tab stands for table) command, found in Unix and Unix-like operating systems, is used to schedule commands to be executed periodically. To see what crontabs are currently running on your system, you can open a terminal and run:

$ crontab -l

If we want to create new job or edit the existing crontab job Just type

$ crontab -e
 If we wish to remove crontabs just type

$ crontab -r

It removes all crontabs.What is crontab ?,it is a file to which jobs are added .So add jobs to the end of that file by launching command “$ crontab -e”

This will open a the default editor (could be vi or pico, if you want you can change the default editor) to let us manipulate the crontab. If you save and exit the editor, all your cronjobs are saved into crontab. Cronjobs are written in the following format,here any valid command can be used after 5 *:

* * * * * /bin/execute/this/
* * * * * [any_valid_linux_command]



As you can see there are 5 stars. The stars represent different date parts in the following order:

  • minute (from 0 to 59)
  • hour (from 0 to 23)
  • day of month (from 1 to 31)
  • month (from 1 to 12)
  • day of week (from 0 to 6) (0=Sunday)

Execute every minute

If you leave the star, or asterisk, it means every. Maybe that’s a bit unclear. Let’s use the the previous example again:

* * * * * python /home/execute/this/

They are all still asterisks! So this means execute /home/execute/this/

  • every minute
  • of every hour
  • of every day of the month
  • of every month
  • and every day in the week.

In short: This script is being executed every minute. Without exception.

Execute every Friday 1AM

So if we want to schedule the python script to run at 1AM every Friday, we would need the following cronjob:

0 1 * * 5 python /home/aryan/this/

Get it? The script is now being executed when the system clock hits:

  • minute: 0
  • of hour: 1
  • of day of month: * (every day of month)
  • of month: * (every month)
  • and weekday: 5 (=Friday)

Execute on workdays 1AM

So if we want to schedule the python script to Monday till Friday at 1 AM, we would need the following cronjob:

0 1 * * 1-5 python /bin/execute/this/

Neat scheduling tricks

What if you’d want to run something every 10 minutes? Well you could do this:

0,10,20,30,40,50 * * * * python /bin/execute/this/

But crontab allows you to do this as well:

*/10 * * * * python /bin/execute/this/


Storing the crontab output

By default cron saves the output of /bin/execute/this/ in the user’s mailbox (root in this case). But it’s prettier if the output is saved in a separate logfile. Here’s how:

*/10 * * * * python /bin/execute/this/ >> /var/log/script_output.log 2>&1

Linux can report on different levels. There’s standard output (STDOUT) and standard errors (STDERR). STDOUT is marked 1, STDERR is marked 2. So the following statement tells Linux to store STDERR in STDOUT as well, creating one datastream for messages & errors:


 this is a shortcut illustration to up and run with cron

So now we understood how to run a python script for particular time.This is doing outside the program,means scheduling python programme manually by programmer.But sometimes we require to schedule from inside the program.For that we use a good library called can install it by using the following command.

$ sudo pip install apscheduler
Ok after installing APScheduler,we can see how simple scheduling any job.Here we are going to a level deeper and schedule python functions to execute at a particular time.Here jobs are python functions.
from apscheduler.scheduler import Scheduler
#start the scheduler i.e create instance
def my_job():
    print 'Happy_Birthday,Aryan'
#schedules job function my_job to greet me every year on my Birthday
 So this script greets me on my birthday by running the function every year.Running the script is done by the step 1 i.e cron scheduling,and inside the script scheduling is handled by APScheduler,see how goodies are provided for us.Here my_job is doing simple task but in real time systems,this mean anything like taking backup,deleting logs,housekeeping etc.
There are lots of things we can do with APScheduler,by adding jobs to Mongostore,redis store etc.For full fledged documentation kindly go to
and especially for cron format go to this page,and explore yourself.

Last but not least

Sometimes we need to create some python applications for operating systems using  KDE,GTK+.So those applications should run at system start up ,for that we need to add this  below command in the crontab file.Just add this simple line in crontab file

@reboot python /home/arya/ &
 here @reboot tells that command should be executed at reboot,and & tells that process to run background.This finishes our little chit chat about CRON and APScheduler.Hope you enjoyed this post.

2 thoughts on “How to wake up a Python script, while you are in a sound sleep?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s