#!/bin/bash # Usage: # flydata_crontab ######## Parameters FLYDATA_HOME=~/".flydata" CRON_TMP_PATH="$FLYDATA_HOME/cron_list.tmp" CRON_BACKUP_PATH="$FLYDATA_HOME/cron_list.back" LOG_PATH="$FLYDATA_HOME/flydata_cron.log" ROUTINE_COMMAND="$FLYDATA_HOME/bin/flydata routine >> $LOG_PATH 2>&1" if [[ "$FLYDATA_API_HOST" != "" ]]; then ROUTINE_COMMAND="export FLYDATA_API_HOST=$FLYDATA_API_HOST && $ROUTINE_COMMAND" fi #CRON_LINE='15 4 * * *'" $ROUTINE_COMMAND" CRON_LINE='* * * * *'" $ROUTINE_COMMAND" ARG=$1 REMOVE_OPTION="-r" CRON_SETTING_DONE=0 # flag ######## Functions check_flydata_setup_done() { which flydata > /dev/null if [ "$?" -ne "0" ]; then echo "flydata command not found. Flydata setup has not been done, yet." exit -1 fi if [ ! -w $FLYDATA_HOME ]; then echo "Flydata setup has not been done, yet. Run setup command." echo " -> flydata setup" exit -1 fi } check_crontab_commnad() { which crontab > /dev/null if [ "$?" -ne "0" ]; then echo "No crontab command. Please install cron." exit -2 fi } check_crond_process() { crond_exist=`ps aux | grep crond | grep -v grep | wc -l` echo $crond_exist if [ "$crond_exist" -ne "1" ]; then echo "No crond process. Please start crond process." exit -3 fi } dump_cron_setting() { crontab -l > $CRON_BACKUP_PATH if [ "$?" -ne "0" ]; then echo "Cannot dump cron settings. " echo "Please install " exit -4 fi } check_cron_setting() { CRON_SETTING_DONE=`cat $CRON_BACKUP_PATH | grep "flydata routine" | wc -l` if [ "$?" -ne "0" ]; then echo "Failed to check cron setting file." exit -5 fi } add_cron_setting() { cat $CRON_BACKUP_PATH > $CRON_TMP_PATH echo "$CRON_LINE" >> $CRON_TMP_PATH crontab $CRON_TMP_PATH if [ "$?" -ne "0" ]; then echo "Failed to register flydata routine job." exit -6 fi } remove_cron_setting() { cat "$CRON_BACKUP_PATH" | grep -v "flydata routine" > $CRON_TMP_PATH crontab $CRON_TMP_PATH if [ "$?" -ne "0" ]; then echo "Failed to remove flydata routine job." exit -7 fi } ######## Main ARG="$1" check_flydata_setup_done check_crontab_commnad # check_crond_process dump_cron_setting check_cron_setting # Skip If add mode and registered if [[ "$ARG" != "$REMOVE_OPTION" && $CRON_SETTING_DONE -gt 0 ]]; then echo "Skip. Already cron setup done." exit 0 fi # Remove mode -> remove setting if [ "$ARG" == "$REMOVE_OPTION" ]; then remove_cron_setting exit 0 fi # Add mode -> add setting add_cron_setting echo "Updated crontab to manage the deletion of your log data." exit 0