usr bin env python coding utf8 import sys os time from signal import S

 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
#!/usr/bin/env python
# -*- coding: utf8 -*-
import sys, os, time
from signal import SIGTERM
import setting
def test():
sys.stdout.write ('Message to stdout...')
sys.stderr.write ('Message to stderr...')
c = 0
while 1:
sys.stdout.write ('%d: %s\n' % (c, time.ctime(time.time())) )
sys.stdout.flush()
c = c + 1
time.sleep(myvar.daemon_sleep)
def startstop(stdout='/dev/null', stderr=None, stdin='/dev/null', pidfile='pid.txt', startmsg = 'started with pid %s' ):
if len(sys.argv) > 1:
action = sys.argv[1]
try:
pf=open(pidfile)
pid = int(pf.read().strip())
pf.close()
except IOError:
pid = None
if ((action == 'stop') or (action == 'restart')):
if (not pid):
mess = "Не могу остановить, pid файл '%s' отсутствует.\n"
sys.stderr.write(mess % pidfile)
sys.exit(1)
try:
while 1:
os.kill(pid,SIGTERM)
time.sleep(1)
except OSError, err:
err = str(err)
if err.find("No such process") > 0:
os.remove(pidfile)
if 'stop' == action:
sys.exit(0)
action = 'start'
pid = None
else:
print str(err)
sys.exit(1)
if ('start' == action):
if (pid):
mess = "Старт отменен — pid файл '%s' существует.\n"
sys.stderr.write(mess % pidfile)
sys.exit(1)
# Запускаем процесс демонизации скрипта :
deamonize(stdout,stderr,stdin,pidfile,startmsg)
return
print "Синтакс запуска: %s start|stop|restart" % sys.argv[0]
sys.exit(2)
if (__name__ == "__main__"):
startstop(stdout = setting.daemon_log, pidfile = setting.daemon_pid)
test()