K
K
KOS_MOS2011-03-18 18:37:56
Python
KOS_MOS, 2011-03-18 18:37:56

Running only one copy of a Python script?

There is a script in python, it is launched by cron, it can run for a long time, launching two copies is undesirable.
Are there ready-made solutions, how to run a script only if the previous one has already worked?
Windows platform, but I would like cross-platform.

Answer the question

In order to leave comments, you need to log in

8 answer(s)
I
IlVin, 2011-03-18
@IlVin

How do I prevent a second copy of the script.
At startup:
1) I create a lock file
2) I make a flock on the lock file (LOCK_NB mode). If it doesn't work, then shutdown.
3) I write pid into it
4) I remember the inode of the file
When the script is running, I do a periodic check:
1) I check the existence of the file. If the file does not exist, exit.
2) I check the inode of the file. If it doesn't match, shutdown
3) I read pid. If it doesn't match the real one, shutdown
When the script ends:
1) I close the lock file.

G
glebkk, 2011-03-18
@glebkk

I did it like this.

  pidfile = "/tmp/server.pid"
  def check_pid(pid):
    """ Check For the existence of a unix pid. """
    try:
      os.kill(pid, 0)
    except OSError:
      return False
    else:
      return True
  if os.path.isfile(pidfile):
    pid = long(open(pidfile, 'r').read())
    if check_pid(pid):
      print "%s already exists, exiting" % pidfile
      sys.exit()
  pid = str(os.getpid())
  file(pidfile, 'w').write(pid)
  # actual code
  # finish
  os.unlink(pidfile)
* This source code was highlighted with Source Code Highlighter.

S
Sergey Lerg, 2011-03-18
@Lerg

Another way is to check if the network port on which the script is listening is busy. If it was not possible to occupy the port, then the script is already running - exit.

E
el777, 2011-03-19
@el777

I also used to do everything through manual lock-files. But this is more crap than good.
Linux has a good lckdo utility - now I run all cron tasks through it.
Perhaps it is also for Windows.

P
phikus, 2011-03-21
@phikus

*/5 * * * * pgrep -f script_name &>/dev/null || script_name

V
Vsevolod, 2011-03-18
@sevka_fedoroff

In Linux, for this, I get a list of processes at the start of the script, executing the “ps” command of the operating system, and check if such a process is already hanging there. I don't know if it's possible to get a list of processes in Windows.

V
Vlad Frolov, 2011-03-18
@frol

Ready solution: lockfile .

O
Oleg Chirukhin, 2011-03-18
@olegchir

The topic of lockfiles has already been covered, but this is not the only way to check that the script is already running.
For example, you can check using a wrapper that can respond to requests over the network. When such a wrapper starts a second time, it sends out a broadcast (how - depends on the level of perversion of the author), and listens to the answers.
Lock files have a minus: if the script crashes with an error before it completes, then there will be no one to delete the lock file, the system will forever assume that the script is running. On the other hand, the network wrapper does not have this problem. The second plus is that this solution can be expanded to several servers in the future, and almost nothing will have to be changed. The main disadvantage is that this canoe will have to be written, which will take some time ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question