Adding a shell script to the daily Cron

Associate
Joined
18 Nov 2008
Posts
2,430
Location
Liverpool
Hey guys, trying to rotate a log using a shell script (It's necessary, I can't use logrotate for my purpose unfortunately). So I've writted the shell script and tested it to see it works fine when manually executed.

So now I want to add it to run with the daily Cron, but I can't figure out how to do it. If I do crontab -e it opens it up for editing, I edit it and close it to see it says "no crontab for root - using an empty one", and then notice it has not made the changes.

I have also tried doing it through Webmin but it wants me to input the command there which I assume isn't right for what I want as the script is multiple lines long?

Basically, how do I add my .sh file to the daily Cron with the above problems?

Thanks!
 
Last edited:
If I do that and come out of it though it says it has not made the changes (Small typo in OP)

EDIT: And yes, it is CentOS
 
As it's CentOS have you tried putting your script into /etc/cron.daily/ ? Looking at the CentOS box I am logged into here /etc/crontab indicates anything in that directory should be run, as root, at 04:02 each day ...
I was originally going to do this, but I'd like it to run at a specified time not 04:02 (Which I guess is the CentOS standard as that's the time on mine as well.

Are you trying to run the script in cron as root or another user?
Root

Is root, or the other user, in /etc/cron.allow ?
There currently isn't an allow, only an empty deny

Do you have a /var/spool/cron/ directory and is there a file in there named as per the user you are creating the cronjob as with just rw permissions for the user ?
There is only a "tmp.XXXXDN86MW" in there?

Is cron enabled for your current runlevel (chkconfig --list cron) ?
Erm: error reading information about service cron: No such file or directory
Is what I get when I try that?
 
that brings up the content which are:

Code:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
 
Oh! I didn't realise you could add it directly into the file! I know how to write it as I found a great guide so I can do that bit, I just didn't realise you could add it directly into that file!

Thanks very much, I'll do a test one now :)
 
I've added:
Code:
0-59 0-23 * * * /root /directory/to/shell/script/rotatelog.sh

But it's not working, whereas it does when manually executed.
 
Last edited:
Sorry just edited my previous post, I have done that but it's still not doing anything.


EDIT: Misread your post and realised my mistake! Applied yours now will update with progress.
 
It's still not working, I've checked the cron log to see the last 3 entries are what I want:
Code:
May 12 16:33:01 localhost crond[16101]: (root) CMD (/directory/to/shell/script/rotatelog.sh)
May 12 16:34:01 localhost crond[16163]: (root) CMD (/directory/to/shell/script/rotatelog.sh)
May 12 16:35:01 localhost crond[16207]: (root) CMD (/directory/to/shell/script/rotatelog.sh)

But it's still not doing anything?
 
It's still not working with that entered and the service restarted.

I'm trying to call this .sh file at 7:05AM every day, but for the purpose of making sure it works at all I'm making it run every minute so I can check that it creates the file as the contents of the .sh indicates.

If I run it manually it works, but through this it doesn't :(

EDIT: Not sure if this is relevant, but with the code as:
Code:
22 0-23 * * * /directory/to/shell/script/rotatelog.sh
It was making log entries of:
Code:
May 12 16:33:01 localhost crond[16101]: (root) CMD (/directory/to/shell/script/rotatelog.sh)

Whereas with:
Code:
*/1 * * * root /directory/to/shell/script/rotatelog.sh
It makes no log entries.
 
Last edited:
Yeah just spotted that, I replaced it with yours with the contents edited, I get:
Code:
May 12 16:53:01 localhost crond[17398]: (root) CMD (/directory/to/shell/script/rotatelog.sh)
In the log but it still doesn't do anything.

This was with:
Code:
*/1 * * * * root /directory/to/shell/script/rotatelog.sh
 
Ahh that worked. So it is the script but it does work manually? Here is the script:
Code:
DYEAR=$(date +%Y)
mkdir logs/$DYEAR
DIR=$(date +%B)
mkdir logs/$DYEAR/$DIR
CURDATE=$(date +%d-%m-%Y)
cp server_log.txt.1 logs/$DYEAR/$DIR/$CURDATE.log

It makes a copy of the current log and places it into a directory of /logs/Year/Month/CurrentDate.log

Could is be anything to do with it executing from the /etc directory when ran by crontab rather than the directory it is placed?
 
I have replaced the contents of rotatelog.sh with what you posted, still nothing happens though. The log shows:
Code:
May 12 17:50:01 localhost crond[21150]: (root) CMD (/directory/to/shell/script/rotatelog.sh)

This is with the /etc/crontab as:
Code:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly

*/1 * * * * root /directory/to/shell/script/rotatelog.sh

and the rotatelog.sh as:
Code:
#!/bin/bash

 DYEAR=$(date +%Y)
 mkdir logs/$DYEAR
 DIR=$(date +%B)
 mkdir logs/$DYEAR/$DIR
 CURDATE=$(date +%d-%m-%Y)
 cp server_log.txt.1 logs/$DYEAR/$DIR/$CURDATE.log
 
Just posting to hide, but the structure is the same, the names between the slashes are the only things changed. I wondered how long before somebody asked that haha.

Just checked the log again and noticed that this comes up when the daemon starts:
Code:
May 12 17:54:23 localhost crond[21439]: (CRON) STARTUP (V5.0)
May 12 17:54:23 localhost crond[21439]: (tmp.XXXXDN86MW) ORPHAN (no passwd entry)
 
What's the output of:
Code:
ls -la /var/spool/cron
?

As follows:
Code:
total 16
drwx------  2 root root 4096 May 12 13:09 .
drwxr-xr-x 12 root root 4096 Dec 21 14:46 ..
-rw-------  1 root root    0 May 12 13:09 tmp.XXXXDN86MW

Also I take it you're editing root's crontab?
Yep sure am!

You've definitely given the script executable permissions right?
Yep think so, I did it using chmod u+rwx /directory/to/shell/script/rotatelog.sh
 
Does the file it's trying to move actually exist when the script is triggered?
I'd try it once an hour or so to give the system time to actually generate your log.

Also, who owns the script in question, and who are you running it manually as?
I'd halfway wonder if something is throwing up a security measure somewhere about using root. Shouldn't do, but I've seen stranger things happen, as you can't use logrotate your app is obviously non-standard.

-Leezer-
The file definitely exists yes, it's owned by root, so is the .sh script. The only thing I can think of in that area is that the Cron service is being ran under a user with less privileges, but I'm not sure how to check?

I would suggest that you should remove that tmp.XXXXDN86MW file from under /var/spool/cron/ .... it's an empty file and shouldn't be there. That would remove the error from the crond log file. It probably wouldn't fix the overall problem but should rule that affecting things out.

Have you got SELinux enabled on the system? What's the output of getenforce ?
That outputs 'Disabled', is that something you think would affect it?

Might be worth outputting any error's the script generates from cron?

*/1 * * * * root /directory/to/shell/script/rotatelog.sh 2>/tmp/rotatelog.error
mkdir: cannot create directory `/directory/to/shell/script/logs/2011': File exists
mkdir: cannot create directory `/directory/to/shell/script/logs/2011/May': File exists
cp: cannot stat `server_log.txt': No such file or directory

The first 2 errors are fine, it tries to create the directory every time so that I can be sure it's there for when the month / year changes, as far as I know from research those errors aren't be enough to cause problems.

As for the third error, that one is evidently the problem, but I'm not sure why? The file is definitely there, I can seee it on SSH and over FTP so why would it be throwing up that error? Could it be trying to read it from the wrong directory? Is there anyway of checking?
 
Last edited:
Hey guys, I changed all paths in the shell file for absolute paths and it seems to have fixed the problem! I assume the root directory for the user running the Cron was influencing the scripts ability to find the destination file.

Anyway, thank you all very much for your help on this, hugely appreciated! I'll make it all run daily now and update with my progress!
 
Back
Top Bottom