k3daemonize
Create daemon processes with CLI for start/stop/restart. Identifies daemons by PID file to prevent duplicate processes.
k3daemonize is a component of pykit3 project: a python3 toolkit set.
Installation
pip install k3daemonize
Quick Start
import time
import k3daemonize
def run():
for i in range(100):
print(i)
time.sleep(1)
# python foo.py start
# python foo.py stop
# python foo.py restart
if __name__ == '__main__':
k3daemonize.daemonize_cli(run, '/var/run/pid')
API Reference
k3daemonize
Help to create daemon process. It supplies a command line interface API to start/stop/restart a daemon.
daemonize identifies a daemon by the pid file.
Thus two processes those are set up with the same pid file
can not run at the same time.
Daemon
Bases: object
Source code in k3daemonize/daemonize.py
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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | |
daemonize()
do the UNIX double-fork magic, see Stevens' "Advanced Programming in the UNIX Environment" for details (ISBN 0201563177) http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
Source code in k3daemonize/daemonize.py
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 | |
daemonize_cli(run_func, pidfn, close_fds=False)
Read command line arguments and then start, stop or restart a daemon process.
:param run_func: a callable object such as a function or lambda to run after the daemon
process is created.
:param pidfn: abosolute path of pid file. It is used to identify a daemon process.
Thus two processes those are with the same pid file can not run at the same time.
:param close_fds: If it is True, besides stdin, stdout and stderr, all other file descriptors
will also be closed.
:return: None
Source code in k3daemonize/daemonize.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | |
License
The MIT License (MIT) - Copyright (c) 2015 Zhang Yanpo (张炎泼)