Ubuntu 20.04 開機執行自定義腳本


#1、切換至root,建立rc-local.service文件

vim  /etc/systemd/system/rc-local.service

 

#2、在里面寫入如下內容

[Unit] 
Description=/etc/rc.local Compatibility 
ConditionPathExists=/etc/rc.local 

[Service] 
Type=forking 
ExecStart=/etc/rc.local start 
TimeoutSec=0 
StandardOutput=tty 
RemainAfterExit=yes 
SysVStartPriority=99 

[Install]
WantedBy=multi-user.target 

 

#3、然后創建文件:rc.local

vim  /etc/rc.local

 

 #4、里面寫入下面內容(就是開機所要執行的腳本) 

#!/bin/sh -e 
## rc.local

# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other 
# value on error. 
# In order to enable or disable this script just change the execution  bits. 
# By default this script does nothing. 

#start script

  sudo -H -u user1 /bin/bash -c "vncserver :11 -localhost no -geometry 1440x900"
  sudo -H -u user2 /bin/bash -c "vncserver :12 -localhost no -geometry 1440x900"

#end script

echo "added sucessfully!" > /tmp/added_script.log 

exit 0

 

#5、加上權限

chmod +x /etc/rc.local

 

 

#6、啟動服務

systemctl enable rc-local

 

#7、開啟服務

systemctl start rc-local.service

 

#8、查看狀態

systemctl status rc-local.service

部分內容如下:(注意active (running))

#9、重啟並檢查test.log文件

vim  /tmp/added_script.log 

 

#10、如果里面有內容(added sucessfully!),則自啟動腳本安裝成功。


免責聲明!

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



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