Centos开机自启的几种方式

目前接触到有三种开机自启的方式,以下以nginx为例进行演示

Init.d服务

cd /etc/rc.d/init.d
vi nginx

#!/bin/sh
#
#chkconfig: 2345 80 90
#description: nginx

start() {
    /usr/local/nginx/sbin/nginx
}

reload() {
    /usr/local/nginx/sbin/nginx -s reload
}

stop() {
    /usr/local/nginx/sbin/nginx -s quit
}

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

chmod +x nginx
service nginx start
service nginx stop
service nginx restart
service nginx reload
chkconfig --add nginx
chkconfig --list

Systemctl服务

cd /usr/lib/systemd/system/
vi nginx.service

[Unit]
Description=nginx
After=syslog.target network.target
Wants=network.target
[Service]
Type=simple
ExecStart=/usr/local/nginx/sbin/nginx
Restart=always
RestartSec=1min
[Install]
WantedBy=multi-user.target

systemctl enable nginx.service
systemctl start nginx.service
systemctl stop nginx.service
service nginx start
service nginx stop

自定义开机程序 rc.local

vi /etc/rc.d/rc.local 添加

/usr/local/nginx/sbin/nginx

chmod +x /etc/rc.d/rc.local

第三方软件Supervisor

你可能感兴趣的:(Linux,运维)