Troubles running DB2 command in shell script via cron

Hi there,

Now I'm facing error regarding running shell script via cron.

The shell script which is required to get value from database.
Below is the main part of shell script.

#/bin/bash

#connect to database(1)
db2 connect to $database user xxxx using yyyy

#set values from database(2)
status=`db2 -x select status from TABLE_X where end_date = select max(end_date) from TABLE_X`

#rest of the process(3)
if [ ${status} -eq 2 ]; then
  echo 'SUCCESS'
  process to next step

elif [ ${status} -eq 1 ]; then
  'WARNING'
  exit 2

else
  echo 'FATAL'
  exit 99

fi

process (1) success, but process (2) doesn't work when run this script via cron, so process (3) always exit with FATAL.

It works properly when run this manually.

I try some solutions:

  • edit crontab settings

(before)

*/10 * * * * /home/aaa/bbb/PPAP.sh

(after)

*/10 * * * * /bin/bash -l /home/aaa/bbb/PPAP.sh
  • run db2profile in shell script

I reffered the relate topic and try that, but error still exists...:frowning:

ttp://dba.stackexchange.com/questions/91807/online-backup-is-not-performed-from-the-cron-job-on-linux-server-manually-it-wo

Somebody has idea??

Thank you in advance.

Sounds like this has been solved before.
Look into the links at the bottom of this page.

I checked the link, and fix my script. It works properly now. Thank you for noticing:)

By the way, why

status=`db2 -x select status from TABLE_X where end_date = select max(end_date) from TABLE_X`

this script above doesn't work when executed by cron?

It doesn't work because cron doesn't know how to set up your environment like your shell does. When you log in, your shell initializes your environment with settings that allow your database commands to be found without specifying absolute pathnames for the database utilities and includes settings specifying what database to use and what access codes are needed to access that database. You have to make the script you run with cron make that same environment information available to the shell that is being run by cron .