常用的开机自启动

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhongwenit/article/details/82314163

1.tomcat开机自启动
第一步:编辑启动文件,修改tomcat的bin路径,修改启动名称、关闭名称和文件名称为自己的站点名称,本文为site1,修改完然后放入/etc/rc.d/init.d文件目录

#!/bin/bash
# chkconfig: 2345 80 20
# Description: Tomcat Server basic start/shutdown script
# /etc/init.d/tomcat6 -- startup script for the Tomcat 6 servlet engine

TOMCAT_HOME=/usr/local/tomcat-site1/bin
START_TOMCAT=/usr/local/tomcat-site1/bin/startup.sh
STOP_TOMCAT=/usr/local/tomcat-site1/bin/shutdown.sh

start() {
 echo -n "Starting site1: "
 cd $TOMCAT_HOME
 ${START_TOMCAT}
 echo "done."
}

stop() {
 echo -n "Shutting down site1: "
 cd $TOMCAT_HOME
 ${STOP_TOMCAT}
 echo "done."
}

case "$1" in

start)
 start
 ;;

stop)
 stop
 ;;

restart)
 stop
 sleep 10
 start
 ;;

*)
 echo "Usage: $0 {start|stop|restart}"

esac
exit 0
[root@solution01-test1 ~]# cd /etc/init.d
[root@solution01-test1 init.d]# ls
functions  jexec  netconsole  network  site1  README
//第二步:设置权限
[root@solution01-test1 init.d]# chmod 755 site1 
//第三步:添加到启动列表
[root@solution01-test1 init.d]# chkconfig --add site1 
//检查是否已经添加
[root@solution01-test1 init.d]# chkconfig --list site1 

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

site1       0:off   1:off   2:on    3:on    4:on    5:on    6:off
//启动站点
[root@solution01-test1 init.d]# service site1 start
//关闭站点
[root@solution01-test1 init.d]# service site1 stop

参考:Linux Centos Automatically Start Apache Tomcat server on boot


2.nginx开机自启动
第一步:编辑nginx.service脚本文件放入/etc/systemd/system文件目录

[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=True

[Install]
WantedBy=multi-user.target

//关闭
[root@solution01-test1 ~]# service nginx stop
Redirecting to /bin/systemctl stop  nginx.service
//启动
[root@solution01-test1 ~]# service nginx start
Redirecting to /bin/systemctl start  nginx.service
//第二步:设置开机启动
[root@solution01-test1 ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /etc/systemd/system/nginx.service.

3.jar包centos开机自启动
第一步:编辑启动、停止、重启脚本文件site1.sh,放入jar文件目录

#!/bin/bash
MD=$1

start() {
        nohup /usr/java/jdk1.8.0_73/bin/java -jar /usr/local/site1.jar &> /usr/local/site1.log &
}
stop() {

        port=$(sudo netstat -tnlp | grep ':9000' |awk '{print $nf}' |awk -f'/' '{print $1}')
        sudo kill $port
}

restart() {

        port=$(sudo netstat -tnlp | grep ':9000' |awk '{print $nf}' |awk -f'/' '{print $1}')
        sudo kill $port
        nohup /usr/java/jdk1.8.0_73/bin/java -jar /usr/local/site1.jar &> /usr/local/site1.log &
}
# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|version}"
        ;;
esac
exit 0

第二步:编辑自启动脚本site1.service,放入/etc/systemd/system/文件目录

[UNIT]
Description=The site1 Service
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/site1.sh start
ExecReload=/usr/local/site1.sh restart
ExecStop=/usr/local/site1.sh stop
PrivateTmp=True

[Install]
WantedBy=multi-user.target

第三步:设置开机启动

[root@bogon system]# systemctl enable site1
Created symlink from /etc/systemd/system/multi-user.target.wants/site1.service to /etc/systemd/system/site1.service.

//启动
#service site1 start
//停止
#service site1 stop

猜你喜欢

转载自blog.csdn.net/zhongwenit/article/details/82314163
今日推荐