?
Solved

Scheduling a task using cron  doesn't work

Posted on 2004-11-05
11
Medium Priority
?
615 Views
Last Modified: 2013-12-15
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
0
Comment
Question by:Quarfelburg
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 2

Expert Comment

by:deurk
ID: 12506595
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)
0
 

Author Comment

by:Quarfelburg
ID: 12506842
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?
0
 
LVL 40

Expert Comment

by:jlevie
ID: 12507221
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.
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 2

Accepted Solution

by:
deurk earned 375 total points
ID: 12508006
Anything in the logs?
/var/log/cron or syslog?
0
 

Author Comment

by:Quarfelburg
ID: 12508422
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?

0
 
LVL 38

Expert Comment

by:wesly_chen
ID: 12508505
> "* * * * * 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
0
 

Author Comment

by:Quarfelburg
ID: 12508868
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.
0
 

Author Comment

by:Quarfelburg
ID: 12508877
BTW deurk's comments were helpful. Thank you very much.
0
 
LVL 40

Expert Comment

by:jlevie
ID: 12509097
"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.
0
 
LVL 2

Expert Comment

by:deurk
ID: 12509486
Good luck Quarfelburg ^^
0
 

Expert Comment

by:prof_pickles
ID: 12526103
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.
0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses
Course of the Month16 days, 3 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question