Linux實現腳本開機自啟動


參考鏈接:https://blog.csdn.net/weixin_40343504/article/details/82457990

Ubuntu系統實現腳本開機自啟動

0、序言

  很多時候,我們使用Linux時,為了方便操作,需要用到一些自啟動的腳本,對於不同的Linux系統來說,自啟動方式各有不同,我這里記錄的是Ubuntu 20.0系統的;文章后面會補充參考鏈接的博客部分內容,僅供參考學習使用。該方法也可以用在移植系統時,把開機啟動服務移植進去。  

  在 Ubuntu 16以前,有個很簡單的 rc.local ,在這個文件內寫上你要執行的命令,在開機后,系統就會以管理員權限去執行這些命令。但是,這個啟動腳本的控制粒度並不是很精細。所以,在Ubuntu18 版本開始,Ubuntu使用了 systemd 替代了 initd 管理系統。並且默認已經取消了 /etc/rc.local 文件,如果要在 Ubuntu16 以后使用 rc.local 這個方法,可以參考我i另一篇博文:https://www.cnblogs.com/xingboy/p/15696526.html

1、建立開機啟動服務

  跳轉到 /etc/systemd/system 文件夾,移植系統時也是到你的文件系統的這個文件夾;

  新建開機啟動服務 sudo vim my_start.service

  [Unit] 
  #該文件是自己建立的啟動服務文件,
  #/bin/my_start.sh 是你要啟動的腳本所在的路徑以及文件名,其他保持默認格式即可
 
  Description=/bin/my_start.sh Compatibility
  ConditionPathExists=/bin/my_start.sh
  
  [Service]
  Type=forking
 ExecStart=/bin/my_start.sh
 TimeoutSec=0
 StandardOutput=tty
 RemainAfterExit=yes
 SysVStartPriority=99
 
 [Install]
 WantedBy=multi-user.target

  給啟動服務添加權限 sudo chmod 777 my_start.service

2、創建要自啟動的腳本

  因為我在第一步的啟動服務里聲明的啟動腳本路徑以及名稱,所以建立的啟動腳本名稱路徑要跟啟動服務里聲明的一致

       跳轉到啟動服務里聲明的 /bin 目錄,添加啟動腳本 vim my_start.sh 

#!/bin/sh
  
#該文件為自定義開機啟動腳本,可以用該腳本執行一些你需要開機啟動的腳本
 
 PASSWORD="your_password" 

#你如果做系統移植時,可以把 roo t的密碼參數設置為 export,然后這里設置為
PASSWORD=${ROOT_PASSWORD},就是直接提取你的 root 密碼
#具體你的系統保持密碼的參數是什么要看你的移植的系統了,我的是 ROOT_PASSWORD

 echo ${PASSWORD} | sudo -S /bin/myStart/set_net_core.sh #echo ${PASSWORD} | sudo -S 是實現腳本自動把執行sudo命令時要輸入的密碼輸入進去

#我在這里的腳本實現的是:啟動 /bin/myStart 文件夾里的 set_net_core.sh 腳本
#這種方法的好處就是我只需要做一個啟動服務專門用來執行一個腳本去執行其他腳本,這樣后續還有新的需要開機自啟動腳本就只需要把執行命令寫入到這里即可

  給啟動腳本添加權限 sudo chmod 777 my_start.sh

3、啟動腳本自啟動服務

  跳轉到 /etc/systemd/system 文件夾,其實不進入也行,我這里為了加深記憶才重新進入一次;

      執行指令 sudo systemctl enable my_start.service 啟動腳本自啟動服務,然后使用指令 reboot 重啟你的系統;

  如果你是做系統移植需要用到的,你需要移植完系統,燒錄后執行一次該指令,只需要執行一次即可,后續你斷電關機重啟都不用執行了

 

以下內容轉自參考鏈接,補充其他系統的部分

一、CentOS系統和Redhat系統如下
  1.修改/etc/rc.d/rc.local為

#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
 
touch /var/lock/subsys/local
source /root/Desktop/start.sh         

  “sourece /root/Desktop/start.sh”為要添加開機自啟動的腳本,並且要給“/root/Desktop/start.sh”賦予執行權限(chmod +x /root/Desktop/start.sh)
  2.chmod 777 rc.local
  3.重啟系統后就會開始執行預設置執行的腳本。(此種自啟動方式不會在GUI界面顯示執行的具體進程)**

 

二、Suse 系統如下
  1.suse15
    1)將需要開機啟動腳本xxx.sh復制到 /etc/profile.d
      chmod 777 xxx.sh
    2)將系統改成root 自動登錄
      vi /etc/sysconfig/displaymanager
      displaymanager-autologin=“root”

  備注:
    a.安裝suse15系統參考:http://morecoder.com/article/1120393.html

     b.查看OS IP指令為“ip -4 address”
     c.遠程連接請用putty,ssh 工具無法使用。

  2.Suse12

    1.修改/etc/init.d/下面有個boot.local為:

#! /bin/sh
#
# Copyright (c) 2002 SuSE Linux AG Nuernberg, Germany.  All rights reserved.
#
# Author: Werner Fink, 1996
#         Burchard Steinbild, 1996
#
# /etc/init.d/boot.local
#
# script with local commands to be executed from init on system startup
#
# Here you should add things, that should happen directly after booting
# before we're going to the first run level.
#
source /root/Desktop/start.sh

    “sourece /root/Desktop/start.sh”為要添加開機自啟動的腳本,並且要給“/root/Desktop/start.sh”賦予執行權限(chmod +x /root/Desktop/start.sh)
    2.重啟系統后就會開始執行預設置執行的腳本。(此種自啟動方式不會在GUI界面顯示執行的具體進程)

 

在Redhat Redflag centOS fc linux系統里面腳本的啟動先后順序
  第一步:通過/boot/vm進行啟動 vmlinuz
  第二步:init /etc/inittab
  第三步:啟動相應的腳本,並且打開終端
    rc.sysinit
    rc.d(里面的腳本)
    rc.local
  第四步:啟動login登錄界面 login
  第五步:在用戶登錄的時候執行sh腳本的順序:每次登錄的時候都會完全執行的
    /etc/profile.d/file
    /etc/profile
    /etc/bashrc
    /root/.bashrc
    /root/.bash_profile
  Redhat中的運行模式2、3、5都把/etc/rc.d/rc.local做為初始化腳本中的最后一個,所以用戶可以自己在這個文件中添加一些需要在其他初始化工作之后,登錄之前執行的命令

 


免責聲明!

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



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