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 want to run my script in crontab, but I have some problems...

In my script I run this command:

db2 connect to myDB2

This works fine when I run this as db2inst1 user, but when I run this script in db2inst1 crontab, I get this:

db2: command not found.

How can I solve this?

share|improve this question
up vote 15 down vote accepted

To avoid having to type the absolute path to a command, shells introduced the $PATH environment variable, each directory is separated by a : and searches are done from left to right. cron often clears the whole environment, including this $PATH variable. Therefore, the script may behave differently in your cron compared to the behavior in the shell.

Use absolute paths

Run which db2 as db2inst1 user to get the full path for the db2 program. If the output is /usr/bin/db2, your cron command would look like:

/usr/bin/db2 connect to myDB2

Set the PATH variable

Run echo "$PATH" as db2inst1 user to get the $PATH variable and make sure this variable is available in your cron script too. For example, if the output was /usr/local/bin:/usr/bin:/bin, you would put the next line in the top of your shell script:

export PATH="/usr/local/bin:/usr/bin:/bin"
share|improve this answer

You must define what db2 and myDB2 are, otherwise use the absolute location of 'file' or 'application' which want to run.


/bin/rm /home/ubuntu/a.txt
share|improve this answer

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.