LinuxQuestions.org
Review your favorite Linux distribution.
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-16-2008, 10:07 PM   #1
Arty Ziff
Member
 
Registered: May 2008
Location: Tacoma, WA
Distribution: CentOS and RHEL
Posts: 102

Rep: Reputation: 15
Script does not execute with cron


I've created the following shell script to rotate a log file (yes, I know about logrotate)
Code:
#!/bin/bash

mv /var/spool/qmailscan/qmail-queue.backup-3.tar.gz /var/spool/qmailscan/qmail-queue.backup-4.tar.gz
mv /var/spool/qmailscan/qmail-queue.backup-2.tar.gz /var/spool/qmailscan/qmail-queue.backup-3.tar.gz
mv /var/spool/qmailscan/qmail-queue.backup-1.tar.gz /var/spool/qmailscan/qmail-queue.backup-2.tar.gz
mv /var/spool/qmailscan/qmail-queue.backup.tar.gz /var/spool/qmailscan/qmail-queue.backup-1.tar.gz

LOGFILE=/var/spool/qmailscan/qmail-queue.log

tar -cvzf /var/spool/qmailscan/qmail-queue.backup.tar.gz $LOGFILE
echo "" > $LOGFILE
And I have this cron job
Code:
30 */6 * * * /var/spool/qmailscan/qmail-queue-log-rotate.sh 2> /dev/null
This should execute the script 30 minutes past every 6 hours (0030, 0630, 1230, 1830).

But while I can execute the script from the command line, if left to cron nothing seems to happen.

Any ideas?

Also...

When I do execute it from the command line, I get this:
Quote:
./qmail-queue-rotate.sh
tar: Removing leading `/' from member names
var/spool/qmailscan/qmail-queue.log
What does "Removing leading `/' from member names" mean?
 
Old 05-16-2008, 10:20 PM   #2
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 5&6 CentOS 5, 6 & 7
Posts: 572

Rep: Reputation: 56
Do you see anything in /var/log/cron? What about mail for the user running it from cron? Are you using root to run it?

Removing leading "/" from the tar archive converts the archive from an "absolute" archive that you can only restore to the place it came from, to a "relative" archive which means you can restore the archive to somewhere other than where it was extracted from.
 
Old 05-16-2008, 10:56 PM   #3
Arty Ziff
Member
 
Registered: May 2008
Location: Tacoma, WA
Distribution: CentOS and RHEL
Posts: 102

Original Poster
Rep: Reputation: 15
Yes, it looks like cron is running it:
Code:
May 12 00:30:00 localhost CROND[19424]: (root) CMD (/var/spool/qmailscan/qmail-queue-log-rotate.sh 2> /dev/null)
And, LogWatch emails me a report that says this as well.

But, the files don't get rotated. Works fine when I run it from the command line (as root).

Do I need to take care of some permissions issue? The script is owned by root.

Also, I changed the paths from full to relative, and the other message I asked about went away. This is now the output when executing it from the command line:
Quote:
[root@localhost qmailscan]# ./qmail-queue-rotate.sh
./qmail-queue.log

Last edited by Arty Ziff; 05-16-2008 at 10:59 PM.
 
Old 05-17-2008, 12:01 AM   #4
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 5&6 CentOS 5, 6 & 7
Posts: 572

Rep: Reputation: 56
It could be an environment issue, so try /bin/mv instead of just mv in your script.

Also, redirect all output, not just stderr, to a file so you can see the errors generated by the script. This will help work out why it isn't working.
 
Old 05-21-2008, 01:26 AM   #5
Arty Ziff
Member
 
Registered: May 2008
Location: Tacoma, WA
Distribution: CentOS and RHEL
Posts: 102

Original Poster
Rep: Reputation: 15
I changed the mv command to /bin/mv as suggested. I also changed the cron job to output the errors to a text file...
Quote:
30 * * * * /var/spool/qmailscan/qmail-queue-log-rotate.sh 2> /var/spool/qmailscan/error.txt
Here's the content of error.txt
Quote:
-bash: ./var/spool/qmailscan/qmail-queue-log-rotate.sh: No such file or directory
However, I can assure you the file is there...
Quote:
[root@localhost qmailscan]# ls -Al

...

-rwxr--r-- 1 root root 529 May 16 23:32 qmail-queue-log-rotate.sh
 
Old 05-21-2008, 01:31 AM   #6
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 5&6 CentOS 5, 6 & 7
Posts: 572

Rep: Reputation: 56
OK, some more debugging. Add "set -x" to line 2 of your script and send the error.txt file again.

Also, change your cron entry to this:-
Code:
30 * * * * /var/spool/qmailscan/qmail-queue-log-rotate.sh > /var/spool/qmailscan/error.txt 2>&1
Let me know if you need to understand the subtle change.
 
Old 05-21-2008, 01:36 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Well, a few things occur to me, which may not directly solve your problem, but are worth considering.

1. Always use the full path to any cmd eg mv, tar, etc in cron, as its default PATH is minimal to put it mildly.

2. tar is for collecting a bunch of files into one (tar cvf) and optionally gzipping (z) or bzip2 (j) to save space.
In your case, only one file is affected, so just use gzip/bzip2, forget tar.

3. trap both the stdout AND stderr:
/var/spool/qmailscan/qmail-queue-log-rotate.sh > /var/spool/qmailscan/qmail-queue-log-rotate.log 2>&1

4. use
set -xv
at the top of the shell script to get verbose output and expanded cmds: http://tldp.org/LDP/abs/html/options.html
 
Old 05-21-2008, 02:11 AM   #8
Arty Ziff
Member
 
Registered: May 2008
Location: Tacoma, WA
Distribution: CentOS and RHEL
Posts: 102

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by chrism01 View Post
3. trap both the stdout AND stderr:
/var/spool/qmailscan/qmail-queue-log-rotate.sh > /var/spool/qmailscan/qmail-queue-log-rotate.log 2>&1
Could you explain what is occuring in this line?

Also set -xv didn't change the error message.

But again, running the script at the command prompt works just fine. Why would there be a problem executing it via cron?
 
Old 05-21-2008, 02:24 AM   #9
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 5&6 CentOS 5, 6 & 7
Posts: 572

Rep: Reputation: 56
1. "set -xv" tells bash to output each command as it processes it - a bit like "@echo on" in DOS land.
2. The altered redirection commands say:-
2.1 Output all standard output to your error.txt file.
2.2 The "2>&1" means for standard error, send it to the same place standard output is going. So all output, not just error output, will now go to your error.txt file.

The content of error.txt should now show us each command the script executes (courtesy of the set -xv setting) and all of the errors generated by those commands.
 
Old 05-21-2008, 03:06 AM   #10
Arty Ziff
Member
 
Registered: May 2008
Location: Tacoma, WA
Distribution: CentOS and RHEL
Posts: 102

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by blacky_5251 View Post
The content of error.txt should now show us each command the script executes (courtesy of the set -xv setting) and all of the errors generated by those commands.
This is what it reveals...
Quote:
/bin/mv /var/spool/qmailscan/qmail-queue-backup-3.tar.gz /var/spool/qmailscan/qmail-queue-backup-4.tar.gz
+ /bin/mv /var/spool/qmailscan/qmail-queue-backup-3.tar.gz /var/spool/qmailscan/qmail-queue-backup-4.tar.gz
/bin/mv /var/spool/qmailscan/qmail-queue-backup-2.tar.gz /var/spool/qmailscan/qmail-queue-backup-3.tar.gz
+ /bin/mv /var/spool/qmailscan/qmail-queue-backup-2.tar.gz /var/spool/qmailscan/qmail-queue-backup-3.tar.gz
/bin/mv /var/spool/qmailscan/qmail-queue-backup-1.tar.gz /var/spool/qmailscan/qmail-queue-backup-2.tar.gz
+ /bin/mv /var/spool/qmailscan/qmail-queue-backup-1.tar.gz /var/spool/qmailscan/qmail-queue-backup-2.tar.gz
/bin/mv /var/spool/qmailscan/qmail-queue-backup.tar.gz /var/spool/qmailscan/qmail-queue-backup-1.tar.gz
+ /bin/mv /var/spool/qmailscan/qmail-queue-backup.tar.gz /var/spool/qmailscan/qmail-queue-backup-1.tar.gz

LOGFILE=/var/spool/qmailscan/qmail-queue.log
+ LOGFILE=/var/spool/qmailscan/qmail-queue.log

tar -cvzf /var/spool/qmailscan/qmail-queue-backup.tar.gz $LOGFILE
+ tar -cvzf /var/spool/qmailscan/qmail-queue-backup.tar.gz /var/spool/qmailscan/qmail-queue.log
tar: Removing leading `/' from member names
var/spool/qmailscan/qmail-queue.log
echo "" > $LOGFILE
+ echo ''
 
Old 05-21-2008, 05:50 PM   #11
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 5&6 CentOS 5, 6 & 7
Posts: 572

Rep: Reputation: 56
Looks like it worked doesn't it? Was this in foreground or from cron?
 
Old 05-22-2008, 12:11 AM   #12
Arty Ziff
Member
 
Registered: May 2008
Location: Tacoma, WA
Distribution: CentOS and RHEL
Posts: 102

Original Poster
Rep: Reputation: 15
Yes! The whole thing was the path to the commands. Instead of mv and tar, I changed it to /bin/mv and /bin/tar. That was IT!

(I learned a few other tricks in the process of discovering the obvious flaw...)

But I'm still left wondering why I could run the script from the command line, but from cron there was the path issue. This is a pretty vanilla install of RHEL 4, but the distro shouldn't make much difference, should it?

Last edited by Arty Ziff; 05-22-2008 at 12:21 AM.
 
Old 05-22-2008, 01:44 AM   #13
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
As mentioned, when you login to a normal cli session, a lot of variables inc PATH are setup (see /etc/profile, ~/.bashrc, ~/.bash_profile) for you.
This does NOT occur in the cron env. The default env vars, inc PATH, are minimal if they even exist.
That's why most (newbie) probs resolve to supplying complete absolute paths to cmds.
It is possible to put some env vars into the crontab, but generally its advisable not to surprise people like that.
 
Old 05-23-2008, 12:01 AM   #14
Arty Ziff
Member
 
Registered: May 2008
Location: Tacoma, WA
Distribution: CentOS and RHEL
Posts: 102

Original Poster
Rep: Reputation: 15
OK, so this is why I see the default PATH definitions in many shell scripts...
 
Old 05-23-2008, 01:08 AM   #15
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Exactly, reduces the risk of running someone else's 'version' of a cmd, which could do anything at all, if they can sneak it into your default PATH.
This is also why your current dir '.' is NOT in your PATH.
 
  


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
shell script using /etc/cron.hourly to execute cron.php file? rioguia Programming 3 06-11-2008 09:09 AM
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 09:16 AM
cron hourly, daily, cron.d jobs don't execute eggsmartha Linux - General 3 09-17-2007 07:37 PM
How to set up cron job to execute bash script lgmqy2000 Linux - General 4 11-22-2006 05:29 AM
execute a script every 5 minutes "cron" ALInux Linux - General 1 02-10-2006 03:36 AM


All times are GMT -5. The time now is 06:00 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration