看到比较好的linux脚本:
startup.sh 启动脚本
#!/bin/sh
rm -f tpid
cd /home/xxx 这个是进入启动项目所在的文件路径
nohup java -jar xxx.jar &
echo $! > tpid
echo Start xxx Success!
shutdown.sh 停止脚本
#!/bin/sh
APP_NAME=xxx 这是对应启动脚本里面的 项目名称 保持一致
tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
echo 'Stop xxx.'
kill -15 $tpid
fi
sleep 5
tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
echo 'Kill xxx!'
kill -9 $tpid
else
echo 'Stop xxx Success!'
fi
---------------------------------------------华丽的分割线--------------------------------------------------------------------------------------------------------
下面一般都不用
check.sh
#!/bin/sh
APP_NAME=myapp
tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
echo 'App is running.'
else
echo 'App is NOT running.'
fi
kill.sh
#!/bin/sh
APP_NAME=xxx
tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
echo 'Kill xxx!'
kill -9 $tpid
fi