4

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?

inetbug
  • 409
  • 6
  • 16
  • 3
    What do you mean with upstart? Crontab has the option to be executed after a reboot. For that, use `@reboot /usr/bin/node ...` – fedorqui Dec 23 '13 at 09:51
  • I want to run tasks on schedule using Upstart instead of cron... – inetbug Dec 23 '13 at 10:02
  • what is wrong with using cron? Upstart is a event based init daemon and isn't meant to run task at specific *time* but at specific events. – Braiam Dec 26 '13 at 13:26
  • @Braiam, actually upstart is meant to replace cron in time... If you can take the time and read the "upstart cookbook" [google], you may see the reason behind embedding a scheduling system (and the plan is for it to be more capable than cron) in upstart. That said, **if cron is available it should be used** - since it's the right tool for the job, but if it's not, upstart can be an alternative. – Reut Sharabani Dec 26 '13 at 14:13
  • 3
    @ReutSharabani "**This cannot currently be handled by Upstart** directly. However, the "Temporal Events" feature is being worked on now will address this. Until Temporal Events are available you **should either use cron(8)**, or ..." that said, *right now* cron is the best option he has. Upstart is not *yet* a complete replacement of cron. – Braiam Dec 26 '13 at 14:23

1 Answers1

1

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! :-)

Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88