Link to home
Start Free TrialLog in
Avatar of Quarfelburg
Quarfelburg

asked on

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
Avatar of deurk
deurk

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)
Avatar of Quarfelburg

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of deurk
deurk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
deurk,

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.

Wesly
wesly,

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.
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.