I have the psql path in my path variable. crontab path is usr:/usr/bin:/bin
I add all the user directories and then execute the cron job.
Default PATH for usr:/usr/bin:/bin
user is:gpadmin
PATH for usr:/usr/bin:/bin:/usr/local/greenplum-db/./bin:/usr/local/greenplum-db/./ext/python/bin:/home/gpadmin/anaconda3/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/home/gpadmin/bin
My script is as below (executed by crontab):
#!/bin/bash
# Usage : bash RR_load.sh
echo "Loading the data into datalake"
now="$(date +'%d-%m-%Y')"
basedir="/home/2125/RR_Dev_Code"
#echo "load started at $now"
#logname=$0"_"$now".log"
ts="$(date +'%d-%m-%Y_%H:%M:%S')"
echo "load started at $ts"
echo `who am i`
logname=$0"_"$ts".log"
echo "load started at $ts" > $logname
chmod 777 $logname
nohup python $basedir/copy_csv_to_data_lake_v3.py $basedir >> $logname
echo "process id $!" >> $logname
echo "Recreate the table using loader.sh"
echo "Default PATH for usr:"$PATH >> $logname
export PATH=$PATH:/usr/local/greenplum-db/./bin:/usr/local/greenplum-db/./ext/python/bin:/home/gpadmin/anaconda3/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/home/gpadmin/bin
source /etc/profile
psql -h '3.48.35.24' -U 'tan12345' 'sourcing' << EOF
Select current_timestamp(0);
EOF
On getting executed as per schedule, I see a message in /var/spool/mail/gpadmin
as below:
Loading the data into datalake
load started at 06-04-2017_12:19:01
dos2unix: converting file /home/2125/RR_Dev_Code/sandbox-rr_data.2017-03-29.csv to UNIX format ...
Recreate the table using loader.sh
psql: error while loading shared libraries: libpq.so.5: cannot open shared object file: No such file or directory
Added the .bash_profile before the command in crontab.
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
# test entry
#* * * * * echo `whoami` > /home/2125/RR_Dev_Code/ETL/crontab.test
59 13 * * 4 . $HOME/.bash_profile;/home/2125/RR_Dev_Code/RR_load_v3.sh
Thanks for the answer posted here : https://unix.stackexchange.com/questions/27289/how-can-i-run-a-cron-command-with-existing-environmental-variables.
Such a relief to see all the hard-work running automatically now.