【小笔记】程序的nohup.out去哪里了?

文章目录

    • nohup.out去哪里了?
    • stackoverflow 的解答
    • startup.sh 模板

nohup.out去哪里了?

相关同事有一个startup.sh脚本的nohup相关模块如下:

nohup run-exe  2>&1 &  echo $! > $PID_FILE

直接命令行界面执行 startup.sh,会出现nohup.out文件,我们可以读取程序的输出(std,stderr).但是我们使用crontab拉起startup.sh的时候,怎么也找不到nohup.out.

stackoverflow 的解答

  • 《cron job doesn’t output to nohup.out》
  • https://stackoverflow.com/questions/14145250/cron-job-doesnt-output-to-nohup-out

Why are you using nohup? nohup is a command that tells the running terminal to ignore the hangup signal. cron, however, has no hangup signal, because it is not linked to a terminal session.

简单翻译:

你为什么用nohup?NOHUP是命令运行终端忽略挂起信号的命令。然而,cron没有挂断信号,因为它没有链接到终端会话。

nohup $JAVA ${JAVA_OPT} >> ${BASE_DIR}/logs/start.out 2>&1 &

如果我们的nohup模块参照如上编写,既能满足cron场景,又能满足平时终端启停的场景

startup.sh 模板

现网中,我们需要写大量的startup.sh 来满足程序启停需求,这里我参照nacos的脚本,编写了一个java程序的参考样例:
https://github.com/pierre94/kafka-manager/tree/pierre94-add-assembly/web/bin

#!/usr/bin/env bash

error_exit ()
{
     
    echo "ERROR: $1 !!"
    exit 1
}

if [ -z "$JAVA_HOME" ]; then
    error_exit "Please set the JAVA_HOME variable in your environment, We need java(x64)! jdk8 or later is better!"

fi

export WEB_SERVER="kafka-manager-web-*"
export JAVA_HOME
export JAVA="$JAVA_HOME/bin/java"
export BASE_DIR=`cd $(dirname $0)/..; pwd`
export DEFAULT_SEARCH_LOCATIONS="classpath:/,classpath:/config/,file:./,file:./config/"
export CUSTOM_SEARCH_LOCATIONS=${DEFAULT_SEARCH_LOCATIONS},file:${BASE_DIR}/conf/

#===========================================================================================
# JVM Configuration
#===========================================================================================

JAVA_OPT="${JAVA_OPT} -server -Xms2g -Xmx2g -Xmn1g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${BASE_DIR}/logs/java_heapdump.hprof"
JAVA_OPT="${JAVA_OPT} -Xloggc:${BASE_DIR}/logs/gc.log -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M"
JAVA_OPT="${JAVA_OPT} -jar ${BASE_DIR}/lib/${WEB_SERVER}.jar"
JAVA_OPT="${JAVA_OPT} --spring.config.location=${CUSTOM_SEARCH_LOCATIONS}"
JAVA_OPT="${JAVA_OPT} --logging.config=${BASE_DIR}/conf/logback-spring.xml"
JAVA_OPT="${JAVA_OPT} --server.max-http-header-size=524288"

if [ ! -d "${BASE_DIR}/logs" ]; then
  mkdir ${BASE_DIR}/logs
fi

echo "$JAVA ${JAVA_OPT}"

# check the start.out log output file
if [ ! -f "${BASE_DIR}/logs/start.out" ]; then
  touch "${BASE_DIR}/logs/start.out"
fi
# start
echo "$JAVA ${JAVA_OPT}" > ${BASE_DIR}/logs/start.out 2>&1 &
nohup $JAVA ${JAVA_OPT} >> ${BASE_DIR}/logs/start.out 2>&1 &
echo "kafka-manager is starting,you can check the ${BASE_DIR}/logs/start.out"

你可能感兴趣的:(java,linux,bash,nohup,nohup.out)