LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 09-29-2010, 08:43 AM   #1
redvelo
LQ Newbie
 
Registered: Sep 2010
Posts: 7

Rep: Reputation: 0
Script runs manually but not from cron


Hello.

I know there's at least 4 other threads related to this here. I've read them over but I'm still stuck.

I have a simple shell script that pushes files from my server to Amazon S3 for backup, then writes the results to a file called 'scrap.'

The script runs perfectly when I run it manually as myself, but when I run it as a cron job - from either the system cron or my user cron, it runs fine and will create my scrap file but it doesn't write to it.

Here's my script

Code:
#!/bin/bash

ruby '/home/my_username/s3sync/s3sync.rb' -r -s -v --dryrun '/var/www/html/' my_S3_server:www > '/home/my_username/s3sync/scrap'
I've saved the script as an executable .sh file, and the file is owned by me (it was owned by root, but I chown-ed to me to see if that would work but it didn't).

Editing crontab from my home directory with crontab -e, my crontab looks like this:

Code:
# testing cron job of backup of vhosts to Amazon S3

20 9 29 9 3 '/full_path_to_my_script.sh'
I've tried everything I can think of, including:

- making my results file world writable
- adding my username to the crontab
- adding my username to a cron.allow file (cron.deny is empty)

Also, I tail -f the cron log when I run it so I know the job does run.

Is my crontab missing some environmental variables?

Thanks for any help. I'm very new at this.

-redvelo

Last edited by redvelo; 09-29-2010 at 08:45 AM.
 
Old 09-29-2010, 09:02 AM   #2
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 707

Rep: Reputation: 55
Hi redvelo,

I too have faced this issue before.

Lets include error stream into your output file

Code:
#!/bin/bash

ruby '/home/my_username/s3sync/s3sync.rb' -r -s -v --dryrun '/var/www/html/' my_S3_server:www > '/home/my_username/s3sync/scrap 2>&1'
It seems it requires some environmental variables which it is not getting through crontab. This will be the problem in most of the cases.

Just check, what can you see in the output file /home/my_username/s3sync/scrap after execution.

Warm Regards,
 
Old 09-29-2010, 10:07 AM   #3
redvelo
LQ Newbie
 
Registered: Sep 2010
Posts: 7

Original Poster
Rep: Reputation: 0
hi vinaytp,

I added the '2>&1' my script as you suggested:

Code:
ruby '/home/my_username/s3sync/s3sync.rb' -r -s -v --dryrun '/var/www/html/' my_S3_server:www > '/home/my_username/s3sync/scrap 2>&1'
This resulted in the script creating a file called 'scrap 2>&1' and it did have the results of my script. But again, this only worked manually. As a cronjob, it didn't even create the empty scrap file that it normally does.

Thanks.
 
Old 09-29-2010, 10:15 AM   #4
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 707

Rep: Reputation: 55
Hi redvelo,

It is because of single quotes.

Try this.

Code:
ruby /home/my_username/s3sync/s3sync.rb -r -s -v --dryrun '/var/www/html/' my_S3_server:www > /home/my_username/s3sync/scrap 2>&1
If you again encounter problem with this, try executing after removing sigle quotes which is given to /var/www/html.

Warm Regards,
 
Old 09-29-2010, 11:46 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
cron runs jobs with a different environment from the one set up when logging on.

Commonly, problems are caused by the PATH environment variable being different; the solution is either to set PATH explicitly in the script using export PATH <whatever it is after logging on> (which has the advantage of setting it for any child processes) or to give the full path for all commands, for example using /bin/ls instead of ls.

Alternatively you can change the first line of your script to #!/bin/bash -l (that's a letter l, meaning "logon") which has the advantage of setting all envars, functions and aliases but will cause breakage if any of the bash initialisation scripts read from stdin (for example, if they ask the user a question and read the answer).
 
Old 09-29-2010, 12:46 PM   #6
redvelo
LQ Newbie
 
Registered: Sep 2010
Posts: 7

Original Poster
Rep: Reputation: 0
vinaytp,

Ok, for some reason, trying to overwrite (>) the data in my scrap file wasn't working, so instead I just appended (>>) the data to an existing scrap file and now it's working as a cron job.

I'm not sure why '>' was only creating but not writing to the file. Would I have to specify that in my script? Something like:

Code:
ruby /home/my_username/s3sync/s3sync.rb -r -s -v --dryrun '/var/www/html/' my_S3_server:www | touch scrap > /home/my_username/s3sync/scrap
In any case, it's working now. Thanks for your help.

catkin,

Thanks for your help. I will try your three suggestions and see what works best for me.

Last edited by redvelo; 09-29-2010 at 12:49 PM.
 
Old 09-29-2010, 01:37 PM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by redvelo View Post
I'm not sure why '>' was only creating but not writing to the file. Would I have to specify that in my script? Something like:
Code:
ruby /home/my_username/s3sync/s3sync.rb -r -s -v --dryrun '/var/www/html/' my_S3_server:www | touch scrap > /home/my_username/s3sync/scrap
That sends the stdout output from the ruby ... command to touch (which does not read it) and the output from touch scrap (which is nothing) to /home/my_username/s3sync/scrap. The effect is to create /home/my_username/s3sync/scrap or, if it exists already, to empty it.

If you want to send the output from the ruby ... command to /home/my_username/s3sync/scrap, then use
Code:
ruby ... > 2>&1 /home/my_username/s3sync/scrap
The 2>&1 sends stderr output (stream 2) to the same place as stdout.

There's a shorter way to redirect stdout and stderr but I can't recall it and it's too late here to research it; just so you know I haven't given you the neatest solution.

EDIT: the code is wrong (it's late at the end of a long day). It should be
Code:
ruby ... > /home/my_username/s3sync/scrap 2>&1

Last edited by catkin; 09-29-2010 at 01:41 PM.
 
  


Reply


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
wget command runs manually but over cron doesnt ulver Linux - Newbie 3 12-24-2009 04:26 AM
bash script runs different when cron executed jalder85 Linux - Server 4 02-20-2009 11:53 AM
Script not running properly in cron though runs well if run manually. linuxlover.chaitanya Linux - Newbie 4 01-15-2009 03:31 AM
Cron Job only runs when it is manually started sinister1 Linux - General 4 02-25-2008 08:35 AM
Script works when run manually but not in cron job Harlin Linux - Software 7 10-16-2007 06:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:15 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration