Search code examples
linuxpostgresqlamazon-ec2cron

Crontab for Postgresql failing with command not found


I'm having some trouble with a cronjob I have set for Postgresql. It reports the error psql: command not found. Refer to the error log output below.

FYI, when I run the bash script manually by pasting /var/opt/tableau/tableau_server/TIL_scripts/FIREPROD-DWH_Daily_Extract.bash into my CLI and pressing return it starts and completes the job successfully on the remote RDS database and then add's a message to the log.

However, the crontab for the same script starts and then immediately fails. What am I missing here?

1) Error Log below

/var/opt/tableau/tableau_server/TIL_scripts/FIREPROD-DWH_Daily_Extract.bash: line 1: psql: command not found
/var/opt/tableau/tableau_server/TIL_scripts/FIREPROD-DWH_Daily_Extract.bash: line 2: psql: command not found

2) Contents of FIREPROD-DWH_Daily_Extract.bash file below:

export PGPASSWORD=FIREPRODW@r3h0u11; psql -h 'fireprod-pgsql-rds.testdomain.net' -U 'fireproddbwhadmin' -d 'fireprod-datawarehouse' -c 'select schedules_ldn_daily()'
export PGPASSWORD=FIREPRODW@r3h0u11; psql -h 'fireprod-pgsql-rds.testdomain.net' -U 'fireproddbwhadmin' -d 'fireprod-datawarehouse' -c 'select schedules_tv_daily()'

3) .bashrc file for root user below:

# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=

# User specific aliases and functions
export PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/tableau/tableau_server/packages/customer-bin.20201.20.0614.2321:/home/ec2-user/.local/bin:/home/ec2-user/bin:/var/opt/tableau/tableau_server/data/tabsvc/services/pgsql_0.20201.20.0614.2321/pgsql/bin

4) echo $PATH displays below:

[root@FIREPROD-02 ec2-user]# echo $PATH
/sbin:/bin:/usr/sbin:/usr/bin:/opt/tableau/tableau_server/packages/customer-bin.20201.20.0614.2321:/var/opt/tableau/tableau_server/data/tabsvc/services/pgsql_0.20201.20.0614.2321/pgsql/bin

5) Crontab configured per below:

00 2 * * * /var/opt/tableau/tableau_server/TIL_scripts/FIREPROD-DWH_Daily_Extract.bash >> /home/ec2-user/fireprod-dwh_daily_extract.log 2>&1
00 2 28-31 * * [ $(date -d tomorrow +\%d) -eq 1 ] && /var/opt/tableau/tableau_server/TIL_scripts/FIREPROD-DWH_Monthly_Extract.bash >> /home/ec2-user/fireprod-dwh_monthly_extract.log 2>&1

Solution

  • crontab has it's own PATH. If you want it to find psql you need to add PATH to top of crontab:

    PATH=/usr/local/bin, etc

    or give the full path to psql in your script.