linux 开机启动 服务的 三种方式

方式一  /etc/rc.d/rc.local

往 /etc/rc.d/rc.local 追加内容,

先配置java, 环境变量,再启动

以便tomcat开机启动

#cd /etc
#FILE_NAME=/etc/rc.d/rc.local
#ubuntu 44
FILE_NAME=/etc/rc.local
chmod 775 $FILE_NAME
echo -e "export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/" >> $FILE_NAME
echo -e "export PATH=$""PATH:$""JAVA_HOME/bin" >> $FILE_NAME
echo -e "/usr/email/start.sh" >> $FILE_NAME

脚本为

方式二,systemd

照着下面的格式创建一个脚本,

添加权限

chmod +x /usr/email/email.service

加软链接

sudo ln -s /usr/email/email.service /etc/systemd/system/multi-user.target.wants/email.service

把脚本,的软连接,加入这个目录后,就会开机自启动
linux 开机启动 服务的 三种方式_第1张图片

[Unit]
Description=email.service
After=network.target

[Service]
Type=simple
RemainAfterExit=yes
ExecStart=/usr/email/start.sh
ExecStop=/usr/email/stop.sh


[Install]
WantedBy=multi-user.target

方式三 /etc/init.d

在/etc/init.d目录加入脚本

linux 开机启动 服务的 三种方式_第2张图片

全部代码

#!/bin/bash

## Fill in name of program here.
PROG="emailSevice"
#PROG_PATH="/usr/local/emailSevice" ## Not need, but sometimes helpful (if $PROG resides in /opt for example).
#PROG_ARGS="" 
#PID_PATH="/var/run/"


start() {
    echo "begin start"
    sh /usr/email/start.sh
    exit 0
}

stop() {
    echo "begin stop"
    sh /usr/email/stop.sh
    exit 0
}

## Check to see if we are running as root first.
## Found at http://www.cyberciti.biz/tips/shell-root-user-check-script.html
if [ "$(id -u)" != "0" ]; then
    echo "This script must be run as root" 1>&2
    exit 1
fi

case "$1" in
    start)
        start
        exit 0
    ;;
    stop)
        stop
        exit 0
    ;;
    reload|restart|force-reload)
        stop
        start
        exit 0
    ;;
    **)
        echo "Usage: $0 {start|stop|reload}" 1>&2
        exit 1
    ;;
esac

 

你可能感兴趣的:(linux 开机启动 服务的 三种方式)