usr bin env python coding utf-8 import os import os path import sys im

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import os.path
import sys
import atexit
import signal
import errno
import subprocess as sp
UMASK = 0
WORKDIR = '/'
#UID = 101
NAME = 'talisman'
#if os.getuid() != 0:
# PIDFILE='/var/run/%s.pid' % (NAME,)
#else:
PIDFILE = os.path.expanduser('~/.%s.pid' % (NAME, ))
def daemonize(stdin='/dev/null',
stdout='/dev/null',
stderr='/dev/null'):
try:
pid = os.fork()
except OSError, err:
raise Exception, "%s [%d]" % (err.strerror, err.errno)
if pid == 0:
os.setsid()
try:
pid = os.fork()
except OSError, err:
raise Exception, "%s [%d]" % (err.strerror, err.errno)
if pid == 0:
os.chdir(WORKDIR)
else:
os._exit(0)
else:
os._exit(0)
si = open(stdin, 'r')
so = open(stdout, 'a+')
se = open(stderr, 'a+', 0)
os.dup2(si.fileno(), sys.stdin.fileno())
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())
return 0
def readpid():
pidf = open(PIDFILE, 'r')
pid = pidf.read()
pidf.close()
return int(pid)
def writepid():
pidf = open(PIDFILE, 'w')
pidf.write("%d" % os.getpid())
pidf.close
def cleanup():
os.unlink(PIDFILE)
def reg_signals():
signal.signal(signal.SIGINT, lambda *args: quit())
signal.signal(signal.SIGQUIT, lambda *args: quit())
def check_pid():
"""Checks if invalid pidfile exists
or process is currently running"""
if os.path.exists(PIDFILE):
pid = readpid()
try:
os.kill(pid, 0)
except OSError, err:
if err.errno == errno.ESRCH:
print "Stale pidfile exists. Removing..."
os.unlink(PIDFILE)
else:
sys.exit("Valid pidfile exists. Exiting...")
def run():
def main():
check_pid()
return 0
if __name__ == '__main__':
sys.exit( main() )