linux开机自启动方法

服务开机自启

docker 开机自启方法

容器没有自启 执行一下 docker update --restart=always NAMES[容器名称]

java 服务器开机自启方法

第一种:利用 rc.local 文件完成开机自启,vi /etc/rc.local

#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
# 这边键入需要启动的脚本
/home/xxx/xx/xx/auto-restart.sh 

auto-restart.sh 脚本模版

#!/bin/sh
#chkconfig: 2345 80 05
#description: 
#author: tonglh
#如果有mysql和redis 或者其他docker容器需要关联重启的可放一起。
#echo "等待Mysql和Redis启动..."
#sleep 90s
#echo "启动nginx"
#/usr/local/nginx/sbin/nginx
#sleep 2s
# java服务路径
cd /xx/xx/xxx-xx/xxx/xx 
echo "启动xxxx服务"
sh rsbc.sh stop
sleep 10s
sh rsbc.sh start
echo "xxxx startup"
tail -f /dev/null
exit $?

rsbc.sh 脚本模版可根据需要进行调整这边给出常见脚本例子

#!/bin/sh

# shell directory
SH_DIR=$(cd "$(dirname "$0")"; pwd)
cd ${SH_DIR}

ACTIVE=$2
ACTIVE=${ACTIVE:=prod}

#[ ! -e "${JAVA_HOME}/bin/java" ]
#export JAVA_HOME
#JAVA="/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.151-5.b12.el7_4.x86_64/bin/java"
JAVA=${JAVA_HOME}/bin/java

################# user options ###############
APP_ID="XXXXX-api"
JAR_FILE_NAME="api.jar"
COMMON_LOG_FILE="./startup.log"
USER_OPT="-Dfile.encoding=UTF-8"
################# user options ###############

################# java options ###############
JAVA_OPT="-server -Xmx1024m -Xms512m"
JAVA_OPT="${JAVA_OPT} -Xmn128m"
# jetty suggestion
JAVA_OPT="${JAVA_OPT} -verbose:gc"
#JAVA_OPT="${JAVA_OPT} -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+PrintGCDetails"
JAVA_OPT="${JAVA_OPT} -Xloggc:./logs/gc.log -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=20m"
JAVA_OPT="${JAVA_OPT} -XX:+PrintTenuringDistribution"
JAVA_OPT="${JAVA_OPT} -XX:+PrintCommandLineFlags"
JAVA_OPT="${JAVA_OPT} -XX:+DisableExplicitGC"
JAVA_OPT="${JAVA_OPT} -XX:+UseConcMarkSweepGC"
JAVA_OPT="${JAVA_OPT} -XX:CMSInitiatingOccupancyFraction=80"
# r&d suggestion
JAVA_OPT="${JAVA_OPT} -XX:HeapDumpPath=./logs/dumpfile.hprof"
JAVA_OPT="${JAVA_OPT} -XX:+HeapDumpOnOutOfMemoryError"
# other like rocketmq
JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow"
#JAVA_OPT="${JAVA_OPT} -XX:+AlwaysPreTouch"
JAVA_OPT="${JAVA_OPT} -XX:MaxDirectMemorySize=15g"
JAVA_OPT="${JAVA_OPT} -XX:-UseLargePages -XX:-UseBiasedLocking"
# r&d
################# java options ###############

################# pid  ###############
#ONE JAR MORE INSTANCE
PID=$(ps -ef|grep "\\-DappId=${APP_ID}" | grep -v grep|awk '{print $2}')
#ONE JAR ONE INSTANCE
#PID=$(fuser ${JAR_FILE_NAME} 2>/dev/null | xargs echo)
################# pid  ###############

start(){
#    if [ x"$1" = x ] ;then
#        echo "Failed to start '${APP_ID}', pls input db password encrypt key."
#        exit
#    fi
    if ! kill -0 ${PID} 2>/dev/null ;then
        echo "${APP_ID} is starting! File is ${JAR_FILE_NAME}"
        nohup ${JAVA} ${JAVA_OPT} ${USER_OPT} -DappId=${APP_ID} -jar ${JAR_FILE_NAME} --spring.profiles.active=${ACTIVE} > ${COMMON_LOG_FILE}  2>&1 &
    else
        echo "Failed to start '${APP_ID}',because it is running (pid:${PID})."
    fi 
    
}

stop(){
    if  kill -0 ${PID} 2>/dev/null ;then
        kill ${PID}
        echo "killing ${APP_ID}"
    else
        echo "Failed to stop '${APP_ID}',It is not running."
    fi
}

status(){
    if kill -0 ${PID} 2>/dev/null ;then
        echo "${APP_ID} (pid:${PID}) is running."
    else
        echo "${APP_ID} is not running."
    fi 
}

case $1 in 
    start)
        start;;
    stop)
        stop;;
    status)
        status;;
    *)
        echo "Usage: $0 start [ACTIVE]|stop|status"
esac

第二种:/etc/systemd/system/ 路径下面新建对应的xxx.service

# 开机自启
systemctl enable xxx.service

# 开启服务
systemctl start xxx.service

# 查询服务状态
systemctl status xxx.service

xxx.service例子

[Unit]

Description=videoFusionApi

After=network.target

[Service]

Environment="JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.181-7.b13.el7.x86_64"

Type=forking

ExecStart=/bin/sh /xx/xx/xx/xx/rsbc.sh start

ExecStop=/bin/sh /xxx/xx/xx/xx/rsbc.sh stop

PrivateTmp=true

[Install]

WantedBy=multi-user.target

以上两种方法看实际业务情况而定用那种。

你可能感兴趣的:(服务器,linux,运维)