LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   My BASH script work perfect when i run it manualy, but not when it run in the crontab (http://www.linuxquestions.org/questions/linux-newbie-8/my-bash-script-work-perfect-when-i-run-it-manualy-but-not-when-it-run-in-the-crontab-806115/)

roqarg 05-05-2010 11:40 AM

My BASH script work perfect when i run it manualy, but not when it run in the crontab
 
I am Cuban
Sorry by language bugs.

I am run an script bash manually an d it run perfectly.
I copy my script into the directory /etc/cron.daily and i programing the crontab to run everything into the cron.daily directory and the script run withow problems but the result it`s wrong.

To be more especific:
My script do a copy of a file; I run it manually and the file copied good with the same size. When the script run by crontab the file that i have copied has 0kb.

Any subjection?

catkin 05-05-2010 11:44 AM

The usual problem when scripts run at the command line but not from cron is that they rely on $PATH and cron sets a very basic $PATH.

Try setting $PATH in the script, for example
Code:

export PATH='/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin'

roqarg 05-05-2010 12:48 PM

the solution don`t works any other subjection?
 
Help me please!!!

roqarg 05-05-2010 01:15 PM

Your solution dont work my friend. do you have any other subjection.

sneakyimp 05-05-2010 01:30 PM

set up your crontab so that the output of your script goes into some text file. for instance, my backup cron entry looks like this:
Code:

30 4 * * * php /var/www/cron/data_backup.php > /var/www/cron/data_backup.log
if the cron job fails, i can check the contents of /var/www/cron/data_backup.log to find any error messages which may have been generated.

like catkin said, the biggest difference between running a script via cron versus command-line is the path. another possible issue is permissions. when you run something from command line, it executes as whatever user you are logged in as. the cron job could be a different user depending on how you set it up.

repo 05-05-2010 01:37 PM

Make sure to use the whole path to all the commands and files in the script.

catkin 05-05-2010 01:41 PM

Assuming it's a bash script, try changing the first line to
Code:

#!/bin/bash -l
That's a letter l for login. It will make bash simulate logon so you get your usual environment, similar to at the command line.

sneakyimp's suggestion is good and may give useful information. Try it with 2>&1 at the end of the line, too. That may catch some error messages that > some_file alone does not.

roqarg 05-05-2010 01:49 PM

Dont work
 
Every that you tell me is already ok, and the problem continue.
Is someone have any other solution.
I AM BECAME CRAZY!!!

roqarg 05-05-2010 02:33 PM

I found the problem
 
I found the possible problem:
I run a pg_dump on a PostgreSQL Data Base.
In that line is the problem.
I probed the line in a SHELL manually and work ok but not in the script.
Any Subjections???
Please I need Help!!!

catkin 05-05-2010 02:41 PM

Are you getting anything in the output file named after > in the crontab?

If so then you can add lines like
Code:

echo "DEBUG $LINENO"
to the script and see how far it is getting.

catkin 05-05-2010 02:43 PM

Quote:

Originally Posted by roqarg (Post 3958385)
I found the possible problem:
I run a pg_dump on a PostgreSQL Data Base.
In that line is the problem.
I probed the line in a SHELL manually and work ok but not in the script.

Would you like to post the line so we can see it?

sneakyimp 05-05-2010 03:04 PM

I'm wondering if he gets prompted for a password when running via CLI. the cron user may not have permission to dump the db.

roqarg 05-05-2010 03:05 PM

This are the lines that are wrong
 
1- /usr/bin/pg_dump --host $hostName --port $port --username $userName --format tar --compress $compressLevel --file "/tmp/OBDBdump-$today-$now.db" openbravo

2- /usr/bin/pg_dump -i -h $hostName -p $port -U $userName -F c -b -v -f "/tmp/OBDBdump-$today-$now.db" openbravo

both work ok manually, but not in the crontab

help?

roqarg 05-05-2010 03:39 PM

Is anyone there?
Please Help Me!!!

sneakyimp 05-05-2010 03:57 PM

Did you try outputting the result of those lines to a file? If there are any errors, they would be written to the file so you could see the problem.

Also, you have not showed us your crontab. Is this crontab created for the same user that you login as when you run the commands from the command line?


All times are GMT -5. The time now is 01:13 PM.