Script runs fine from command line, but not when attempted from Cron
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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....
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:
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.
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.
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).
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.