1、窗口被锁定,可按CTRL + C打断程序运行,关闭窗口程序停止运行
java -jar XXX.jar
2、窗口不被锁定,关闭窗口时,程序停止运行
java -jar XXX.jar &
3、nohup 意思是不挂断运行,当账户退出或终端关闭时,程序仍然运行,日志内容默认存入nohup.out 文件内
nohup java -jar XXX.jar &
4、日志内容默认存入temp.txt文件内
nohup java -jar XXX.jar >temp.txt &
5、/dev/null 2>&1 2代表标准错误,1代表标准输出。把日志输出到黑洞(就是这里不打印日志)
nohup java -jar XXX.jar >/dev/null 2>&1 &
//日志输出到file,这里file会被打开2次command > file 2>file//日志输出到file,这里file会被打开1次command > file 2>&1
6、完整日志输出到log文件内
nohup java -jar XXX.jar >/data/log 2>&1 &
7.例子
#!/bin/bashAPP_NAME=server-1.6.0-1.6.0.jarserverid=$2cd /data/misc_apps/hero-provider-$serverid/#使用说明,用来提示输入参数usage() {echo "Usage: sh robotcenter.sh [start|stop|restart|status]"exit 1}#检查程序是否在运行is_exist(){pid=`ps -ef|grep /data/misc_apps/hero-provider-$serverid/|grep -v grep|grep -v PPID|awk '{ print $2}'`# pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}'`#如果不存在返回1,存在返回0if [ -z "${pid}" ]; thenreturn 1elsereturn 0fi}#启动方法start(){is_existif [ $? -eq 0 ]; thenecho "${APP_NAME} is already running. pid=${pid}"elsecp -fr /root/${APP_NAME} /data/misc_apps/hero-provider-$serverid/nohup java -jar "/data/misc_apps/hero-provider-$serverid/$APP_NAME" > /data/misc_apps/hero-provider-$serverid/logs/server.log 2>&1 &fi}#停止方法stop(){is_existif [ $? -eq "0" ]; thenline=12port=99999for ID in $(cat -A /data/nfs/afk-conf/ServerConfigStruct.txt)doNUM=$lineS_ID=$(awk 'NR=='$NUM' {print $1}' /data/nfs/afk-conf/ServerConfigStruct.txt)S_ID=${S_ID%.*}T_NUM=$(awk 'NR=='$NUM' {print $6}' /data/nfs/afk-conf/ServerConfigStruct.txt)T_NUM=${T_NUM%.*}line=`expr 1 + $line`if [ "$S_ID"X != "$serverid"X ]thencontinueelseecho "find port"port=$T_NUMbreakfidoneecho $portcurl --data "cmd=stop&zhangnian=nidaye@skyrim" http://localhost:$port# curl http://localhost:5400/summitTest?cmd=stopsleep 5skill -15 $pidelseecho "${APP_NAME} is not running"fi }#输出运行状态status(){is_existif [ $? -eq "0" ]; thenecho "${APP_NAME} is running. Pid is ${pid}"elseecho "${APP_NAME} is NOT running."fi}#重启restart(){stopsleep 5start}#根据输入参数,选择执行对应方法,不输入则执行使用说明case "$1" in"start")start;;"stop")stop;;"status")status;;"restart")restart;;*)usage;;esac
如果觉得《java 启动方式 java -jar xx.jar》对你有帮助,请点赞、收藏,并留下你的观点哦!