• Python3将web服务和脚本做成开机自启


    1.将bwService文件放到 /etc/init.d/下

    bwService文件(类型是文件)

    #!/bin/bash
    #
    # This shell script takes care of starting and stopping
    #  the Bw system.
    #
    # chkconfig: - 99 01
    # description: Bw Service.
    # processname: BwService
    # pidfile: /var/run/BwService.pid
    # version: V3.0.0.0
    
    myname=`whoami`
    pid_file=bwService.pid
    #. /lib/lsb/init-functions
    python_prog=python3
    svc_arg=/home/bw/repo/switch_ssh.py
    web_arg="/opt/zabbix3d/manage.py runserver 0.0.0.0:8000"
    makeAbsolute() {
        case $1 in
            /*)
                # already absolute, return it
                echo "$1"
                ;;
            *)
                # relative, prepend $2 made absolute
                echo `makeAbsolute "$2" "$PWD"`/"$1" | sed 's,/.$,,'
                ;;
        esac
    }
    
    getlibdir(){
        for element in `ls $1`
        do  
            dir_or_file=$1"/"$element
            if [ -d $dir_or_file ]
            then 
                libdir="$libdir:$dir_or_file"
                getlibdir $dir_or_file
            fi  
        done
    }
    
    
    BOOTUP=color
    RES_COL=60
    MOVE_TO_COL="echo -en \033[${RES_COL}G"
    SETCOLOR_SUCCESS="echo -en \033[1;32m"
    SETCOLOR_FAILURE="echo -en \033[1;31m"
    SETCOLOR_NORMAL="echo -en \033[0;39m"
    
    # define some functions to be used by most or all shell scripts
    # -------------------------------functions start------------------------------
    
    echo_success() {
      txt="OK"
      if [ ! "$1" = "" ]; then
        txt="$1"
      fi
      [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
      echo -n "[  "
      [ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS
      echo -n $"$txt"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      echo -n "  ]"
      echo -e "
    "
      return 0
    }
    
    
    echo_failure() {
      txt="FAILED"
      if [ ! "$1" = "" ]; then
        txt="$1"
      fi
      [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
      echo -n "["
      [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
      echo -n $"$txt"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      echo -n "]"
      echo -e "
    "
      return 1
    }
    
    #test -x $PROGRAM || exit 0
    
    # Check if $pid (could be plural) are running
    checkpid() {
     local i
    
     for i in $* ; do
      [ -d "/proc/$i" ] && return 0
     done
     return 1
    }
    
    
    #get the pid
    getpid() {
    ps -ef|grep -v grep| grep -v '/usr/bin' |grep "$python_prog $1" | awk '{print $2}'
    #cmd="ps -ef|grep -v grep |grep ""
    #for arg in $*; do
    #cmd=${cmd}" "${arg} 
    #done
    #cmd="${cmd}" | awk '{print $2}'"
    #echo $cmd
    #pid=`$cmd`
    #echo $$
    #pid=$(ps -ef|grep $1|grep "$2"|awk '{printf $2}')
    #echo $pid
       #if [ -n $2 ]; then
    
           #ps -ef|grep "$1" |grep "$2"| awk '{print $2}'
       #else
        #ps -ef|grep "$1" | awk '{print $2}'
       #fi
    
    }
    
    #get the port
    getport()
    {
      grep "sPORT_NUM" "$path"  | grep -Eo "[0-9]+"
    }
    #get root user
    get_root_user()
    {
        if [ "x" = "x$ners_user" ] || [ "$ners_user" = "root" ]; then
            ners_user=`ls -ld "$ners_home/." 2>/dev/null | awk 'NR==1{print $3}'`
            export ners_user
        fi
    }
    
    #get the pid username
    getpiduser()
    {
        ps -eo user,pid,args | grep -F "$path" | grep "$prog_path/$1" | awk '$3 !~/^[su|-]/{print $1}'
    }
    
    
    # A function to stop a program.
    killproc() {
     RC=0
     # Test syntax.
     if [ "$#" -eq 0 ]; then
       echo $"Usage: killproc {program} [signal]"
       return 1
     fi
    
     notset=0
     # check for second arg to be kill level
     if [ -n "$2" ]; then
       killlevel=$2
     else
       notset=1
       killlevel=""
     fi
    
     pid=`getpid $1`
     echo $pid
     # Kill it.
     if [ -n "${pid:-}" ] ; then
       [ "$BOOTUP" = "verbose" -a -z "$LSB" ] && echo -n "$2 "
       if [ "$notset" -eq "1" ] ; then
           # TERM first, then KILL if not dead
           kill -TERM $pid
           while checkpid $pid; do
             sleep 1
         done
         checkpid $pid
         RC=$?
         [ "$RC" -eq 0 ]
         RC=`expr 1 - $RC `
       # use specified level only
       else
         if checkpid $pid; then
           kill -9 $pid
        while checkpid $pid; do
             sleep 1
         done
           RC=$?
           [ "$RC" -eq 0 ]
         fi
       fi
     else
       echo $"$1 shutdown"
       RC=1
     fi
    
     # Remove pid file if any.
     if [ "$notset" = "1" ]; then
       rm -f /var/run/$pid_file
     fi
     
     return $RC
    }
    
    start(){
      pid=`getpid $2`
      if [ -n "$pid" ]
      then
        echo "$1 服务已经在运行."
        return 1
      fi
      echo -n $"启动$1服务: "
      dirPath=`dirname $2`
      nohup $python_prog $2 $3 $4 >> "$dirPath/output.log" 2>&1 &
    
      ret=$?  
      if [ $ret -eq 0 ]; then
        sleep 5
        pid=`getpid $2`
        if [ -n "$pid" ]; then
          echo_success
        else
          echo_failure
          cat "$dirPath/output.log"
          return 1
        fi
      else
        echo_failure
        cat "$dirPath/output.log"
        return 1
      fi
    
     if [ $ret -eq 0 ] && [ -w "/var/run/" ]
      then
        touch /var/run/$pid_file
        pid=`getpid $2`
        echo $pid > /var/run/$pid_file
      fi
     return $ret
    }
    
    
    stop(){
    
     pid=`getpid $2`
    
     dirPath=`dirname $2`
     if [ -n "$pid" ]; then
       echo -n $"停止$1服务: "
       killproc $2 > "$dirPath/output.log" 2>&1
       ret=$?
       if [ $ret -eq 0 ]; then      
         echo_success
       else
         echo_failure
         cat "$dirPath/output.log"
       fi
    
    if [ $ret -eq 0 ] && [ -w "/var/run/$pid_file" ]
       then
         rm -f /var/run/$pid_file
       fi
     else
       echo -n "停止$1服务"
        echo_success
       return 0
     fi
     return $ret
    }
    startSvc(){
        start "交换机" $svc_arg 
        start "网站" $web_arg 
    }
    
    stopSvc(){
        stop "交换机" $svc_arg 
        stop "网站" $web_arg 
    }
    
    restart(){
       stopSvc
       startSvc
    }
    restartWeb(){
       stopSvc
       startSvc
    }
    list(){
      pid=`getpid $2`
      if [ -n "$pid" ]
        then
            echo -n "$1服务"
            echo_success "正在运行"
        else
            echo -n "$1服务"
            echo_failure "停止运行"
      fi
     return $ret
    }
    
    case "$1" in
      start)
        start "交换机" $svc_arg 
        start "网站" $web_arg 
         ;;
      stop)
        stop "交换机" $svc_arg 
        stop "网站" $web_arg 
         ;;
      startswitch)
        start "交换机" $svc_arg 
         ;;
      stopswitch)
        stop "交换机" $svc_arg 
         ;;
      startweb)
        start "网站" $web_arg 
         ;;
      stopweb)
        stop "网站" $web_arg 
         ;;
      restart)
         restart
         ;;
      status)
         list "交换机" $svc_arg 
         list "网站" $web_arg
         ;;
      *)
         exit 1
    esac
    exit 0

    相关命令

    [root@localhost ~]# cd /etc/init.d/
    [root@localhost init.d]# rz
    [root@localhost init.d]# ls
    bwService functions netconsole network README

    2.bwService赋予执行权限

    [root@localhost init.d]# chmod +x bwService 

    3.添加bwService到centos服务里

    [root@localhost init.d]# chkconfig --add bwService


    4.创建相关软连接

    [root@localhost init.d]# ln -s /etc/init.d/bwService /etc/rc0.d/K03bwService
    [root@localhost init.d]# ln -s /etc/init.d/bwService /etc/rc1.d/K03bwService
    [root@localhost init.d]# ln -s /etc/init.d/bwService /etc/rc2.d/S98bwService
    [root@localhost init.d]# ln -s /etc/init.d/bwService /etc/rc3.d/S98bwService
    [root@localhost init.d]# ln -s /etc/init.d/bwService /etc/rc4.d/S98bwService
    [root@localhost init.d]# ln -s /etc/init.d/bwService /etc/rc5.d/S98bwService

    5.查看服务状态,开启服务

    [root@localhost init.d]# service bwService status
    交换机服务 [停止运行]
    网站服务 [停止运行]
    [root@localhost init.d]# service bwService start
    启动交换机服务: [ OK ]
    启动网站服务: [ OK ]
    [root@localhost init.d]#
  • 相关阅读:
    null in ABAP and nullpointer in Java
    SAP ABAP SM50事务码和Hybris Commerce的线程管理器
    Hybris service layer和SAP CRM WebClient UI架构的横向比较
    SAP ABAP和Linux系统里如何检查网络传输的数据量
    SAP CRM WebClient UI和Hybris的controller是如何被调用的
    SAP CRM和Cloud for Customer订单中的业务伙伴的自动决定机制
    SAP CRM WebClient UI和Hybris CommerceUI tag的渲染逻辑
    SAP BSP和JSP页面里UI元素的ID生成逻辑
    微信jsapi支付
    微信jsapi退款操作
  • 原文地址:https://www.cnblogs.com/daofaziran/p/14119342.html
Copyright © 2020-2023  润新知