Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need to query the crontab file.

Status
Not open for further replies.

Tison

Programmer
May 12, 1999
216
CH
I am trying to write a script that will show me what jobs are scheduled thru cron on a specific date.
Does anyone have a clever plan on how to do this ?

The crontab entry looks like this ;
00 19 * * 1-4 /home/cleanup
00 04 1-5,15,28 * * /home/archive

I can get the first entry without a problem,but how do I tell my ksh script to deal with any combination of * , or - ?
 
crontab -l # list crontab
crontab -e # edit using $EDITOR
 
NO,,,you are missing the point.

I am trying to write a script that will tell me what jobs are going to run today.
eg
My script will return ;
"The following jobs are scheduled to run today ; at 04:00 /home/archive , at 19:00 /home/cleanup"
 
Hmmm... This is going to be quite a complex script, but here are some starting pointers. Maybe someone can add to this.

Assuming you only need to check for crons running each day, then you need to check two fields, these being field 3 day of month, and field 6 day of week.

Lets concentrate on day of month.

First cut the field

CronDOM = `cut -d" " -f3` (from each line of crontab file)

Now get the current day of month from date command

TodayDOM = `date -u +%e`

Now you need to compare the two, but here its gets complicated because CronDOM could be :-

A single number e.g 1
Several numbers e.g 1,7,15
Range of numbers e.g 1-7
Any number e.g *

Comparing TodayDOM to a single or any CronDOM should be easy enough, but Several or range are more complicated.

If you can do this for DOM, then the scripting for Day of week should be very similar.

Hope this gives you a starting point. Perhaps others can add to this ?
 
Don't really have time to go into this in detail, but would it halp to put a 'dummy' entry below each crontab entry, eg:

00 19 * * 1-4 /home/cleanup
#00 19 * * Mon Tue Wed Thu /home/cleanup
00 04 1-5,15,28 * * /home/archive
#00 04 1-5,15,28 * Every day /home/archive

It strikes me that it would be easier to compare the Mon .. etc (ie a textual 'day') using the date command syntax than going into loops to convert the crontab 'day' to something recognisable by date.

Hope this helps or at least sparks other ideas you may have.



 
You will have fun learning about regular expressions, but that is the key to your solution. I suggest using awk. You can do "crontab -l > /tmp/croncheck.out" or something if you want to gen a file, or you can pipe "crontab -l" into awk. Either way, you will want to match patterns.

I can't possibly go over it all, since there are hundreds of ways to do it, so I suggest the O'Reilly book on sed and awk. You can do math in awk, which will be useful if you want to do any date calculations.

You can, in awk, match a pattern against a specific field, so in the day-of-month example you could use the above method to get today's date into a variable (TodayDOM = `date -u +%e`, but that puts a space in front of the 2 for today) and match against it like so:

awk ' $3 ~ TodayDOM {print $0}' TodayDOM=$(date|awk '{print $3}')

The assignment to TodayDOM is done within awk in this case, and I used awk to get the day-of-month because that way there is no leading space. The command "print $0" will print the whole line in the file, but here only when the third field matches what is in TodayDOM.

I hope that gives you a good start. Throw in some regex and you will have a great script.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top