데몬으로 python script 를 실행할때 직접 daemonizing 코드를 구형하여 만들었는데

python-daemon 이라는 패키지를 사용해보니 cmdline 에서 start/stop/restart 도 지원하고 filelock 도 지원한다.

쓸만해보여서 정리해둠 


wget https://www.python.org/ftp/python/2.7.12/Python-2.7.12.tgz
tar zxf Python-2.7.12.tgz
cd Python-2.7.12
./configure --prefix=/home/apps/python2.7 --enable-unicode=ucs4 --enable-shared
make
make install
wget --no-check-certificate https://pypi.python.org/packages/25/4e/1b16cfe90856235a13872a6641278c862e4143887d11a12ac4905081197f/setuptools-28.8.0.tar.gz
tar zxf setuptools-28.8.0.tar.gz
cd setuptools-28.8.0
python2.7 setup.py build
python2.7 setup.py install
wget --no-check-certificate https://bootstrap.pypa.io/get-pip.py
python2.7 get-pip.py
pip install python-daemon
 
mkdir /daemon_sample
cd /daemon_sample
mkdir  bin lib log run
cd bin
vi daemon_sample.py
--------------------------------------------------------------------------------------------
#!/home/apps/python2.7/bin/python
# -*- coding: UTF-8 -*-
import time
from daemon.runner import DaemonRunner
class TestDaemon(object):
    def __init__(self):
        self.stdin_path = '/dev/null'
        self.stdout_path = '/dev/tty'
        self.stderr_path = '/dev/tty'
        self.pidfile_timeout = 0
        self.pidfile_path="/usr/mgmt/apm_package_manager/run/TestDaemon.pid"
    def run(self):
        while 1:
            time.sleep(3)
DaemonRunner(TestDaemon()).do_action()
--------------------------------------------------------------------------------------------
chmod 700 daemon_sample.py
./daemon_sample.py
usage: test.py start|stop|restart
 
 
./daemon_sample.py start
started with pid 24751
 
 
./daemon_sample.py start
  File "./test.py", line 19, in <module>
    DaemonRunner(TestDaemon()).do_action()
  File "/home/apps/python2.7/lib/python2.7/site-packages/daemon/runner.py", line 274, in do_action
    func(self)
  File "/home/apps/python2.7/lib/python2.7/site-packages/daemon/runner.py", line 187, in _start
    raise error
daemon.runner.DaemonRunnerStartFailureError: PID file '/usr/mgmt/apm_package_manager/run/TestDaemon.pid' already locked


'Python' 카테고리의 다른 글

Pika Python AMQP Client Library  (0) 2017.01.31
s3 example  (0) 2016.12.28
Threading  (0) 2016.12.22
pidlockfile.py for windows  (0) 2016.12.19
cygwin + ssh + rsync  (0) 2016.12.10

+ Recent posts