Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download
Views: 39598
1
# -*- coding: utf-8 -*-
2
# Copyright (c) 2009-2010 Sauce Labs Inc
3
#
4
# Portions taken from twistd:
5
#
6
# Copyright (c) 2001-2009
7
# Allen Short
8
# Andrew Bennetts
9
# Apple Computer, Inc.
10
# Benjamin Bruheim
11
# Bob Ippolito
12
# Canonical Limited
13
# Christopher Armstrong
14
# David Reid
15
# Donovan Preston
16
# Eric Mangold
17
# Itamar Shtull-Trauring
18
# James Knight
19
# Jason A. Mobarak
20
# Jean-Paul Calderone
21
# Jonathan Lange
22
# Jonathan D. Simms
23
# Jürgen Hermann
24
# Kevin Turner
25
# Mary Gardiner
26
# Matthew Lefkowitz
27
# Massachusetts Institute of Technology
28
# Moshe Zadka
29
# Paul Swartz
30
# Pavel Pergamenshchik
31
# Ralph Meijer
32
# Sean Riley
33
# Software Freedom Conservancy
34
# Travis B. Hartwell
35
# Thomas Herve
36
# Eyal Lotem
37
# Antoine Pitrou
38
# Andy Gayton
39
#
40
# Permission is hereby granted, free of charge, to any person obtaining
41
# a copy of this software and associated documentation files (the
42
# "Software"), to deal in the Software without restriction, including
43
# without limitation the rights to use, copy, modify, merge, publish,
44
# distribute, sublicense, and/or sell copies of the Software, and to
45
# permit persons to whom the Software is furnished to do so, subject to
46
# the following conditions:
47
#
48
# The above copyright notice and this permission notice shall be
49
# included in all copies or substantial portions of the Software.
50
#
51
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
52
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
53
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
54
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
55
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
56
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
57
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
58
59
import os
60
import sys
61
import errno
62
63
def basic_daemonize():
64
# See http://www.erlenstar.demon.co.uk/unix/faq_toc.html#TOC16
65
if os.fork(): # launch child and...
66
os._exit(0) # kill off parent
67
os.setsid()
68
if os.fork(): # launch child and...
69
os._exit(0) # kill off parent again.
70
os.umask(022) # Don't allow others to write
71
null=os.open('/dev/null', os.O_RDWR)
72
for i in range(3):
73
try:
74
os.dup2(null, i)
75
except OSError, e:
76
if e.errno != errno.EBADF:
77
raise
78
os.close(null)
79
80
81
def writePID(pidfile):
82
open(pidfile,'wb').write(str(os.getpid()))
83
if not os.path.exists(pidfile):
84
raise Exception( "pidfile %s does not exist" % pidfile )
85
86
87
def checkPID(pidfile):
88
if not pidfile:
89
return
90
if os.path.exists(pidfile):
91
try:
92
pid = int(open(pidfile).read())
93
except ValueError:
94
sys.exit('Pidfile %s contains non-numeric value' % pidfile)
95
try:
96
os.kill(pid, 0)
97
except OSError, why:
98
if why[0] == errno.ESRCH:
99
# The pid doesnt exists.
100
print('Removing stale pidfile %s' % pidfile)
101
os.remove(pidfile)
102
else:
103
sys.exit("Can't check status of PID %s from pidfile %s: %s" %
104
(pid, pidfile, why[1]))
105
else:
106
sys.exit("Another server is running, PID %s\n" % pid)
107
108
def daemonize(pidfile):
109
checkPID(pidfile)
110
basic_daemonize()
111
writePID(pidfile)
112
113
114