1829411 Members
2195 Online
109991 Solutions
New Discussion

Crontab Filtering

 
SkBhat
New Member

Crontab Filtering



We need to create a script to find all the programs or scripts running at a particular time from crontab.

1. Should have an option to search by name or using Shell's meta characters.
2. Should have an option to search by Minute.
3. Should have an option to search my Hour.
4. Should have an option to search by day.
7. Should have an option to search by month.
8. Should have an option to search by Weekday.

for Ex : filter.sh -h 22 -day 10 -f /var/spool/cron/crontab_file
The above script should display all the scprits running at 22:00 and 10th of each month. /var/spool/cron/crontab_file is sample crontab file.

Could anyone throw some light on this?
7 REPLIES 7
Dennis Handly
Acclaimed Contributor

Re: Crontab Filtering

You are going to have to write a program or a perl script if you are going to allow complex queries.

If all you are doing is ANDing all of your options you might be able to grep and awk to select the fields. But you would have to handle "*" and ",".
Michael Steele_2
Honored Contributor

Re: Crontab Filtering

Why can't you just look at the /var/adm/cron/log? Why can't you just tail the log and grep for CMD?
Support Fatherhood - Stop Family Law
Fredrik.eriksson
Valued Contributor

Re: Crontab Filtering

Dennis,
You can create complex queries using case statement and shift. It's not fool proof thou.
ex.
filter.sh -h 22 -day 10 -f file

until [ -z $1 ]; do
case $1 in
-h)
some_command $2
break
;;
-day)
some_other_command $2
break
;;
-f)
some_third_command $2
break
;;
esac
shift
done

This is untested (not near a machine) so I'm not 100% sure that break won't destroy the "until" statement. But there's ways around that too. You could just ignore doing a break and let it pass.

But what you need is something that goes throu every line with a regexp and then sorts it out and I think it'll make a lot of nested if-else-then statements.

But I think Michael has the better plan... cat/grep/tail/head the logfile and just get the dates from there.
Dennis Handly
Acclaimed Contributor

Re: Crontab Filtering

>Fredrik: You can create complex queries using case statement and shift.

This is not the complex query I had in mind. I was thinking of combinations of queries 1 through 8 with AND and OR. Possibly searching for multiple names or hours, etc.
It looks like yours does an OR.

>until [ -z $1 ]; do

Typically I only use while and count parms. Yours will fail if using "set -u".
while [ $# -gt 0 ]; do

>that break won't destroy the "until" statement.

I think you want to use just ";;" to terminate each case label. break gets you out of the until.
Also, you need to do "shift 2".
Fredrik.eriksson
Valued Contributor

Re: Crontab Filtering

> It looks like yours does an OR.
Agreed, it'll be tricky and quite long if you want it to take AND parameters. It is possible thou, that was just a quick example on how to get multiple parameters without the need to have them at the right position.

> Also, you need to do "shift 2".
Not really, unless you have a case label that will match the parameter value.
Like having -2 as a case label whilest sending something like "file.sh -degrees -2" (in which case you're absolutely right, shift 2 would solve it).

This is quite easily solved by always saying that you want --degrees instead of just -degrees :)

Best regards
Fredrik Eriksson
Dennis Handly
Acclaimed Contributor

Re: Crontab Filtering

>Fredrik: unless you have a case label that will match the parameter value.

I was talking about your example. But it would be better to have two shifts in case you had various types of options taking different parms:
while [ $# -gt 0 ]; do
case $1 in
-h)
some_command $2
shift
;;
...
*) echo "bad option: $1"
;;
esac
shift
done
Jannik
Honored Contributor

Re: Crontab Filtering

This some light on a possible perl solution. It is not perfect but it works.

#!/usr/bin/perl

$numArgs = $#ARGV + 1;

foreach $argnum (0 .. $#ARGV) {

$argument=@ARGV[$argnum];

if ($argument eq "-day" or $argument eq "-d") {
if (@ARGV[$argnum+1] =~ m/^[0-9]+$/) {
$day=@ARGV[$argnum+1];
} else {
print "@ARGV[$argnum] should only be followed by a digit.\n";
$exitcode=1;
}
}
if ($argument eq "-hour" or $argument eq "-h") {
if (@ARGV[$argnum+1] =~ m/^[0-9]+$/) {
$hour=@ARGV[$argnum+1];
} else {
print "@ARGV[$argnum] should only be followed by a digit.\n";
$exitcode=2;
}
}
if ($argument eq "-minut" or $argument eq "-m") {
if (@ARGV[$argnum+1] =~ m/^[0-9]+$/) {
$minut=@ARGV[$argnum+1];
} else {
print "@ARGV[$argnum] should only be followed by a digit.\n";
$exitcode=3;
}
}
if ($argument eq "-month" or $argument eq "-M") {
if (@ARGV[$argnum+1] =~ m/^[0-9]+$/) {
$month=@ARGV[$argnum+1];
} else {
print "@ARGV[$argnum] should only be followed by a digit.\n";
$exitcode=4;
}
}
if ($argument eq "-weekday" or $argument eq "-w") {
if (@ARGV[$argnum+1] =~ m/^[0-9]+$/) {
$weekday=@ARGV[$argnum+1];
} else {
print "@ARGV[$argnum] should only be followed by a digit.\n";
$exitcode=5;
}
}
}

if ($exitcode) {
exit $exitcode;
} else {
# Run your script
print "$hour $minut $day $month $weekday\n";
}
jaton