Debian設置開機自啓動腳本的兩種方法

寫在前面的話:

  1. 版權聲明:本文爲博主原創文章,轉載請註明出處!
  2. 博主是一個小菜鳥,並且非常玻璃心!如果文中有什麼問題,請友好地指出來,博主查證後會進行更正,啾咪~~
  3. 每篇文章都是博主現階段的理解,如果理解的更深入的話,博主會不定時更新文章。
  4. 本文最後更新時間:2020.5.19

正文開始

假設開機需要自動執行的腳本文件名稱爲MyStartup

腳本內容爲:

#!/bin/bash

ls /root/ > /root/doc.txt

記得一定要有可執行權限:chmod +x MyStartup

方法一

腳本放在目錄/root/

配置方法

Applications
Settings
Session and Startup
選擇 Applications Autostart 下的 Add

在這裏插入圖片描述
在這裏插入圖片描述
添加啓動 my_startup,其中 NameDescription 都是自己寫的,隨意寫,Command 爲 /root/MyStartup(需要自啓動腳本的完整路徑):
在這裏插入圖片描述

方法二

執行腳本:

#!/bin/bash

# 刪除可能已存在的 MyStartup
FileName="MyStartup"
rm /etc/init.d/$FileName
rm /etc/rc3.d/S01$FileName
rm /etc/rc4.d/S01$FileName
rm /etc/rc5.d/S01$FileName

# 向 MyStartup 中寫入需要執行的命令
echo "#!/bin/bash" > /etc/init.d/$FileName
echo "ls /root/ > /root/doc.txt" >> /etc/init.d/$FileName

chmod +x /etc/init.d/$FileName

ln -s /etc/init.d/$FileName /etc/rc3.d/S01$FileName
ln -s /etc/init.d/$FileName /etc/rc4.d/S01$FileName
ln -s /etc/init.d/$FileName /etc/rc5.d/S01$FileName
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章