1.可以直接放在opt目录 java -jar app.jar & 启动。(&代表后台启动)
2.
nohup java -jar app.jar > app.log 2>&1 &
- app.log将标准输出指向制定文件app.log
- nohup java -jar起完之后只可以存在一天,nohup可以一直运行在后台
- 2>&1 输出所有的日志文件
- & 后台启动数字 含义
0 标准输入(一般是键盘)
1 标准输出(一般是显示屏,准确的说是用户终端控制台)
2 标准错误(出错信息输出)
3.用shell脚本启动(app.sh)
#!/bin/sh
pid=` ps -ef | grep app-1.0.jar | grep -v grep | awk '{print$2}'`
filename=all$(date +%Y-%m-%d)
echo $pid
if [[ $pid != '' ]];then
kill -9 $pid
echo " 重新启动app"
nohup /usr/java/jdk1.8.0_191/bin/java -jar /opt/app-1.0.jar >/dev/null 2>&1 &
else
echo "app 之前没启动,现在已经启动"
nohup /usr/java/jdk1.8.0_191/bin/java -jar /opt/app-1.0.jar >/dev/null 2>&1 &
fi
echo "日志文件名称"$filename
tail -f /opt/logs/app/$filename.txt
3.2:比较全(app.sh)
#!/bin/sh
#功能简介:启动app.jar 文件
#注意:在sh文件中=赋值,左右两侧不能有空格
APP=app
APP_NAME=${APP}".jar"
log_dir=/home/jar_logs/
log_file=/home/jar_logs/app.log
command=$1
# 启动
function start(){
# 日志文件夹不存在,则创建
if [ ! -d "${log_dir}" ];then
mkdir "${log_dir}"
fi
rm -f tpid
nohup java -jar ${APP_NAME} 1>/dev/null 2>"${log_file}" &
echo $! > tpid
check
}
# 停止
function stop(){
tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
echo 'stop process...'
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 Process!'
kill -9 $tpid
else
echo 'Stop Success!'
fi
}
# 检查
function check(){
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进程
function forcekill(){
tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
echo 'Kill Process!'
kill -9 $tpid
fi
}
# 输出进程号
function showtpid(){
tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
echo 'process '$APP_NAME' tpid is '$tpid
else
echo 'process '$APP_NAME' is not running.'
fi
}
if [ "${command}" == "start" ]; then
start
elif [ "${command}" == "stop" ]; then
stop
elif [ "${command}" == "check" ]; then
check
elif [ "${command}" == "kill" ]; then
forcekill
elif [ "${command}" == "tpid" ];then
showtpid
else
echo "Unknow argument...."
fi
编写完之后切记要赋予管理员权限,作为可执行脚本
chmod +x app.sh
之后用 ./app.sh 启动