LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 05-05-2010, 11:40 AM   #1
roqarg
LQ Newbie
 
Registered: May 2010
Posts: 14

Rep: Reputation: 0
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?
 
Old 05-05-2010, 11:44 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,531
Blog Entries: 27

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
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'
 
0 members found this post helpful.
Old 05-05-2010, 12:48 PM   #3
roqarg
LQ Newbie
 
Registered: May 2010
Posts: 14

Original Poster
Rep: Reputation: 0
the solution don`t works any other subjection?

Help me please!!!
 
Old 05-05-2010, 01:15 PM   #4
roqarg
LQ Newbie
 
Registered: May 2010
Posts: 14

Original Poster
Rep: Reputation: 0
Your solution dont work my friend. do you have any other subjection.
 
Old 05-05-2010, 01:30 PM   #5
sneakyimp
Member
 
Registered: Dec 2004
Posts: 791

Rep: Reputation: 49
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.
 
Old 05-05-2010, 01:37 PM   #6
repo
LQ 5k Club
 
Registered: May 2001
Location: Belgium
Distribution: Linux Mint
Posts: 8,496

Rep: Reputation: 883Reputation: 883Reputation: 883Reputation: 883Reputation: 883Reputation: 883Reputation: 883
Make sure to use the whole path to all the commands and files in the script.
 
Old 05-05-2010, 01:41 PM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,531
Blog Entries: 27

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
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.
 
Old 05-05-2010, 01:49 PM   #8
roqarg
LQ Newbie
 
Registered: May 2010
Posts: 14

Original Poster
Rep: Reputation: 0
Dont work

Every that you tell me is already ok, and the problem continue.
Is someone have any other solution.
I AM BECAME CRAZY!!!
 
Old 05-05-2010, 02:33 PM   #9
roqarg
LQ Newbie
 
Registered: May 2010
Posts: 14

Original Poster
Rep: Reputation: 0
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!!!
 
Old 05-05-2010, 02:41 PM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,531
Blog Entries: 27

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
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.
 
Old 05-05-2010, 02:43 PM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,531
Blog Entries: 27

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by roqarg View Post
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?
 
Old 05-05-2010, 03:04 PM   #12
sneakyimp
Member
 
Registered: Dec 2004
Posts: 791

Rep: Reputation: 49
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.
 
Old 05-05-2010, 03:05 PM   #13
roqarg
LQ Newbie
 
Registered: May 2010
Posts: 14

Original Poster
Rep: Reputation: 0
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?
 
Old 05-05-2010, 03:39 PM   #14
roqarg
LQ Newbie
 
Registered: May 2010
Posts: 14

Original Poster
Rep: Reputation: 0
Is anyone there?
Please Help Me!!!
 
Old 05-05-2010, 03:57 PM   #15
sneakyimp
Member
 
Registered: Dec 2004
Posts: 791

Rep: Reputation: 49
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?
 
  


Reply

Tags
do, dont, friend, solution


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Script won't run in crontab rastellig Linux - Software 2 03-25-2009 09:18 AM
How to run this script in Crontab baig Linux - Newbie 2 11-30-2008 02:34 PM
how to run crontab in shell script panselva Linux - General 3 05-08-2008 08:33 PM
Crontab -e doesnt run my script ! c0nsur Linux - General 4 08-03-2005 11:21 PM
Script doesn't run in crontab dani81 Linux - General 1 11-16-2004 03:42 AM


All times are GMT -5. The time now is 06:22 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration