ActiveState Code

Recipe 439353: avoid running multiple copies of pine


This example shows how to stop yourself from running multiple copies of a program (in this case, pine).

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#!/bin/env/python
import os

# figure out what user I am:
myusr = os.popen("/usr/bin/whoami").read().strip()

# see if pine is already running for me:
pines = os.popen("/usr/bin/pgrep -u %s pine" % myusr).read().split()

# but *this* script is called pine, so take it out of the list:
mypid = str(os.getpid())
pines.remove(mypid)

if pines:
    print "pine is already running."
else:
    # replace this process with the real thing:
    os.execv("/usr/bin/pine", [""])

Discussion

I tend to have multiple terminals open, and I'm always starting pine when I already have a copy open somewhere else. Running a second instance locks both instances up for a few seconds, and it's annoying, so I wrote this script.

Just call this script "pine" and put it on your path before the "real" pine. (You can set your path in your ~/.bash_profile file, or the equivalent for whatever shell you prefer)

Sign in to comment