Linux開機自啓動配置

不少時候,咱們須要將一些服務在Linux系統啓動時即自動運行,免得每次都要去手動啓動一遍,如Redis, MySQL, Nginx等。本文對CentOS與Ubuntu下開機自啓動的配置方法進行整理,供參考查閱。nginx

CentOS7的開機自啓動配置

一. rc.local方式

rc.local是CentOS之前版本的方式,在CentOS7中仍然以兼容的形式存在,雖仍可用,但不推薦(推薦使用systemd service)。redis

一、編寫須要開機自啓動的腳本,並添加執行權限shell

[root@dev-server-1 ~]# vim test_rclocal.sh

#!/bin/bash
time=`date +%F_%T`
echo $time' from rc.local' >> /tmp/test.log

[root@dev-server-1 ~]# chmod +x test_rclocal.sh複製代碼

做爲測試,上述腳本打印一個時間到/tmp/test.log文件中ubuntu

二、在/etc/rc.d/rc.local配置文件中添加腳本運行命令(使用絕對路徑)vim

[root@dev-server-1 ~]# vim /etc/rc.d/rc.local 

#!/bin/bash
# ...註釋部分
touch /var/lock/subsys/local

/root/test_rclocal.sh >/dev/null 2>/dev/null
複製代碼

三、添加/etc/rc.d/rc.local文件的執行權限centos

在centos7中,/etc/rc.d/rc.local沒有執行權限,須要手動受權bash

[root@dev-server-1 ~]# chmod +x /etc/rc.d/rc.local複製代碼

以上三步,便可使/root/test_rclocal.sh >/dev/null 2>/dev/null 命令在服務器系統啓動時自動運行。服務器

二. chkconfig方式

一、編寫須要開機自啓動的測試腳本,並添加執行權限微信

[root@dev-server-1 ~]# vim test_chkconfig.sh

#!/bin/bash

time=`date +%F_%T`
echo $time' from chkconfig' >> /tmp/test.log

[root@dev-server-1 ~]# chmod +x test_chkconfig.sh複製代碼

二、在/etc/rc.d/init.d/目錄下添加一個可執行腳本testchkconfig網絡

[root@dev-server-1 ~]# vim /etc/rc.d/init.d/testchkconfig

#!/bin/bash
# chkconfig: 2345 90 10
# description: test chkconfig

/root/test_chkconfig.sh >/dev/null 2>/dev/null

[root@dev-server-1 ~]# chmod 755 /etc/rc.d/init.d/testchkconfig複製代碼

上述testchkconfig腳本的頭部必須遵循必定的格式 # chkconfig: 2345 90 10, 其中2345指定服務在哪些執行等級中開啓或關閉,90表示啓動的優先級(0-100,越大優先級越低),10表示關閉的優先級。執行等級包括

  • 0:表示關機
  • 1:單用戶模式
  • 2:無網絡鏈接的多用戶命令行模式
  • 3:有網絡鏈接的多用戶命令行模式
  • 4:保留未使用
  • 5:帶圖形界面的多用戶模式
  • 6:從新啓動

三、加入開機啓動服務列表

[root@dev-server-1 ~]# chkconfig --add testchkconfig
[root@dev-server-1 ~]# chkconfig --list

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]'.

netconsole         0:off    1:off    2:off    3:off    4:off    5:off    6:off
network            0:off    1:off    2:on    3:on    4:on    5:on    6:off
testchkconfig      0:off    1:off    2:on    3:on    4:on    5:on    6:off複製代碼

使用 chkconfig --list 可查看當前加入開機自啓動的服務列表,但如Note部分所述,該命令只顯示SysV服務,不包含原生的systemd服務,查看systemd服務可以使用systemctl list-unit-files命令。

以上三步,便可使/root/test_chkconfig.sh >/dev/null 2>/dev/null 命令在服務器系統啓動時自動運行。

chkconfig的其它命令參考

$chkconfig --list # 表示查看全部服務在各個運行級別下的狀態。
$chkconfig testchkconfig on # 表示指定服務在全部的運行級別下都是開啓狀態。
$chkconfig testchkconfig off # 表示指定服務在全部的運行級別下都是關閉狀態。
$chkconfig --level 5 testchkconfig on # 表示指定服務在運行級別5圖形模式的狀態下開機啓動服務。
$chkconfig --level 5 testchkconfig off # 表示指定服務在運行級別5圖形模式的狀態下開機不啓動服務。複製代碼

三. 自定義systemd service方式

CentOS7的systemd服務腳本存放在:/usr/lib/systemd/system(系統級)/usr/lib/systemd/user(用戶級)下,以.service結尾。這裏以nginx爲例

一、在/usr/lib/systemd/system目錄下建立nginx.service文件

[devuser@test-server-1 ~]$ sudo vim /usr/lib/systemd/system/nginx.service

[Unit]
Description=nginx server
Documentation=http://nginx.org/en/docs/
# 依賴服務,僅當依賴的服務啓動以後再啓動自定義的服務
After=network.target remote-fs.target nss-lookup.target  

[Service]
# 啓動類型,包括simple、forking、oneshot、notify、dbus
Type=forking
# pid文件路徑
PIDFile=/var/run/nginx.pid
# 啓動前執行的操做
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
# 啓動命令
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
# 是否給服務分配獨立的臨時空間
PrivateTmp=true

[Install]
# 服務安裝的用戶模式,通常使用multi-user便可
WantedBy=multi-user.target複製代碼

其中Service部分的Type包括以下幾種類型:

  • simple:表示ExecStart啓動的進程是該服務的主進程。若是它須要爲其餘進程提供服務,那麼必須在該服務啓動以前先創建好通訊渠道,好比套接字,以加快後續單元的啓動速度。
  • forking:表示ExecStart進程將會在啓動時使用fork()函數,這是傳統Unix系統的作法,也就是說這個進程將由systemd進程fork出來,而後當該進程都準備就緒時,systemd進程退出,而fork出來的進程做爲服務的主進程繼續運行,對於此類型的進程,建議設置PIDFile選項,以幫助systemd準肯定位該服務的主進程。
  • oneshot:該進程會在systemd啓動後續單元以前退出,適用於僅須要執行一次的程序。好比清理磁盤,你只須要執行一次,不須要一直在後臺運行這個程序。
  • notify:與simple相似,不一樣之處在於該進程會在啓動完成以後經過sd_notify之類的接口發送一個通知消息。systemd在啓動後續單元以前,必須確保該進程已經成功地發送了一個消息。
  • dbus:該進程須要在D-Bus上得到一個由BusName指定的名稱,systemd將會在啓動後續單元以前,首先確保該進程已經成功獲取了指定D-Bus名稱。

二、 開啓開機自啓動

[devuser@test-server-1 ~]$ sudo systemctl enable nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.複製代碼

以上兩步,就將nginx服務配置成了在操做系統啓動時自動啓動。

其它命令參考

$sudo systemctl start nginx.service # 啓動
$sudo systemctl restart nginx.service # 重啓
$sudo systemctl reload nginx.service # 重載
$sudo systemctl stop nginx.service # 中止
$sudo systemctl status nginx.service # 查看服務狀態
$sudo systemctl cat nginx.service # 查看服務配置

$systemctl list-unit-files |grep nginx # 查看服務enabled狀態
$sudo systemctl disable nginx.service # 關閉開機自啓動


$sudo journalctl -f -u nginx.service # 查看日誌
$sudo systemctl daemon-reload # 配置修改後,從新加載複製代碼

根據以上配置,經過start啓動nginx服務時,報PID file /var/run/nginx.pid not readable (yet?) after start.的錯誤,啓動失敗,日誌以下

[devuser@test-server-1 ~]$ sudo journalctl -f -u nginx.service
-- Logs begin at Wed 2020-03-25 09:14:55 CST. --
Mar 25 11:02:27 test-server-1 nginx[14144]: nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
Mar 25 11:02:27 test-server-1 systemd[1]: PID file /run/nginx.pid not readable (yet?) after start.
Mar 25 11:04:29 test-server-1 systemd[1]: nginx.service start operation timed out. Terminating.
Mar 25 11:04:29 test-server-1 systemd[1]: Failed to start nginx.
Mar 25 11:04:29 test-server-1 systemd[1]: Unit nginx.service entered failed state.
Mar 25 11:04:29 test-server-1 systemd[1]: nginx.service failed.複製代碼

從字面看是PID文件不可讀,查看/var/run/nginx.pid,該文件也確實不存在,查看nginx.conf配置文件,發現是pid /var/run/nginx.pid;這行配置被註釋掉了, 若是不指定pid文件位置,nginx默認會把pid文件保存在logs目錄中。因此出現systemd啓動服務時找不到pid文件而報錯,將nginx.conf中的pid配置註釋去掉,重啓nginx.service便可。

Ubuntu18.04的開機自啓動配置

在Ubuntu18.04中,主要也是以systemd服務來實現開機自啓動,systemd默認讀取/etc/systemd/system/下的配置文件,該目錄下的一些文件會連接到/lib/systemd/system/下的文件。

所以能夠在/etc/systemd/system/目錄下面建立一個自啓動服務配置,之內網穿透服務frp客戶端爲例,如

[Unit]
Description=frpc
After=network.target
Wants=network.target

[Service]
TimeoutStartSec=30
ExecStart=/home/devuser/apps/frp/frpc -c /home/devuser/apps/frp/frpc.ini
ExecStop=/bin/kill $MAINPID
Restart=1

[Install]
WantedBy=multi-user.target複製代碼

各配置項與CentOS相似。而後將服務器加到自啓動列表中並啓動服務

$sudo systemctl enable frpc
$sudo systemctl start frpc複製代碼

其它更多systemctl命令與CentOS相似。

也可使用/lib/systemd/system/rc-local.service來執行一些開機須要執行的腳本,該文件內容爲

#  SPDX-License-Identifier: LGPL-2.1+
#
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

# This unit gets pulled automatically into multi-user.target by
# systemd-rc-local-generator if /etc/rc.local is executable.
[Unit]
Description=/etc/rc.local Compatibility
Documentation=man:systemd-rc-local-generator(8)
ConditionFileIsExecutable=/etc/rc.local
After=network.target

[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no複製代碼

從Description看它是爲了兼容以前版本的/etc/rc.local的,該服務啓動命名就是/etc/rc.local start,將該文件連接到/etc/systemd/system下

$ sudo ln -s /lib/systemd/system/rc-local.service /etc/systemd/system/rc-local.service  複製代碼

建立/etc/rc.local文件,並賦予可執行權限

$ vim /etc/rc.local
#!/bin/bash  
echo "test rc " > /var/test.log  

$ sudo chmod +x /etc/rc.local複製代碼

<>

做者:空山新雨,一枚仍在學習路上的IT老兵 近期做者寫了幾十篇技術博客,內容包括Java、Spring Boot、Spring Cloud、Docker,技術管理心得等
歡迎關注做者微信公衆號:空山新雨的技術空間,一塊兒學習成長

微信公衆號

相關文章
相關標籤/搜索