Archive

Posts Tagged ‘crontab’

APScheduler3 examples

June 26, 2019 Leave a comment

A long time ago (in 2013) I wrote a post about APScheduler, see here. Since then a new version of APScheduler has come out, so it was time to update that old post.

So, the examples below were tested with APScheduler 3.6. The source code of the examples can be found on GitHub too (here).

#!/usr/bin/env python3

import os
from apscheduler.schedulers.blocking import BlockingScheduler
import mylogging as log

sched = BlockingScheduler()


@sched.scheduled_job('interval', minutes=60)
def once_per_hour():
    """
    If you lauch this script at time T, then this function will be called
    at T+60 minutes for the first time.
    Ex.: if you lauch the script at 13h07, then this function will be called at 14h07
    for the first time.
    """
    log.info('calling once_per_hour')


@sched.scheduled_job('interval', minutes=2)
def once_per_hour():
    """
    Call it every 2 minutes.
    """
    log.info('2 minutes passed')


@sched.scheduled_job('cron', hour='*/4', minute=2)
def four_hours():
    """
    Run this function every four hour + 2 minutes.
    Ex.: it's called at 00h02, 04h02, 08h02, etc.
    """
    log.info('calling four_hours')


@sched.scheduled_job('cron', day='*', hour=0, minute=5)
def daily_backup():
    """
    Run it once a day at 5 minutes after midnight.

    !!! If it takes time, then don't do the work here because the work
    here will block the calling of the other functions! If it takes time, then
    simply launch the work in the background. Here the slow work is collected in
    a batch file and the batch file is launched in the background.
    """
    log.info('calling daily_backup')
    os.system("./daily_backup.sh &")


@sched.scheduled_job('cron', day='*', hour=0)
def midnight():
    """
    Call it at midnight.
    """
    log.info('calling midnight')


@sched.scheduled_job('cron', day='*', hour=18)
@sched.scheduled_job('cron', day='*', hour=19)
@sched.scheduled_job('cron', day='*', hour=20)
def triple_check():
    """
    Call this function every day at 18h00, 19h00 and 20h00.
    """
    log.info('calling triple_check')


@sched.scheduled_job('cron', day_of_week='wed', hour=19, minute=0)
@sched.scheduled_job('cron', day_of_week='sun', hour=19, minute=0)
def mini_backup():
    """
    Call this function on Wednesday at 19h00 and
    on Sunday at 19h00.
    """
    log.info('calling mini_backup')


def main():
    log.info('the scheduler is running...')
    sched.start()

##############################################################################

if __name__ == "__main__":
    main()

Find the complete source code on GitHub (here).

Categories: python Tags: , , ,

Calling a Python script from crontab

March 31, 2012 6 comments

Problem
You want to call a Python script of yours from crontab but it doesn’t really want to work :(

Solution
First, redirect everything to a file, thus you can check the error messages. For testing, let’s call the script at every minute. Edit your crontab settings with “crontab -e“.

* * * * * python /absolute/path/to/script.py >>/tmp/out.txt 2>&1

Don’t redirect to /var/log/cronrun (as many guides suggest), because if you have no rights to modify it, you won’t see anything… Choose a file where you can write to.

Then start checking the output of the log file:

touch /tmp/out.txt
tail -f /tmp/out.txt

tail -f” monitors the file constantly and prints every change.

Bare in mind that crontab doesn’t read your startup settings from .bashrc for instance, thus if you added something to PYTHONPATH, it won’t be visible! You have to repeat your PYTHONPATH settings in crontab too. “Each directory you need will have to be listed, because you can’t append to it since the previous value isn’t available.” (tip from here)

PYTHONPATH=/dir1:/dir2
* * * * * python /absolute/path/to/script.py >>/tmp/out.txt 2>&1

If it works, customize the execution time of the script by changing the “* * * * *” part.

Troubleshooting
You may also have problems with imports. Say your script wants to import another module from a different folder. In this case you can have a look at this post: Import a file from another directory of the project.

Related posts

Categories: python Tags: ,

Create a temporary file with unique name

February 19, 2011 Leave a comment

Problem

I wanted to download an html file with Python, store it in a temporary file, then convert this file to PDF by calling an external program.

Solution #1

#!/usr/bin/env python

import os
import tempfile

temp = tempfile.NamedTemporaryFile(prefix='report_', suffix='.html', dir='/tmp', delete=False)

html_file = temp.name
(dirName, fileName) = os.path.split(html_file)
fileBaseName = os.path.splitext(fileName)[0]
pdf_file = dirName + '/' + fileBaseName + '.pdf'

print html_file   # /tmp/report_kWKEp5.html
print pdf_file    # /tmp/report_kWKEp5.pdf
# calling of HTML to PDF converter is omitted

See the documentation of tempfile.NamedTemporaryFile here.

Solution #2 (update 20110303)

I had a problem with the previous solution. It works well in command-line, but when I tried to call that script in crontab, it stopped at the line “tempfile.NamedTemporaryFile”. No exception, nothing… So I had to use a different approach:

from time import time

temp = "report.%.7f.html" % time()
print temp    # report.1299188541.3830960.html

The function time() returns the time as a floating point number. It may not be suitable in a multithreaded environment, but it was not the case for me. This version works fine when called from crontab.

Learn more

Update (20150712): if you need a temp. file name in the current directory:

>>> import tempfile
>>> tempfile.NamedTemporaryFile(dir='.').name
'/home/jabba/tmpKrBzoY'

Update (20150910): if you need a temp. directory:

import tempfile
import shutil

dirpath = tempfile.mkdtemp()    # the temp dir. is created
# ... do stuff with dirpath
shutil.rmtree(dirpath)

This tip is from here.

Categories: python Tags: , , , , , ,