Reputation: 409
Can I use Upstart to run tasks on schedule?
Now I have this tasks in my 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
How I can run this tasks by Upstart? Is it possible?
Upvotes: 4
Views: 2509
Reputation: 31339
This is a very naive implementation of cron-like behavior using bash:
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
This example is per-day, but can be easily altered to any basis you want using the date
command.
Make sure to use a relevant wait time. I used ten minutes for a daily backup which is accurate enough for my needs (should start backup some minutes after midnight).
Don't forget you can use rsync, which is what I've used initially (but zip suited my needs more since I want to keep a history of individual backups)
Good luck! :-)
Upvotes: 1