Adding a shell script to the daily Cron

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?
 
Try it with the shebang assuming you haven't already excluded it from your post?

i.e. your script should be...

#!/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
 
Last edited:
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
 
Ok, stupid question time now...

Is this"/directory/to/shell/script/rotatelog.sh" the actual location of your script or are you just posting that to hide the location?
 
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)
 
Might have to give up on this one...

I've tested your script on my system and it works perfect both using crontab -e and /etc/crontab.
Unless it's something simple like a permissions error or a path not set correctly I'm not sure what it could be.
 
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
?

Also I take it you're editing root's crontab?
 
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-
 
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 ?
 
Might be worth outputting any error's the script generates from cron?

*/1 * * * * root /directory/to/shell/script/rotatelog.sh 2>/tmp/rotatelog.error
 
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