Fedora 22(15以上版本)開機自啟動腳本


  前段時間做了一個網站btdog磁力btdog電視直播。DHT爬蟲需要消耗比較多的資源,原來的服務器不夠用了,於是自己使用電腦搭了一台服務器,使用Fedora22系統。在Fedora22中自動寫了些開機自啟動腳本,但始終找不到放在哪里。折騰了下,發現原來Fedora 從15開始,系統初始化軟件開始由initscript轉向了systemd方式,原來要寫開機啟動腳本一般寫在rc.local里面,但現在rc.local已經不存在了,不過systemd仍然有rc-local服務。

  編輯/usr/lib/systemd/system/rc-local.service  

#  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.d/rc.local is executable.
[Unit]
Description=/etc/rc.d/rc.local Compatibility
ConditionFileIsExecutable=/etc/rc.d/rc.local
After=network.target

[Service]
Type=forking
ExecStart=/etc/rc.d/rc.local start
TimeoutSec=0
RemainAfterExit=yes

  可見我們只要新建文件/etc/rc.d/rc.local 然后加入自己的腳本就可以了

  新建rc.local  

touch /etc/rc.d/rc.local

  vim /etc/rc.d/rc.local

#!/bin/bash
#下面是你需要執行的腳本

#####################################

  賦以可執行權限

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

  然后使用systemd開機自啟動

systemctl enable rc-local.service

  提示錯誤如下: 

The unit files have no [Install] section. They are not meant to be enabled
using systemctl.
Possible reasons for having this kind of units are: 1) A unit may be statically enabled by being symlinked from another unit's .wants/ or .requires/ directory. 2) A unit's purpose may be to act as a helper for some other unit which has  a requirement dependency on it. 3) A unit may be started when needed via activation (socket, path, timer, D-Bus, udev, scripted systemctl call, ...).

  這個時候,我們需要在/usr/lib/systemd/system/rc-local.service 中加入[Install]區域,用於告訴systemd需要在多用戶啟動時還是圖形啟動時自啟動這個腳本

  編輯/usr/lib/systemd/system/rc-local.service 

#  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.d/rc.local is executable.
[Unit]
Description=/etc/rc.d/rc.local Compatibility
ConditionFileIsExecutable=/etc/rc.d/rc.local
After=network.target

[Service]
Type=forking
ExecStart=/etc/rc.d/rc.local start
TimeoutSec=0
RemainAfterExit=yes
SysVStartPriority=99 [Install] WantedBy=multi-user.target

  好了,然后在使用systemd開機自啟動

systemctl enable rc-local.service 

  然后查看啟動狀態

#systemctl status rc-local.service
● rc-local.service - /etc/rc.d/rc.local Compatibility
   Loaded: loaded (/usr/lib/systemd/system/rc-local.service; enabled; vendor preset: disabled)
   Active: active (exited) since 一 2015-06-22 20:23:23 CST; 28min ago

6月 22 20:23:23 btdog systemd[1]: Starting /etc/rc.d/rc.local Compatibility...
6月 22 20:23:23 btdog rc.local[3973]: /etc/rc.d/rc.local:行4: [Install]: 未找到命令
6月 22 20:23:23 btdog systemd[1]: Started /etc/rc.d/rc.local Compatibility.
6月 22 20:27:28 btdog systemd[1]: Started /etc/rc.d/rc.local Compatibility.
6月 22 20:37:05 btdog systemd[1]: [/usr/lib/systemd/system/rc-local.service:20] Support for option SysVStartPriority=...ignored
Hint: Some lines were ellipsized, use -l to show in full.

  啟動成功~~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM