Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

PID=$(ps -ef | grep "sh" | grep -v grep | awk '{print $2}')
if [ -z $PID ]
    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 ]
            echo "Inside manual gps" | cat >> /opt/cronlog/unprocess_data_process.log # for log writing
        echo "Inside gps" | cat >> /opt/cronlog/unprocess_data_process.log # for log writing
 echo "gps Process is Running with PID=$PID"

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.

share|improve this question
How did you install the cronjob? – i08in Jan 16 '14 at 8:47
possible duplicate of Reasons why crontab does not work – c0rp Jan 16 '14 at 8:51
up vote 0 down vote accepted

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:


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

share|improve this answer
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.