LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 12-11-2008, 11:46 AM   #1
hwest
LQ Newbie
 
Registered: Dec 2008
Distribution: RHEL 5
Posts: 5

Rep: Reputation: 0
Script runs fine from command line, but not when attempted from Cron


Hello,

I am using RHEL 5. I have a script file that does what I want it to, when I run it as root from the command line. However, when I call the script from a cron job and run it as root from there, it will not write any files. Following is the contents of my script file:

touch /test
alias rm='rm'
rm /mnt/exthd/image/mystuffbkp/img_bkp.gz
tar czvf /mnt/exthd/image/mystuffbkp/img_bkp.gz /home/img
alias rm='rm -i'


My intent is to have it delete the current img_bkp.gz file and then tar and zip the contents of /home/img and write the img_bkp.gz file to the /mnt/exthd/image/mystuffbkp directory. I added touch /test to the script just to see if it could even just write a file to the root directory (and it does not).

To set up the cron job, I created an /etc/cron.imgbkp directory and I put the above script, called imgbkp, into this directory. The permissions on the directory are 755 and the permissions on the scriptfile are 755. Next, I edited /etc/crontab, using Vi, and I added the following entry to that file:

20 20 * * 1-6 root run-parts /etc/cron.imgbkp.

Finally, I typed in the following to restart the cron daemon: service crond restart (and it shows to be running). When I didn't see my files getting created, I checked /var/log/cron and it shows my job starting at 8:20pm, but I don't see that it shows any errors.

The permissions on the /mnt/exthd/image/mystuffbkp directory are 777, at this point.

Anyone have any idea what I've missed or what else to look for?

Thanks.
 
Old 12-11-2008, 12:13 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by hwest View Post
Hello,

I am using RHEL 5. I have a script file that does what I want it to, when I run it as root from the command line. However, when I call the script from a cron job and run it as root from there, it will not write any files. Following is the contents of my script file:

touch /test
alias rm='rm'
rm /mnt/exthd/image/mystuffbkp/img_bkp.gz
tar czvf /mnt/exthd/image/mystuffbkp/img_bkp.gz /home/img
alias rm='rm -i'


My intent is to have it delete the current img_bkp.gz file and then tar and zip the contents of /home/img and write the img_bkp.gz file to the /mnt/exthd/image/mystuffbkp directory. I added touch /test to the script just to see if it could even just write a file to the root directory (and it does not).

To set up the cron job, I created an /etc/cron.imgbkp directory and I put the above script, called imgbkp, into this directory. The permissions on the directory are 755 and the permissions on the scriptfile are 755. Next, I edited /etc/crontab, using Vi, and I added the following entry to that file:

20 20 * * 1-6 root run-parts /etc/cron.imgbkp.

Finally, I typed in the following to restart the cron daemon: service crond restart (and it shows to be running). When I didn't see my files getting created, I checked /var/log/cron and it shows my job starting at 8:20pm, but I don't see that it shows any errors.
Where to begin, here?? First, you need to look at the man page for crontab ("man crontab"), and use it to edit CRON.

Second, directly editing crontab is never a good idea...especially when the format for the entry you entered was wrong. Also, the 'program' you've put in there to run (the last parameter), is a directory, not a script file.

As root, type in "export EDITOR=vi", then type in "crontab -e". That will open up the root CRON entry. From there, enter "20 20 * * 1-6 <full path and name of your script file>". Exit VI. That's it. You don't create directories, and I'm not sure, but I highly doubt that crontab is in /etc/ to start with....
 
Old 12-11-2008, 12:19 PM   #3
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,337

Rep: Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548
The problem is probably the cron PATH. cron usually does not have a decent PATH and so programs fail to execute because they can't be found. You can solve this problem by giving the full path name for every command that you execute in cron. For example:

/sbin/touch /test

Another way to solve the problem is to create a PATH as the first command in cron.

For example:

PATH=$PATH:/sbin

If you need a good example of what files to include in your PATH log in as root and use the command:

echo $PATH

------------------------
Steve Stites
 
Old 12-11-2008, 12:30 PM   #4
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
Cron is also a system user. If you want cron to do something, and you're editing by hand, you need to make sure the proper permissions are granted.
 
Old 12-11-2008, 02:10 PM   #5
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
General rules of thumb when creating cron scripts:
  1. Ensure you stipulate which shell to use
  2. Use full paths for commands
  3. make a backup of the crontab file first (using crontab -l or sudo crontab -l)
  4. Ensure you use the correct crontab format

Minute(0-59) Hour(0-23) DayOfMonth(1-31) Month(1-12) DayOfWeek(0-7) Command

I believe your code should be more like:
Code:
#!/bin/bash
##
## Script: /usr/local/my_backup
##

/usr/bin/touch /test
alias rm='/bin/rm'
rm /mnt/exthd/image/mystuffbkp/img_bkp.gz
/bin/tar czvf /mnt/exthd/image/mystuffbkp/img_bkp.gz /home/img
Then your cron entry (in root's cron file) should be something like:
Code:
20 20 * * 1-6 /usr/local/my_backup
EDIT:-

Ensure you have safe permissions on /usr/local/my_backup

chown root /usr/local/my_backup
chmod 700 /usr/local/my_backup

This script will be run by the root user, therefore it is important to protect the file from tampering (as much as possible on an unencrypted filesystem).

Last edited by Disillusionist; 12-11-2008 at 02:20 PM.
 
Old 12-11-2008, 02:28 PM   #6
hwest
LQ Newbie
 
Registered: Dec 2008
Distribution: RHEL 5
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by TB0ne View Post
Where to begin, here?? First, you need to look at the man page for crontab ("man crontab"), and use it to edit CRON.

Second, directly editing crontab is never a good idea...especially when the format for the entry you entered was wrong. Also, the 'program' you've put in there to run (the last parameter), is a directory, not a script file.

As root, type in "export EDITOR=vi", then type in "crontab -e". That will open up the root CRON entry. From there, enter "20 20 * * 1-6 <full path and name of your script file>". Exit VI. That's it. You don't create directories, and I'm not sure, but I highly doubt that crontab is in /etc/ to start with....
Thanks for your response. Before posting the first time, I read the man page for cron (always a good suggestion). Maybe I wasn't clear enough on what I was doing or that this is RHEL 5. I guess a bunch of this may be Redhat specific or something (not really sure since I haven't yet played around with other distros much), but when I started messing with cron, I read an article at http://docs.moodle.org/en/RedHat_Linux_installation and it actually does suggest modifying the Redhat /etc/crontab file with Vi and that is why I went that route. I have done it that way plenty in the past and have never had a problem. If I want to mess with personal crontabs, then I would use the crontab -e method. Trying that with Vi might cause problems. I am doing this with the root account and I don't want to modify the root user's personal crontab. Here is what the article from the url, above, says:

Set up the cron job
As root user edit the /etc/crontab file using ***vi*** (or another editor) OR you can add a line to the root user's "personal" crontab (don't do both!).

Second, as stated in my original post, I am using run-parts. I guess in Redhat run-parts causes all of the individual scripts in a specified directory to be run, so the correct format actually does need to include a directory and not a specific filename. So, the format 20 20 * * 1-6 <full path to the directory, like /etc/cron. and not a file> is correct and to make this work you do need to create a directory (One thing I noticed is that in my post I had a period at the end of that line, but in my real /etc/crontab, I don't, so that can be ignored). I wanted to use run-parts because there may be other things I want to test or run as part of this cron schedule and then all I'd have to do is drop the script in the directory.

Rest assured that in Redhat, at least, crontab has been in /etc since at least Redhat 7. Thanks again for your efforts. I'm still pretty puzzled about this one.
 
Old 12-11-2008, 02:40 PM   #7
hwest
LQ Newbie
 
Registered: Dec 2008
Distribution: RHEL 5
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by AwesomeMachine View Post
Cron is also a system user. If you want cron to do something, and you're editing by hand, you need to make sure the proper permissions are granted.
Thanks and good point. For this one, I was logged in as root.
 
Old 12-11-2008, 02:53 PM   #8
hwest
LQ Newbie
 
Registered: Dec 2008
Distribution: RHEL 5
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Disillusionist View Post
General rules of thumb when creating cron scripts:
  1. Ensure you stipulate which shell to use
  2. Use full paths for commands
  3. make a backup of the crontab file first (using crontab -l or sudo crontab -l)
  4. Ensure you use the correct crontab format

Minute(0-59) Hour(0-23) DayOfMonth(1-31) Month(1-12) DayOfWeek(0-7) Command

I believe your code should be more like:
Code:
#!/bin/bash
##
## Script: /usr/local/my_backup
##

/usr/bin/touch /test
alias rm='/bin/rm'
rm /mnt/exthd/image/mystuffbkp/img_bkp.gz
/bin/tar czvf /mnt/exthd/image/mystuffbkp/img_bkp.gz /home/img
Then your cron entry (in root's cron file) should be something like:
Code:
20 20 * * 1-6 /usr/local/my_backup
EDIT:-

Ensure you have safe permissions on /usr/local/my_backup

chown root /usr/local/my_backup
chmod 700 /usr/local/my_backup

This script will be run by the root user, therefore it is important to protect the file from tampering (as much as possible on an unencrypted filesystem).
Thanks...good points and I have copied them down for future reference. I am wanting to use the /etc/crontab file and to use run-parts so I can specify that all the files in the directory should be run. Since this file already exists and has entries (and I was able to successfully do this last in Fedora 3), I thought I should just be able to create a directory, make sure the permissions were good, throw in the scriptfile (and make sure the permissions were good (and thanks for your suggestion on locking that down more)), and modify the /etc/crontab file like I did before. /var/logs/cron shows the cronjob I created runs, but I never get any files written anywhere and I have made sure to specify the full paths and I also checked the $PATH variable and that appears to have already had the paths to all the executables in it, anyway. Any other ideas (and again thanks for the suggestions you provided).
 
Old 12-11-2008, 06:48 PM   #9
hwest
LQ Newbie
 
Registered: Dec 2008
Distribution: RHEL 5
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by hwest View Post
Hello,

I am using RHEL 5. I have a script file that does what I want it to, when I run it as root from the command line. However, when I call the script from a cron job and run it as root from there, it will not write any files. Following is the contents of my script file:

touch /test
alias rm='rm'
rm /mnt/exthd/image/mystuffbkp/img_bkp.gz
tar czvf /mnt/exthd/image/mystuffbkp/img_bkp.gz /home/img
alias rm='rm -i'


My intent is to have it delete the current img_bkp.gz file and then tar and zip the contents of /home/img and write the img_bkp.gz file to the /mnt/exthd/image/mystuffbkp directory. I added touch /test to the script just to see if it could even just write a file to the root directory (and it does not).

To set up the cron job, I created an /etc/cron.imgbkp directory and I put the above script, called imgbkp, into this directory. The permissions on the directory are 755 and the permissions on the scriptfile are 755. Next, I edited /etc/crontab, using Vi, and I added the following entry to that file:

20 20 * * 1-6 root run-parts /etc/cron.imgbkp.

Finally, I typed in the following to restart the cron daemon: service crond restart (and it shows to be running). When I didn't see my files getting created, I checked /var/log/cron and it shows my job starting at 8:20pm, but I don't see that it shows any errors.

The permissions on the /mnt/exthd/image/mystuffbkp directory are 777, at this point.

Anyone have any idea what I've missed or what else to look for?

Thanks.
Since this was running fine on a prevous distribution (Fedora 3), I figured it was going to be something dumb and it was--me! I did run the script from the command line and it worked fine. I then moved the directory that was to hold the .gz file and I changed the script to reflect this change. I had forgotten I had tested the script file only before this change and not after, when I posted here. The issue was that I had an embedded character in the script file. I simply recreated the script file and everything works great, now. Thanks to all for replying.
 
  


Reply



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
PHP script through cron not working fine. vikasumit Linux - General 5 09-04-2008 02:22 PM
script doesn't run in cron (runs from shell just fine) Timur Sakayev Linux - Software 6 02-26-2007 12:56 PM
Shell: command not found / Runs fine with "Run command" badbunny Linux - Newbie 1 01-22-2007 01:21 AM
cron issue, php script runs fine in browser and cli dtra Linux - Software 1 07-15-2005 05:45 AM
script runs fine from a command line, but doesn't work from cron? kleptophobiac Linux - Software 5 05-03-2004 04:14 PM

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

All times are GMT -5. The time now is 04:03 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