添加 shell/只执行一次的定时任务.md
This commit is contained in:
parent
62e3557721
commit
cc2e7a707b
1 changed files with 26 additions and 0 deletions
26
shell/只执行一次的定时任务.md
Normal file
26
shell/只执行一次的定时任务.md
Normal file
|
@ -0,0 +1,26 @@
|
|||
```shell
|
||||
#!/bin/bash
|
||||
|
||||
# filename: sync.sh
|
||||
|
||||
LOCK_FILE="/tmp/sync.lock"
|
||||
LOG_FILE="/var/log/sync.log"
|
||||
|
||||
(
|
||||
flock -n 9 || {
|
||||
echo "$(date): 已有同步任务在运行,退出。" >> "$LOG_FILE"
|
||||
exit 0
|
||||
}
|
||||
|
||||
if [ -f "/root/abc" ]; then
|
||||
echo "$(date): 检测到触发文件,开始同步..." >> "$LOG_FILE"
|
||||
rsync -avP /root/def /data/sync/ >> "$LOG_FILE" 2>&1
|
||||
|
||||
echo "$(date): 同步完成,移除定时任务。" >> "$LOG_FILE"
|
||||
crontab -l 2>/dev/null | grep -v "sync.sh" | crontab -
|
||||
else
|
||||
echo "$(date): 未检测到触发文件,跳过同步。" >> "$LOG_FILE"
|
||||
fi
|
||||
) 9>"$LOCK_FILE"
|
||||
|
||||
```
|
Loading…
Add table
Add a link
Reference in a new issue