Scheduling a task using cron doesn't work

I want to run a apache log rotation task every once a week on my server. I came up with a script call " rotatelog" that does the task. So I want to run this script on every friday. I tried two ways to set it up :

1) Schedule the task by running "crontab -u myusername -e" , and add one line to the cron file like this:
40 9 * * fri root rotatelog &

and after exit from the crontab, restart the crond service

2) put a file called "rotatelog.cron" in dir /etc/cron.d, which contains exactly the same line mentioned above. Then restart crond.

But neither way works so far, the rotatelog script never run.

Could any body figure out why? Is there something else I should do to schedule the task?
Thanks a lot
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Try to do as root directly 'crontab -e'
them modify the file and put:
40 9 * * fri rotatelog

or put the same line using 'crontab -u myusername -e'

(might want to put something like * * * * * rotatelog to test it)
QuarfelburgAuthor Commented:
hi, deurk,

It still doesn't work, even when i put the line "* * * * * echo hello" by command "crontab -e" as root.

I suspect that the crond daemon is not working, although it's running.

any further idea?
I don't believe you'll be able to rotate the log as other than root. So you need to add:

40 9 * * 5 /path-to/rotatelog

to root's crontab via 'crontab -e' as root. Not that the syntax for crontab's are different from those in /etc/cron* (anacron). If you want to add to anacron you need to edit /etc/crontab and add:

40 9 * * 5 root /path-to/rotatelog

Be sure that you have absolute paths to all commands, files, etc both when calling the script and within the script.
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Anything in the logs?
/var/log/cron or syslog?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
QuarfelburgAuthor Commented:

yes, it has, something like "Nov  5 11:37:00 hayden CROND[5672]: (root) CMD (path-to/rotatelog &)" record in the file /var/log/cron. Does it mean that it's been executed? Why couldn't I see anything changed?

> "* * * * * echo hello"
You might want to change to
* * * * * echo hello >> /tmp/testfile

So you can check the time(or content) of /tmp/testfile. In crontab, "echo hello" might not show "hello" on your screen.

QuarfelburgAuthor Commented:

You are right, echo hello doesn't show "hello" on my screen but can output to a file. So I've tested the crond works. From now on i should focus on checking why the rotatelog doesn't work.

Thanks a lot.
QuarfelburgAuthor Commented:
BTW deurk's comments were helpful. Thank you very much.
"CROND[5672]: (root) CMD (path-to/rotatelog &)"" says that you are telling cron to background the rotate logs script and you don't want to do that. Edit the crontab and remove the trailing & and see if it works or what errors are reported in mail to root.
Good luck Quarfelburg ^^
I would recommend using the "logrotate" package that is available for nearly every linux distribution and is normally bundled with Apache definitions and a cron.d script.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.