1. 创建shell脚本 test.sh ,并将脚本放到指定目录(建议 /root/.config 目录)
#!/bin/bash
while [ 2 -gt 1 ]
do
echo 'ok ...' >> /root/aaa.txt
sleep 2s
done
2. 创建 mytest.service
[Unit]
Description=my first service
[Service]
ExecStart=/bin/bash /root/.config/test.sh
[Install]
WantedBy=multi-user.target
3. 移动 mytest.service 到 /etc/systemd/system/ 目录下
4. 重新加载
systemctl daemon-reload
5. 设置开机启动
systemctl enable mytest.service
systemctl start mytest.service
6. 设置时间间隔检测脚本运行可以在 test.sh 中写循环脚本