2013-12-23 55 views
4

我可以使用Upstart按計劃運行任務嗎?使用Upstart代替cron

現在我有我的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 

我怎麼可以運行通過新貴這個任務?可能嗎?

+3

你是什麼意思與暴發戶? Crontab可以在重新啓動後執行。爲此,請使用'@reboot/usr/bin/node ...' – fedorqui

+0

我想按照計劃使用Upstart而不是cron運行任務... – inetbug

+0

使用cron有什麼問題? Upstart是一個基於事件的init守護進程,並不意味着在特定的時間運行任務*,而是在特定的事件中運行。 – Braiam

回答

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,這就是我最初使用(但拉鍊適合我的需要,更因爲我想保留單個備份的歷史)

祝你好運! :-)