gpt4 book ai didi

cron - 使用 Upstart 代替 cron

转载 作者:行者123 更新时间:2023-12-04 10:44:46 25 4
gpt4 key购买 nike

我可以使用 Upstart 来按计划运行任务吗?

现在我的 crontab 中有这个任务:

0 3 * * * /usr/bin/node ~/update.js
0 9 * * * /usr/bin/node ~/update.js
0 12 * * * /usr/bin/node ~/update.js
0 15 * * * /usr/bin/node ~/update.js

我如何通过 Upstart 运行此任务?是否可以?

最佳答案

这是使用 bash 对类似 cron 的行为的一种非常幼稚的实现:

description "cron-like naive backup service"
author "Reut Sharabani"

stop on [016] or backup-stop

start on backup-start or filesystem

# restart for 20 times, every 5 seconds, if crashes
respawn
respawn limit 20 5

script
# actual backup
logger "starting backup"

# folder to back-up to
BKP_FOLDER="/home/XXXX/YYYY/backups"

# backups log file
LOG_FILE="/home/XXXX/YYYY/backups.log"

# logger "entering backup loop"
while true
do

# verify log file exists
if [ ! -e "$LOG_FILE" ]
then
logger "log file for backup service did not exist, creating one in "$LOG_FILE
echo "Backup logs: " > $LOG_FILE
fi
# verify destination folder exist
if [ ! -d "$BKP_FOLDER" ]
then
logger "Backup service folder did not exist, creating it in "$BKP_FOLDER
mkdir $BKP_FOLDER
fi


# logger "checking last backup"

# generate current backup, makes backups daily
# (if we changed to +%M-%d-%m-%y it'd be per minute...)
current_bkp="`date +%d-%m-%Y`"

# generate last backed-up file
last_bkp=`tail -1 $LOG_FILE`

# check if this file wasn't already backed-up
logger "checking $current_bkp against last status: $last_bkp"

if [ "$last_bkp" = "$current_bkp finished" ]
then
logger "date $current_bkp already backed up!"
# check for existing backup every hour
sleep 600
else

# make sure a backup process isn't currently working!
# a backup process is any process zipping into XXXX for the matter...
if [ "`pgrep -f 'zip.*XXXX'`" = '' ]
then
# no process is running, back things up
echo "$current_bkp started" >> $LOG_FILE
logger $current_bkp" is being backed up..."
# zip all folders in this list, array definition
# does not work in upstart for some reason...
zip -r $BKP_FOLDER"/"$current_bkp".zip" '/home/.../CENSORED' '/home/.../CENSORED' '/home/.../CENSORED'
echo "$current_bkp finished" >> $LOG_FILE
logger "date $current_bkp backed up!"

else
# backup process is still running, do nothing
logger "Backup stopped because process $BKP_PROC was already backing up things..."
logger "process info:"
logger "`ps aux | grep $BKP_PROC | grep zip`"
fi

fi
done
end script

此示例是每天的,但可以使用 date 轻松更改为您想要的任何基础。命令。

确保使用相关的等待时间。我使用十分钟进行每日备份,这足以满足我的需要(应该在午夜后几分钟开始备份)。

不要忘记您可以使用 rsync,这是我最初使用的(但 zip 更适合我的需求,因为我想保留单个备份的历史记录)

祝你好运! :-)

关于cron - 使用 Upstart 代替 cron,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20739452/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com