1. Linux 设置开机自动运行脚本
1.1. auto_run_script.sh
创建脚本,文件后缀 .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
创建 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
从服务的内容可以看出来,最终还是会调用 /home/luckyboy/auto_run_script.sh 这个脚本。
1.3. 更新 systemd 配置文件
sudo systemctl daemon-reload
sudo systemctl enable auto_run_script.service
1.4. 重启系统
sudo reboot