11
11
from canonical.config import config
13
def pidfile_path(service_name):
13
def pidfile_path(service_name, use_config=None):
14
14
"""Return the full pidfile path for the given service
16
>>> pidfile_path('nuts') == '/tmp/%s-nuts.pid' % config.instance_name
16
>>> pidfile_path('nuts') == '/var/tmp/%s-nuts.pid' % config.instance_name
19
You can pass in your own config instance to use.
23
... pid_dir = '/var/tmp'
24
... instance_name = 'blah'
25
>>> pidfile_path('beans', MyConfig)
26
'/var/tmp/blah-beans.pid'
19
return os.path.join(config.canonical.pid_dir, '%s-%s.pid' % (
20
config.instance_name, service_name
28
if use_config is None:
30
return os.path.join(use_config.canonical.pid_dir, '%s-%s.pid' % (
31
use_config.instance_name, service_name
96
107
os.rename(tempname, pidfile)
99
def remove_pidfile(service_name):
110
def remove_pidfile(service_name, use_config=None):
100
111
"""Remove the PID file.
102
113
This should only be needed if you are overriding the default SIGTERM
116
>>> path = pidfile_path('legumes')
117
>>> file = open(path, 'w')
119
... print >> file, os.getpid()
122
>>> remove_pidfile('legumes')
123
>>> os.path.exists(path)
126
You can also pass in your own config instance, in which case the pid does
127
not need to match the current process's pid.
131
... pid_dir = '/var/tmp'
132
... instance_name = 'blah'
133
>>> path = pidfile_path('pits', MyConfig)
135
>>> file = open(path, 'w')
137
... print >> file, os.getpid() + 1
140
>>> remove_pidfile('pits', MyConfig)
141
>>> os.path.exists(path)
105
pidfile = pidfile_path(service_name)
106
if os.path.exists(pidfile):
107
# Check that the PID is actually ours in case something overwrote
108
# it or we are forked.
109
pid = open(pidfile).read()
113
raise ValueError("Invalid PID %s" % repr(pid))
114
if pid == os.getpid():
118
def get_pid(service_name):
144
pidfile = pidfile_path(service_name, use_config)
145
pid = get_pid(service_name, use_config)
148
if use_config is not None or pid == os.getpid():
152
def get_pid(service_name, use_config=None):
119
153
"""Return the PID for the given service as an integer, or None
121
155
May raise a ValueError if the PID file is corrupt.
134
168
>>> remove_pidfile('nuts')
135
169
>>> get_pid('nuts') is None
172
You can also pass in your own config instance.
176
... pid_dir = '/var/tmp'
177
... instance_name = 'blah'
178
>>> path = pidfile_path('beans', MyConfig)
180
'/var/tmp/blah-beans.pid'
181
>>> file = open(path, 'w')
183
... print >> file, 72
186
>>> get_pid('beans', MyConfig)
138
pidfile = pidfile_path(service_name)
190
pidfile = pidfile_path(service_name, use_config)
140
192
pid = open(pidfile).read()