CentOS 7 添加開機啟動有兩種方式:
一鞋屈、添加開機啟動服務(wù)
Centos 系統(tǒng)服務(wù)腳本目錄:
/usr/lib/systemd/
有系統(tǒng)(system)和用戶(user)之分,如需要開機沒有登陸情況下就能運行的程序,存在系統(tǒng)服務(wù)(system)里项贺,即:
/usr/lib/systemd/system/
反之勇边,用戶登錄后才能運行的程序,存在用戶(user)里横腿,服務(wù)以.service結(jié)尾颓屑。
/usr/lib/systemd/user/
這邊以nginx開機運行為例:
1.建立服務(wù)文件
vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
User=nginx
Group=nginx
ExecStart=/etc/init.d/nginx start
ExecReload=/etc/init.d/nginx restart
ExecStop=/etc/init.d/nginx stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
Description:描述服務(wù)
After:描述服務(wù)類別
[Service]服務(wù)運行參數(shù)的設(shè)置
Type=forking是后臺運行的形式
User服務(wù)啟動用戶
Group服務(wù)啟動用戶組
ExecStart為服務(wù)的具體運行命令
ExecReload為重啟命令
ExecStop為停止命令
PrivateTmp=True表示給服務(wù)分配獨立的臨時空間
注意:[Service]的啟動斤寂、重啟、停止命令全部要求使用絕對路徑
[Install]服務(wù)安裝的相關(guān)設(shè)置揪惦,可設(shè)置為多用戶
2.保存目錄
以754的權(quán)限保存在目錄:
/lib/systemd/system
3.設(shè)置開機自啟動
systemctl enable nginx.service
4.其他命令
任務(wù) | 舊指令 | 新指令 |
---|---|---|
使某服務(wù)自動啟動 | Chkconfig --level 3 httpd on | systemctl enable httpd.service |
使某服務(wù)不自動啟動 | chkconfig --level 3 httpd off | systemctl disable httpd.service |
檢查服務(wù)狀態(tài) | service httpd status | systemctl status httpd.service (服務(wù)詳細信息) systemctl is-active httpd.service(僅顯示是否Active) |
顯示所有已啟動的服務(wù) | chkconfig --list | systemctl list-unit-files systemctl list-units --type=service |
啟動某服務(wù) | service httpd start | systemctl start httpd.service |
停止某服務(wù) | service httpd stop | systemctl stop httpd.service |
重啟某服務(wù) | service httpd restart | systemctl restart httpd.service |
啟動nginx服務(wù):
systemctl start nginx.service
設(shè)置開機自啟動:
systemctl enable nginx.service
停止開機自啟動:
systemctl disable nginx.service
查看服務(wù)當(dāng)前狀態(tài):
systemctl status nginx.service
重新啟動服務(wù):
systemctl restart nginx.service
查看所有已啟動的服務(wù):
systemctl list-units --type=service
systemctl list-unit-files
常用服務(wù)文件
1.nginx.service
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
[Install]
WantedBy=multi-user.target
2.mysql.service
[Unit]
Description=mysql
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
#ExecReload=/usr/local/mysql/support-files/mysql.server restart
#ExecStop=/usr/local/mysql/support-files/mysql.server stop
#PrivateTmp=true
[Install]
WantedBy=multi-user.target
3.php-fpm.service
[Unit]
Description=php
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/php/sbin/php-fpm
[Install]
WantedBy=multi-user.target
4.redis.service
[Unit]
Description=Redis
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server /etc/redis.conf
ExecStop=kill -INT `cat /tmp/redis.pid`
User=www
Group=www
[Install]
WantedBy=multi-user.target
5.supervisord.service
[Unit]
Description=Process Monitoring and Control Daemon
After=rc-local.service
[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf
SysVStartPriority=99
[Install]
WantedBy=multi-user.target
二遍搞、添加開機啟動腳本
在centos7中增加腳本有兩種常用的方法,以腳本autostart.sh為例:
#!/bin/bash
#description:開機自啟腳本
/mnt/shell/autostart.sh #啟動nginx
方法一
1器腋、賦予腳本可執(zhí)行權(quán)限(/mnt/shell/autostart.sh是你的腳本路徑)
chmod +x /mnt/shell/autostart.sh
2溪猿、打開/etc/rc.d/rc/local文件,在末尾增加如下內(nèi)容
/mnt/shell/autostart.sh
3纫塌、在centos7中诊县,/etc/rc.d/rc.local的權(quán)限被降低了,所以需要執(zhí)行如下命令賦予其可執(zhí)行權(quán)限
chmod +x /etc/rc.d/rc.local
方法二
1措左、將腳本移動到/etc/rc.d/init.d目錄下
mv /mnt/shell/autostart.sh /etc/rc.d/init.d
2依痊、增加腳本的可執(zhí)行權(quán)限
chmod +x /etc/rc.d/init.d/autostart.sh
3、添加腳本到開機自動啟動項目中
cd /etc/rc.d/init.d
chkconfig --add autostart.sh
chkconfig autostart.sh on