1.shell脚本

#!/bin/bash
# description: selenium service
#端口号,根据此端口号确定PID
PORT=9000
#jar包所在目录
HOME='/usr/local/selenium'
#查询出监听了port端口的TCP协议的程序
pid=`netstat -anp|grep $PORT|awk '{printf $7}'|cut -d/ -f1`
start(){
 echo "INFO: Starting selenium ..."
   if [ -n "$pid" ]; then
      echo "server already start,pid:$pid"
      return 0
   fi
   #进入命令所在目录
   cd $HOME
   #启动selenium服务
   java -jar $HOME/selenium-sever-standalone-2.53.1.jar -role hub -port 9000 >selenium.log 2>&1 &
   echo "start at port:$PORT"
}
stop(){
   if [ -z "$pid" ]; then
      echo "not find program on port:$PORT"
      return 0
   fi
   #结束程序
   kill -9 $pid
   rm -rf $pid
   while true
   do
        process=`netstat -anp|grep $PORT|awk '{printf $7}'|cut -d/ -f1`;
echo "process id is"+$process;
        if [ "$process"!="" ]; then
                sleep 1;
                echo "stopping ...";
        else
                echo "service selenium stop";
                break;
        fi
    done
}
status(){
   if [ -z "$pid" ]; then
      echo "not find program on port:$PORT"
   else
      echo "program id running,pid:$pid"
   fi
}
case $1 in
   start)
      start
   ;;
   stop)
      stop
   ;;
   restart)
      $0 stop
      sleep
      $0 start
   ;;
   status)
      status
   ;;
   *)
      echo "Usage: {start|stop|status}"
   ;;
esac
exit 0

2.在/etc/init.d文件夹下新建文件 cat>>filename

然后将上述shell脚本拷进去,然后执行service filename start/stop/status/restart

3.问题

直接从windows将脚本考到linux环境会运行不成功

因为windows与linux环境下编码不一致导致的

解决方法问度娘

实在不行就手动输一遍

03-06 19:06