1. Linux 設置開機自動運行腳本
1.1. auto_run_script.sh
創(chuàng)建腳本唯绍,文件后綴 .sh
$ cat /home/luckyboy/auto_run_script.sh
#!/bin/bash
of=/home/luckyboy/output.txt
date >> $of
hostname >> $of
1.2. auto_run_script.service
創(chuàng)建 systemd 啟動服,文件后綴 .service
$ cat /etc/systemd/system/auto_run_script.service
[Unit]
Description=Run a Custom Script at Startup
After=default.target
[Service]
ExecStart=/home/luckyboy/auto_run_script.sh
[Install]
WantedBy=default.target
從服務的內(nèi)容可以看出來甚牲,最終還是會調(diào)用 /home/luckyboy/auto_run_script.sh 這個腳本弛姜。
1.3. 更新 systemd 配置文件
sudo systemctl daemon-reload
sudo systemctl enable auto_run_script.service
1.4. 重啟系統(tǒng)
sudo reboot