1

I am new in writing shell script i write a script like this

#!/bin/bash
PID=$(ps -ef | grep "sh gps.sh" | grep -v grep | awk '{print $2}')
if [ -z $PID ]
then
    cd /opt/etrans/cronjobs
    cnt=$(echo "select 'count '||count(*) from schema.table where c_is_processed ='N'" | psql | grep count |awk '{print $2}')
    echo "Out side of if count is $cnt" | cat >> /opt/cronlog/unprocess_data_process.log # for log writing
    if [ $cnt -ge 5000 ]
            then
            echo "Inside manual gps" | cat >> /opt/cronlog/unprocess_data_process.log # for log writing
            sh manual_gps.sh
        else
        echo "Inside gps" | cat >> /opt/cronlog/unprocess_data_process.log # for log writing
                sh gps.sh
        fi
else
 echo "gps Process is Running with PID=$PID"
fi
exit

when I run the .sh file manually it runs file and I can see all the counts in my log file but when I run it from crontab it is not printing the counts.

txwikinger
  • 28,462
smn_onrocks
  • 526
  • 5
  • 14

1 Answers1

0

I suppose the reason is your cron environment. Maybe it does not know where the psql command is.

To investigate the problem try to add another script to your cron, with this command:

#/bin/bash

which psql >> /tmp/investigation

Add this script to cron so that it will be executed as soon as possible. I believe in /tmp/investigation you will see that shell cannot found psql command. To provide shell with psql you have to add it to PATH variable exactly before command execution.

Also read this wiki question

c0rp
  • 9,820
  • 3
  • 38
  • 60
  • 1
    Hi friends i have solved it by bringing the environment into my class path and source that environment its look like #!/bin/bash export PATH=$PATH:$HOME/bin export PGPORT=5432 export PGDATABASE=database_name – smn_onrocks Jan 16 '14 at 10:49