Parsing cron data with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parsing cron data with awk
# 1  
Old 02-04-2013
RedHat Parsing cron data with awk

Heya,

I'm currently working on a script so I can see which cron jobs, if any, on a system are executing less frequently than 15 minutes (1 - 14 minutes). This is the only data I'm interested in. So far I have the following:

Code:
#!/bin/bash

IFS=$'\n';for line in `ls -f /var/spool/cron/*`;
 do
  echo -e '5\n10\n0\n20' | awk '{if($1 ~ "*" || ($1 ~ /^[0-9]+$/ && $1 != 0) && $1 < 15) print}' <${line};
done

That being said, I'm a bit stuck. That lists the data for minutes, but if a cron job is like so:

5 2 * * * echo "test"

It will still list that cron job, since it's not taking into account the other columns. Is there a decent way to search out only the cron jobs that meet said criteria while excluding those which execute on the hour/day/week?

Any help is appreciated. Thanks!
# 2  
Old 02-04-2013
A cronjob that runs every 6 mins would look like (V7 standard)
Code:
0,6,12,18,24,30,36,42,48,54 * * * *

or
Code:
0-54/6 * * * *

You should also consider some of these (should they be listed or not?):
Code:
* * * * 2 # every min on tuesdays
20-30 * * * * * # every min between 20 and 30 past hour
2-58/2 9-17 * * * # every 2 mins 9am - 5pm

# 3  
Old 02-04-2013
Ah, I didn't even consider those... yeah, that's going to make it a lot more complicated than it already is.

Do you know of any docs / examples out there which cover specifically what I'm trying to do here? I've been reading awk docs throughout the day, but I've of yet to find anything of relevance. Just a piece here or there, but nothing that's helped me form a whole so far.
# 4  
Old 02-04-2013
Dosn't appear to be a lot of resources around for processing crontab entries.

Do you need to know if any job that MAY run less than 15 mins apart, or for a given day/hour will the script run more than twice in a 15min period?. Think about something that is setup to run every min but only on Fri 13th, should this be listed by your script or only when the run date is actually Friday 13th.
# 5  
Old 02-04-2013
The script is only going to run once a week. I'm not interested in anything that runs every 15 minutes or greater. If a script runs on Friday at 2 pm, it should be ignored. Now, if a script is supposed to run on Friday every 5 minutes, then that of course could be something I want to see.

That being said, there are so many potential combinations that some of them are unrealistic for the scope of what I'm trying to do here. I basically just need to say that, if a script is set to execute between 1 and 14 minutes in frequency every hour, every day of the week then it should be flagged. However, if it's set to execute every hour at 5 past the hour then it should be ignored. Likewise with daily/weekly.

I'm wondering if potentially using additional regex's for the other columns would be a viable way to go. i.e. if * or 1 - 9 is found in columns 2, 3, 4, or 5 then exclude this cron from the list.
# 6  
Old 02-04-2013
Quote:
Originally Posted by Chubler_XL
A cronjob that runs every 6 mins would look like (V7 standard)
Code:
0,6,12,18,24,30,36,42,48,54 * * * *

or
Code:
0-54/6 * * * *

You should also consider some of these (should they be listed or not?):
Code:
* * * * 2 # every min on tuesdays
20-30 * * * * * # every min between 20 and 30 past hour
2-58/2 9-17 * * * # every 2 mins 9am - 5pm

Many systems still don't support / in the time fields (so check your cron and crontab man pages before worrying about /.

If your system supports / and one appears in the first field and the number after the / is less than 15, flag it.
Otherwise, if there is a - in the first field, flag it.
Otherwise, if there are one or more commas in the first field, sort the values separated by the commas and if the difference between any two adjacent values in the sorted list is less than 15, flag it. If the difference between the last value in the list and the (1st value in the list + 60) is less than 15, you have to determine if the hour field will ever yield consecutive hours. If it does, flag it. (Note that consecutive hours could also be hour 23 on one day and hour 0 on the next day, ... ... ...) Sorting the values in the minutes field is necessary because a minutes field of:
Code:
0,20,40,10,50,30

results in the job running every 10 minutes, but no two adjacent values are within 15 minutes of each other.

The logic isn't that hard, but there are enough special cases about which fields are considered depending on which fields have values specified in a crontab entry that this is not going to be a trivial task.

Good luck.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing XML (and insert data) then output data (bash / Solaris)

Hi folks I have a script I wrote that basically parses a bunch of config and xml files works out were to add in the new content then spits out the data into a new file. It all works - apart from the xml and config file format in the new file with XML files the original XML (that ends up in... (2 Replies)
Discussion started by: dfinch
2 Replies

2. Shell Programming and Scripting

Help with parsing data with awk , eliminating unwanted data

Experts , Below is the data: --- Physical volumes --- PV Name /dev/dsk/c1t2d0 VG Name /dev/vg00 PV Status available Allocatable yes VGDA 2 Cur LV 8 PE Size (Mbytes) 8 Total PE 4350 Free PE 2036 Allocated PE 2314 Stale PE 0 IO Timeout (Seconds) default --- Physical volumes ---... (5 Replies)
Discussion started by: rveri
5 Replies

3. Shell Programming and Scripting

Data parsing

Hi, I do have a data file which is divided into compartments by ---------. I would like to extract (parse) some of the data and numbers either using awk or sed The file has the format: CATGC Best GO enrichment: Genes/ORF that have the motifs (genes are sorted by max(pa+pd+po)): ... (6 Replies)
Discussion started by: Lucky Ali
6 Replies

4. Shell Programming and Scripting

Help in Parsing data

I have below string Transaction_ID:SDP-DM-151204679 , Transaction_DateTime:2011-02-11 00:00:15 GMT+05:30 , Transaction_Mode:WAP , Circle_ID:4 , Circle_Name:BJ ,Zone: , CustomerID:B_31563486 , MSISDN:7870904329 , IMSI:405876122068099 , IMEI: , Sub_Profile:Pre-Paid , CPID:Nazara , CPNAME:Nazara ,... (6 Replies)
Discussion started by: poweroflinux
6 Replies

5. Shell Programming and Scripting

Parsing the data

Hi friends, I need to parse the following data in the given format and get the desired output. I need a function, which takes the input as a parameter and the desired output will be returned from the function. INPUT(single parameter as complete string) A;BCF;DFG;FD ... (3 Replies)
Discussion started by: sumesh.1988
3 Replies

6. Shell Programming and Scripting

Parsing cron with sed

Hello I want to convert my cron list into a csv Can you please help me with sed ? eg: Convert #06,21,36,51 * * 1,2 * (. ~/.profile ; timex /some/path/script -30 -15) >> /some/path/logfile2 2>&1 * * * * * (. ~/.profile ; timex /some/path/script2) > /some/path/logfile2 To:... (1 Reply)
Discussion started by: drbiloukos
1 Replies

7. Shell Programming and Scripting

Parsing data

Hi all , I have a file with billing CDR records in it. I need to parse that information (row format) . The purpose is to compare full content. The example I have given below is a single line record but it has two portions, (1) the line start with “!” and end with “1.2.1.8” and (2) second part... (5 Replies)
Discussion started by: jaygamini
5 Replies

8. Shell Programming and Scripting

Question about Awk for Data Parsing

Hi, I am doing some data parsing for some economics research. I was recently exposed to shell script and am brand new to awk. I have a large csv file (like 10G) and I would like to make it a lot smaller with awk, but it is a bit tricky for me and I haven't been able to get it yet. I would... (5 Replies)
Discussion started by: EconResearch
5 Replies

9. Shell Programming and Scripting

Parsing the data

Hi I need to parse the following data using shell script Table ----- stage4n_abc 48 stage4o_abcd 4 adashpg_abc_HeartBeat 1 stage4l_asc 168 Can anyone gimme the solution. I want each value to get stored in an array or variable and want the value to be greped from another file.... (1 Reply)
Discussion started by: Archana.Dheepan
1 Replies

10. UNIX for Dummies Questions & Answers

Parsing XML dynamic data via awk?

I am trying to use a line of output in an XML file as input in another new XML file for processing purposes via a shell script. Since I am a newbie though, I'm not sure how to do this since the data is different everytime. I am using this technique with static data right now: echo -n "Running... (5 Replies)
Discussion started by: corwin43
5 Replies
Login or Register to Ask a Question