Forums

Long running task

Hello I need some explanation on how to run a long-term process

import logging
import socket
import sys

lock_socket = None  # we want to keep the socket open until the very end of
                    # our script so we use a global variable to avoid going
                    # out of scope and being garbage-collected

def is_lock_free():
    global lock_socket
    lock_socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
    try:
        lock_id = "mgaforever.Myscript"   # 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,))
        return True
    except socket.error:
        # socket already locked, task must already be running
        logging.info("Failed to acquire lock %r" % (lock_id,))
        return False

if not is_lock_free():
    print("execute")
    sys.exit()

import Myscript

from what I understand I have to create a planned activity in which every hour I launch this script is correct?

[edited by admin: formatting]

yes- so set it up on your scheduled tasks so that say this script gets run every hour. when it does run, it either exits early because a similar one is already running, or it runs and starts up. How it "starts up" is basically the import Myscript, do xyz from Myscript part.