Python

python 스크립트 데몬으로 실행하기

behonestar 2016. 5. 9. 16:23

1. 데몬 스크립트 작성

# sudo su

# cd /etc/init.d

# touch tunnel

# chmod 755 ./tunnel

# vi ./tunnel


nohup 사용

- 세션이 종료되어도 백그라운드로 프로세스 동작

- 로그를 파일로 출력

#!/bin/bash

# chkconfig: 345 88 08

# description: Tunneling Server Deamon


VENV=/home/ec2-user/venv

WORKDIR=/home/ec2-user/tunnel

DAEMON=tunnel.py

LOG=/var/log/tunnel.log


function do_start()

{

        source ${VENV}/bin/activate

        cd ${WORKDIR}

        nohup python ${DAEMON} & >> ${LOG}

}


function do_stop()

{

        PID=`ps -ef | grep ${DAEMON} | grep -v grep | awk '{print $2}'`

        if [ "$PID" != "" ]; then

                kill -9 $PID

        fi

}


case "$1" in

    start|stop)

        do_${1}

        ;;

    reload|restart)

        do_stop

        do_start

        ;;

    *)

        echo "Usage: /etc/init.d/tunnel {start|stop|restart}"

        exit 1

        ;;

esac



2. 부팅시 자동실행 등록

# chkconfig --add tunnel

# chkconfig --list | grep tunnel


'Python' 카테고리의 다른 글

[Python] sqlalchemy "Mysql Gone Away" 오류  (0) 2017.05.22
AWS Cognito Python Sample Code  (0) 2017.01.11
[python] 트래픽 초과 시 메일 발송하기  (0) 2016.12.01
Redis를 DB Table처럼 활용하기  (0) 2016.05.04
윈도우 Python 설치  (0) 2015.12.14