2
# -*- coding: utf-8 -*-
4
import os, sys, shutil, tempfile
7
# ensure that single instance of applet is running for each user
8
class SingleInstance(object):
10
#Initialize, specifying a path to store pids
11
def __init__(self,pidPath):
13
self.pidPath = pidPath
14
self.lasterror = False
15
if os.path.exists(pidPath):
16
# Make sure it is not a "stale" pidFile
17
pid = open(pidPath, 'r').read().strip()
18
# Check list of running pids, if not running it is stale so overwrite
19
pidRunning = subprocess.getoutput('ls -1 /proc | grep ^%s$' % pid)
20
self.lasterror = True if pidRunning else False
22
self.lasterror = False
24
if not self.lasterror:
25
# Create a temp file, copy it to pidPath and remove temporary file
26
(fp, temp_path) = tempfile.mkstemp()
28
os.fdopen(fp, "w+b").write(bytes(os.getpid()))
29
shutil.copy(temp_path, pidPath)
31
except Exception as e:
34
def is_already_running(self):
38
if not self.lasterror and os.path.exists(self.pidPath):
39
os.unlink(self.pidPath)