Linux 设置开机自动运行脚本

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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容