Discussion Locked

manage cron entry via script

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hi,

i am building a custom rpm which i also want to do the following:
1. install a crontab entry for 11pm, to run a script daily
2. automatically remove the cron entry when removing the rpm
3. upgrade the cron entry if changed when upgrading the rpm.

I know a bit about shell scripting, but im not too clever with awk,sed and perl which i think is what i will need.

cron.daily or cron.weekly are not suitable because they run early in the morning, not late at night, which is necessary as this script will run backups.

I would use pre and post directives for the rpm spec file.
A sample cron entry would be
"0 23 * * 1-5 /path/to/my/script"

Can someone help with how to automate the addition/deletion/modification of a cron entry, using either awk, sed or perl or equivalent?

TIA,

Les

3 replies

by jaqui

cron daily is the right choice.
tell cron WHEN to run it, that is an option in the cron daily.

and, cron daily normally runs around midnight if the time is not set by the script.

by support

Sorry but cron.daily is not the right answer.
cron.daily runs at 4am by default. I need to run this script at the end of the day, not the beginning.
I dont want to tell cron.daily to run at a different time because then i'm relying on cron.daily to be customised. What happens when a new rpm of the cron tools is installed and resets the default runtimes for cron.daily back to 4am?
This sort of thing has happened to me before with many other rpms restoring standard configurations when upgraded.
I cannot rely on cron.daily.
I want this script and its install via rpm to be totally self-manageable.

by nico baggus

Adding:
---8<---
#!bin/sh
#add script...
# $1 = filename

echo '0 23 * * 1-5 /path/to/my/script' >>$1
exit 0
---8<---

Removing:
---8<---
#!bin/sh
#add script...
# $1 = filename

grep -v '0 23 * * 1-5 /path/to/my/script' $1 >$1.tmp \
&& mv $1.tmp $1
exit 0
---8<---

unset VISUAL
EDITOR=addscript
or
EDITOR=removescript

crontab -e [-u] root

I would do some modification by removing an old one
and adding a new one...
Te problem being is someone modifies it, I think
these are teh edit options...

Otherwise:

drop it in the hourly table and run
as the first thing just check if it time to run the BIG
thing, if not exit

This discussion is closed to new replies.