kokeksibir
kokeksibir

Reputation: 1745

Running shell script with web2py scheduler

I need to manage some scheduled tasks controlled by web2py web interface. For that I want to utilize web2py's scheduler. First thing came into my mind is to run shell scripts using subprocess module inside scheduler function, though I am not sure if it is the correct way. Is there a best practice for that? Do you have any advices?

Upvotes: 2

Views: 2079

Answers (2)

syk435
syk435

Reputation: 308

That's exactly how I ran it the one time I used it, if my anecdotal experience comforts you. Led to smooth results.

Upvotes: 0

espern
espern

Reputation: 659

Depends on where you are hosting your web2py app. If you host it on PythonAnywhere, you can use the scheduled tasks to run web2py's scheduler.

For example you can create a daily task as following :

#/usr/bin/env python
import logging
import socket
import sys
import subprocess

lock_socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
try:
    lock_id = "MyApp-scheduler"   # this should be unique. using your username as a prefix is a convention
    lock_socket.bind('\0' + lock_id)
    logging.debug("Acquired lock %r" % (lock_id,))
except socket.error:
    # socket already locked, task must already be running
    logging.info("Failed to acquire lock %r" % (lock_id,))
    sys.exit()

subprocess.call(["python","web2py/web2py.py","-K","MyApp"])

Upvotes: 6

Related Questions