使用Upstart代替cron

使用Upstart代替cron,cron,scheduled-tasks,crontab,schedule,upstart,Cron,Scheduled Tasks,Crontab,Schedule,Upstart,我可以使用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

我可以使用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
此示例是每天一次的,但是可以使用命令轻松地更改为您想要的任何基础

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

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


祝你好运!:-)

你对暴发户是什么意思?Crontab具有在重新启动后执行的选项。为此,请使用
@reboot/usr/bin/node…
我想使用Upstart而不是cron按计划运行任务…使用cron有什么问题?Upstart是一个基于事件的init守护进程,它不是在特定时间运行任务,而是在特定事件中运行任务。@Braiam,实际上Upstart是用来及时替换cron的。。。如果你能花点时间阅读“upstart cookbook”[google],你可能会发现在upstart中嵌入调度系统(计划是让它比cron更有能力)的原因。这就是说,如果cron可用,就应该使用它-因为它是作业的正确工具,但是如果它不可用,upstart可以是一个替代工具。@ReutSharabani“这目前不能由upstart直接处理。但是“目前正在开发的功能将解决这一问题。在临时事件可用之前,您应该使用cron(8),或者……”也就是说,现在cron是他最好的选择。Upstart还不是cron的完全替代品。