0

I am using the following shell script to fetch data from the database,and in these fetched data I have a field named 'frequency', then I need to set the script to run according to this frequency.Can I handle this functionality in the script so that it will tell the crone job to run it frequently?

#! /bin/bash
######## Settings ########
BASE_DIR_PATH='/var/www/html/aer/'
######## End Settings ########
CUS_PHP_FILE_PATH='shd/save_customer_info.php'
EMAIL_PHP_FILE_PATH='shd/email_customer_report.php'
CUSTOMER_REPORT_STORAGE_DIRECTORY='ev/customer_report/'
COMPLETED_CUSTOMER_REPORT_STORAGE_DIRECTORY='ev/rty'
LOG_DIRECTORY='ev/customer_report/logs'
DB_PHP_FILE='db/db_config.php'

#Obtain FTP server credentials from database
DB_FILE_NAME="$BASE_DIR_PATH$DB_PHP_FILE"

DB_NAME=`cat $DB_FILE_NAME | grep DB_DATABASE | cut -d \' -f 4`
DB_USERNAME=`cat $DB_FILE_NAME | grep DB_SERVER_USERNAME | cut -d \' -f 
4`
DB_PASSWORD=`cat $DB_FILE_NAME | grep DB_SERVER_PASSWORD | cut -d \' -f 
4`

FTP_PROFILE='bgty'
SYSTEM_PACKAGE='bfgtt' 
results=($(mysql --user $DB_USERNAME -p${DB_PASSWORD} ${DB_NAME} -Bse 
"SELECT api_url,api_user_name,api_password,frequency FROM 
brt_profile WHERE profile_name='$FTP_PROFILE' AND 
distributor='$SYSTEM_PACKAGE'"))

if [ $? -ne 0 ]; #check if database connection is failed
   then
   exit 
fi
  • You can set this script to run periodically using a cron job, and in this script, you can check the frequency and using that value you can judge whether at the current time you can run this script or not. If it doesn't satisfy your frequency skip the execution of logic. Like, if the frequency is 24, and as we have 24 hours in a day, the cron becomes an hourly cron job, then check the time while running the script, if it matches the time like 12:00, 01:00, and 02:00, then execute the code and skip otherwise. – Shubham Vaishnav Aug 28 '19 at 10:27
  • Can't you use a systemd drop-in timer service? – KamilCuk Aug 28 '19 at 10:31
  • 1
    [Uppercase variable names](/questions/673055/correct-bash-and-shell-script-variable-capitalization), [useless `cat`s](/questions/11710552/useless-use-of-cat), and [useless comparison of `$?`](/questions/36313216/why-is-testing-to-see-if-a-command-succeeded-or-not-an-anti-pattern) are antipatterns you should probably avoid. Probably try http://shellcheck.net/ before asking for human review. – tripleee Aug 28 '19 at 10:37
  • @Shubham Vaishnav can you give me your method in a code example? – Chameera Nuwan Aug 28 '19 at 10:57

0 Answers0