Linux Cron Bash Script Template
作者Lou Xiao创建时间2020-04-20 17:24:00更新时间2020-11-23 21:21:00
1.双击鼠标左键复制此行;2.单击复制所有代码。
1
#!/usr/bin/env bash
2
3
SCRIPT_PATH="$( cd "$(dirname "$0")" ; pwd -P )"
4
SCRIPT_NAME=$(basename "$0")
5
cd "${SCRIPT_PATH}"
6
7
# prepare log file
8
mkdir -p logs
9
LOG_DATE="$(date +'%Y%m%d')"
10
LOG_LINK="logs/${SCRIPT_NAME}.log"
11
LOG_FILE="logs/${SCRIPT_NAME}.log-${LOG_DATE}"
12
exec &>>"$LOG_FILE"
13
14
(
15
flock -n 99 || exit 1
16
date +'{{{ %Y%m%d %H%M%S'
17
18
# update symbol link
19
if [ -L "$LOG_LINK" -o ! -e "$LOG_LINK" ];then
20
[ -L "$LOG_LINK" ] && unlink "$LOG_LINK"
21
ln -s "$(basename $LOG_FILE)" "$LOG_LINK"
22
fi
23
24
# compress/delete log file
25
find logs -type f -iname "${SCRIPT_NAME}.log-????????" -mtime +3 -exec gzip -9v "{}" \;
26
find logs -type f -iname "${SCRIPT_NAME}.log-????????.gz" -mtime +30 -exec rm -v "{}" \;
27
28
# Todo: here is your code.
29
timeout -s KILL 5m sleep 1
30
echo "Done"
31
32
33
date +'}}} %Y%m%d %H%M%S'
34
) 99>"${SCRIPT_NAME}.lock"
文章目录