linux添加開機啓動腳本

說明


0——關機,
1——單用戶,就是咱們以前修改root帳戶密碼的模式,
2——多用戶模式,但比3模式少了一個nfs服務
3——多用戶命令行模式,最經常使用
4——保留級別暫時沒用,
5——圖形模式,
6——重啓
html

 

1、chkconfig-CentOS6之前

chkconfig就是CentOS6之前用來控制系統服務的工具,系統開機時啓動的部分服務存儲在/etc/init.d/目錄下。咱們能夠把須要開機啓動的服務放在這個目錄下而後用chkconfig來管理。mysql

查看命令

chkconfig --list #列出全部的系統服務。

操做命令

chkconfig --add httpd #增長httpd服務。
chkconfig --del httpd #刪除httpd服務。
chkconfig --level httpd 2345 on #設置httpd在運行級別爲二、三、四、5的狀況下都是on(開啓)的狀態。
chkconfig --list mysqld #列出mysqld服務設置狀況。
chkconfig --level 35 mysqld on #設定mysqld在等級3和5爲開機運行服務,--level 35表示操做只在等級3和5執行,on表示啓動,off表

示例linux

chkconfig --add nginx #添加nginx服務開機啓動項

建立命令

(略)nginx

 

2、systemd-CentOS6之後

查看命令

systemctl list-units --all --type=service #查看全部服務
systemctl list-units --type=service #查看全部已經啓動的服務

操做命令

針對單一服務的sql

systemctl enable crond ##設置開機啓動crond服務或工具
systemctl disable crond ##設置關閉開機啓動crond服務或工具
systemctl status crond ##查看crond服務當前狀態,如是否運行
systemctl stop crond ##中止crond服務是,但開機仍會運行
systemctl start crond ##開啓crond服務
systemctl restart crond ##重啓crond服務
systemctl is-enabled crond ##檢查crond服務是否開機啓動

示例:vim

systemctl enable nginx.service #添加nginx服務開機啓動項

 

建立命令

vim /lib/systemd/system/nginx.service #在系統服務目錄裏建立nginx.service文件

內容:bash

[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

 

說明:工具

[Unit]:服務的說明
Description:描述服務
After:描述服務類別
[Service]服務運行參數的設置
Type=forking是後臺運行的形式
ExecStart爲服務的具體運行命令
ExecReload爲重啓命令
ExecStop爲中止命令
PrivateTmp=True表示給服務分配獨立的臨時空間
注意:[Service]的啓動、重啓、中止命令所有要求使用絕對路徑
[Install]運行級別下服務安裝的相關設置,可設置爲多用戶,即系統運行級別爲3ui

保存退出。spa

systemctl enable nginx.service #設置開機啓動


3、chkconfig 和systemctl 對比

任            務 舊         指        令 新        指      令
使某服務自動啓動 chkconfig –level 3 httpd on systemctl enable httpd.service
使某服務不自動啓動 chkconfig –level 3 httpd off systemctl disable httpd.service
檢查服務狀態 service httpd status systemctl status httpd.service
顯示全部已啓動的服務 chkconfig –list systemctl list-units –type=service
啓動某服務 service httpd start systemctl start httpd.service
中止某服務 service httpd stop systemctl stop httpd.service
重啓某服務 service httpd restart systemctl restart httpd.service

 

4、對照表

1.Systemd 命令和 sysvinit 命令的對照表

2.Sysvinit 運行級別和 systemd 目標的對應表

 

參考文檔

1.Linux系統管理初步(七)系統服務管理、chkconfig與systemd 編輯中:https://www.cnblogs.com/superlinux/p/bfd4812adffaccb36520279aaafcc160.html

2.Nginx+Center OS 7.2 開機啓動設置:https://www.cnblogs.com/piscesLoveCc/p/5867900.html

3.Linux 設置程序開機自啓動 (命令systemctl 和 chkconfig用法區別比較):https://blog.csdn.net/kenhins/article/details/74518978

相關文章
相關標籤/搜索