【问题标题】:Using Upstart instead of cron使用 Upstart 而不是 cron
【发布时间】:2014-01-11 10:20:13
【问题描述】:

我可以使用 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 运行这些任务?有可能吗?

【问题讨论】:

  • 新贵是什么意思? Crontab 可以选择在重启后执行。为此,请使用@reboot /usr/bin/node ...
  • 我想使用 Upstart 而不是 cron 按计划运行任务...
  • 使用 cron 有什么问题? Upstart 是一个基于事件的初始化守护进程,并不意味着在特定的时间运行任务,而是在特定的事件中运行。
  • @Braiam,实际上 upstart 是为了及时替换 cron ......如果你能花时间阅读“upstart 食谱”[google],你可能会看到嵌入调度系统的原因(并且计划是让它比 cron 更有能力)在新贵中。也就是说,如果 cron 可用,则应该使用它 - 因为它是适合这项工作的工具,但如果不是,upstart 可以作为替代方案。
  • @ReutSharabani "Upstart 目前无法直接处理此问题。但是,现在正在开发的“时间事件”功能将解决此问题。在时间事件可用之前,您应该使用 cron(8),或者......”也就是说,现在 cron 是他拥有的最佳选择。 Upstart还没有完全替代 cron。

标签: cron scheduled-tasks crontab schedule upstart


【解决方案1】:

这是一个非常幼稚的使用 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 更适合我的需要,因为我想保留单个备份的历史记录)

祝你好运! :-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-10
    • 1970-01-01
    • 2021-06-13
    • 2014-10-10
    • 1970-01-01
    • 1970-01-01
    • 2017-06-08
    • 2012-11-09
    相关资源
    最近更新 更多