在服务目录中,vim server.sh
新建shell文件。
然后编写如下内容:
#!/bin/bash
app='./www/webapps/CQ_syy/syy.jar'
args='-Xms512m -Xmx512m'
cmd=$1
pid=`ps -ef |grep $app | grep -v 'grep'| awk '{print $2}'`
startup(){
nohup java -jar syy.jar > ./log/syy.log 2>&1 &
tail -500f ./log/syy.log
}
if [ ! $cmd ]; then
echo "Please specify args 'start|restart|stop'"
exit
fi
if [ $cmd == 'start' ]; then
if [ ! $pid ]; then
startup
else
echo "$app is running! pid=$pid"
fi
fi
if [ $cmd == 'restart' ]; then
if [ $pid ]
then
echo "$pid will be killed after 2 seconds!"
sleep 2
kill -9 $pid
fi
startup
fi
if [ $cmd == 'stop' ]; then
if [ $pid ]; then
echo "$pid will be killed after 2 seconds!"
sleep 2
kill -9 $pid
fi
echo "$app is stopped"
fi
使用说明:
- 执行
./server.sh start
或者 ./server.sh restart
即可启动项目 - 执行
./server.sh stop
停止项目运行
启动报错问题解决:
- 错误为:
/bin/bash^M: 坏的解释器: 没有那个文件或目录
。
出现原因:Windows 下编写的 shell 脚本,里面的换行符,在 Linux 下表达的含义不一致,多了些 Linux 无法解析的字符。
如何查看:cat -A 文件名
,例如:cat -A server.sh
。
如何解决多余的字符:sed -i 's/\r$//' 文件名
,例如:sed -i 's/\r$//' server.sh