zoukankan      html  css  js  c++  java
  • bash 启动/关闭程序

      1 #!/bin/bash
      2 #防止程序未启动完成 预留时间
      3 sleep 0
      4 
      5 #定义入参
      6 
      7 ip=$(who -m|awk '{print $5}')
      8 user=$(who -m|awk '{print $1}')
      9 #定义日期
     10 date=`date  "+%Y%m%d"`
     11 
     12 #定义日志函数
     13 function LOG() {
     14   echo $user $ip [`date "+%Y-%m-%d %H:%M:%S"`] '=>' $* | tee -a ./Maintain.$date
     15 }
     16 
     17 
     18 
     19 #定义端口参数转换函数
     20 function exchange_port(){
     21 
     22     #目标字符串转为端口号
     23     case $1 in
     24         'svn') port=23690
     25         ;;
     26         'ruby') port=29636
     27         ;;
     28         *) port=1
     29         
     30         ;;
     31     esac
     32     #返回端口号
     33     echo $port
     34 }
     35 
     36 #定义检查程序是否正常启动
     37 function check_status(){
     38     #根据端口查询,通过返回是否有字符串进行判断
     39     port=$(exchange_port $1)
     40     flag=$(netstat -tunpl|grep $port|awk '{print $1}')
     41     #休眠3s
     42     sleep 3
     43     #字符串不为空
     44     if [[ ${#flag} != 0 ]] && [[ "${port}" != "1" ]];
     45     then
     46         #status=true
     47         LOG $1 "正在运行"
     48     #字符串为空
     49     else [[ "${port}" != "1" ]];
     50         #status=false    
     51         LOG  $1 "未运行"
     52     fi
     53     #echo $status
     54 }
     55 
     56 #定义启动命令参数转换函数
     57 function start_program(){
     58 
     59     case $1 in
     60         #判断
     61         'svn') 
     62         #执行命令
     63         svnserve -d -r /opt/svn --listen-port 23690
     64         ;;
     65         #判断
     66         'ruby')
     67         #切换执行路径 
     68         cd /opt/redmine
     69         #后台运行
     70         nohup bundle exec rails server webrick -e production -p 29636 >> /opt/logs/redmine/log.$date 2>&1 &
     71 
     72         ;;
     73         *) cmd[0]='echo Null'
     74     esac      
     75      
     76     check_status $1
     77 
     78 }
     79 
     80 
     81 #定义关闭程序函数
     82 function stop_program() {
     83   
     84   port=$(exchange_port $1)
     85   #获取程序运行pid
     86   pid=$(ps -ef|grep $1|grep $port|awk '{print $2}')
     87   if [[ "$pid" != "" ]]
     88   then
     89       kill -9 $pid
     90       LOG $target_str "已关闭"
     91   else
     92       LOG $target_str "未启动"
     93   fi
     94 }
     95 
     96 
     97 
     98 function main(){
     99     #主控制程序
    100     port=$(exchange_port $1)
    101 
    102     if [[ "${port}" = "1" ]];
    103     then
    104         LOG "未知程序"
    105     else
    106         case $2 in
    107             start) start_program $1
    108             ;;
    109             stop) stop_program $1
    110             ;;
    111             status) check_status $1
    112             ;;
    113             *) LOG "命令错误!请使用:start stop status"
    114         esac
    115     fi
    116 }
    117 
    118 main $1 $2
  • 相关阅读:
    关于明确我校教学作息时间的通知
    放假时间
    五一上课表
    课表
    mysql 数据库备份与还原
    默默的追随
    2004上半年
    IE慢吗?
    创建的permission java.security.AllPermission;文件,用于加密
    数据库驱动
  • 原文地址:https://www.cnblogs.com/aongao/p/14346344.html
Copyright © 2011-2022 走看看