cancel
Showing results for 
Search instead for 
Did you mean: 

how to sort crontab in unix

surekha_4
Occasional Advisor

how to sort crontab in unix

I want to know, how can you sort crontab with respect to min, hour and day of month.
10 REPLIES
Steve Steel
Honored Contributor

Re: how to sort crontab in unix

Hi

The fields in format x,x,x

Will give a problem but

crontab -l|sort -k 1,1 -k 2,2 -k 3,3

Works


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Robert-Jan Goossens_1
Honored Contributor

Re: how to sort crontab in unix

Hi,

CRON ENTRIES

minutes hours day Month day_off_week

Hope this helps,
Robert-Jan
Franky_1
Respected Contributor

Re: how to sort crontab in unix

Hi,

this should work with

crontab -l | sort -k1 -k2 -k3

Regards

Franky
Don't worry be happy
Muthukumar_5
Honored Contributor

Re: how to sort crontab in unix

min is located in first, hour in second column, and day is on 3rd column on crontab -l

Try as like,

crontab -l | sort -nbk 1,3

It will sort 1 to 3 fields one by one.

-nb are used for optional field separation between the fields.

Regards
Muthu
Easy to suggest when don't know about the problem!
Yogeeraj_1
Honored Contributor

Re: how to sort crontab in unix

hi,

this is how my end of crontab looks like:
#*******************************************************************************
# min|hour |day |month|day |script
# | |of mo| |of wk|
#----|-----|-----|-----|-----|--------------------------------------------------
#*******************************************************************************
#*******************************************************************************
# END OF TABLE day0->Sunday day6->Saturday
#*******************************************************************************

one easy way to see it sorted is using SAM->Process Management->Scheduled Cron Jobs->View->sort

regards
Yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
surekha_4
Occasional Advisor

Re: how to sort crontab in unix

Thanks a lot for all your prompt responses.

I wanted to sort first w.r.t minutes and then w.r.t seconds, and the following got me the desired result.

crontab -l | grep -v ^# | sort -nb -k 2,1,3

...

But i would like to know.. why we are using
-k 2,2 1,1
in this case: crontab -l|sort -k 2,2 -k 1,1 -k 3,3.

Also, i am looking at extracting crontabs for a given timeframe say from 7 am to 8 am.

What i intend to do is to read the sorted crontab into a file and then extract cron-entries from 7am to 8am.
But is there a better method of doing the same?

Do let me know.

Thanks to one and all
Muthukumar_5
Honored Contributor

Re: how to sort crontab in unix

Sort command will work based upon the key settings.

IF you are going to sort with multiple fields one by one then we have to use multiple keys else,

crontab -l | grep -v ^# | sort -nb -k 2,1,3

it will not give the effect.

IF you are trying to sort as non-order then ( 2 , 1 , 3) then you have to use multiple keys. Else you can as like as -k 1,3 ( to sort from 1 to 3)

In your case it will sort based on 2nd field then 1st then 3rd one.

You can also use as,

crontab -l | grep -v ^# | sort -nb -k 2,2 1,1 3,3

or
crontab -l | grep -v ^# | sort -nb -k 2 -k 1 -k 3

-k --> startpos , end pos

You can give or need not.

To know more about the difference refer the sort man page for -k options. It will give some more effective features / usage on keys

Regarding extraction on time limit, it is good to extract the timelimits on sorted cornjobs with grep command.

It will give the way to do.

Regards
+Muthu+
Easy to suggest when don't know about the problem!
surekha_4
Occasional Advisor

Re: how to sort crontab in unix

Thanks for your guidance.

I am sorting crontab and then grep cron for the timeframe.

Regards,
Surekha


Franky_1
Respected Contributor

Re: how to sort crontab in unix

Hi Surekha,

don't forget to assign points ...

Regards

Franky
Don't worry be happy
surekha_4
Occasional Advisor

Re: how to sort crontab in unix

Thanks again to one and all.

Any of the following may be used to sort the crontab:

crontab -l | grep -v ^# | sort -nb -k 2,1,3
or
crontab -l | grep -v ^# | sort -nb -k 2,2 1,1 3,3
or
crontab -l | grep -v ^# | sort -nb -k 2 -k 1 -k 3