Welcome, guest | Sign In | My Account | Store | Cart

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

Python, 18 lines
 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", [""])

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)

Created by Michal Wallace on Tue, 16 Aug 2005 (PSF)
Python recipes (4591)
Michal Wallace's recipes (1)

Required Modules

Other Information and Tasks