Tips
个人学习内容,仅供参考
实现步骤
准备脚本
1.创建脚本文件
touch test.sh
2.编写完脚本后,修改脚本文件权限
chmod 777 test.sh //权限看情况定
3.测试脚本效果
./test.sh //运行脚本
添加开机自启服务
1.创建service文件
cd /etc/stytemd/system
touch test.service
2.编写service文件
[Unit]
Description=test service after starting
[Service]
ExecStart=~/test.sh
[Install]
WantedBy=multi-user.target
关键:在ExecStart参数中填入test.sh的路径
3.使服务生效
sudo systemctl daemon-reload //重新加载所有的systemd服务
sudo systemctl start tets.service //启动服务
sudo systemctl enable test.service //使能服务自启动
补充:
sudo systemctl enable test.service /* 使能自启动 */
sudo systemctl start test.service /* 启动 */
sudo systemctl stop test.service /* 停止 */
sudo systemctl restart test.service /* 重启 */
sudo systemctl status test.service /* 查看状态 */