Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Problem statement

I have 5 solaris boxes, some have Solaris 10 and some have Solaris 9.
All of them have many cronjobs in their crontabs.

I would like to know number of active cronjobs available, so I manually count the cronjobs.

Now I am looking for a command [bash shell I am using] to count the number of active cronjobs.

I have tried crontab -l|wc -l, but my crontab contains many comments lines which are also counted with my command. crontab

What I have tried

crontab -l|wc -l

What I am expecting

A bash shell command to count the number of active cronjobs (excluding comments lines).

share|improve this question
    
Which cron implementation are you using? Some of them allow directives like @daily or @hourly, which also define jobs. –  manatwork Feb 9 '12 at 9:01
    
@ manatwork Im a newbee,I dont understand ur statement,I did not use @daily like commands. –  Balaswamy vaddeman Feb 9 '12 at 9:39
    
This days most of the crons are actually improved rewrites of cron, like anacron, dcron, fcron, mcron. They are all compatible with the original one, but usually they add something new too, by introducing directives with proprietary syntax. Anyway, if you use no directives, you can safely ignore my question. –  manatwork Feb 9 '12 at 9:58
add comment

4 Answers

up vote 2 down vote accepted

You need to delete everything that does not start with a digit (the minute). But to get that, remove any leading whitespace first. This will get rid of comments, blank lines, variable assignments, etc.

crontab -l | sed 's/^ *//;/^[*@0-9]/!d' | wc -l
share|improve this answer
1  
What about entries starting with a * or @? –  forcefsck Feb 9 '12 at 10:10
    
Correct, I missed the "*". Thanks. –  Arcege Feb 9 '12 at 12:29
add comment
crontab -l | grep -v '^#'

Simple.

The number?

crontab -l | grep -v '^#' | wc -l

or

crontab -l | grep -c -v '^#'

(last one inspired by an answer here).

This will give you the (number of) scheduled cron jobs, not the active cron jobs, which could mean the jobs that are currently running.

share|improve this answer
1  
This counts empty lines, lines with an indented comment, and lines containing environment variable definitions. –  Gilles Feb 9 '12 at 23:03
add comment

Why remove characters? Try the following:

$ crontab -l | grep -c "^[0-9*]"
share|improve this answer
    
says illegal option -p on my solaris box –  Balaswamy vaddeman Feb 9 '12 at 8:47
    
This would be my preferred way, but the minute can be “*” or “*/5” too, which would be missed by your expression. –  manatwork Feb 9 '12 at 8:56
    
@Balaswamyvaddeman note it's a big P , can you try again ? –  warl0ck Feb 9 '12 at 9:08
    
@manatwork , thanks , i've updated my answer –  warl0ck Feb 9 '12 at 9:10
1  
@AaronLewis, either ^[\d*] or ^(\d|\*). As you wrote you allow the literal “|” character in the character class. But ^[0-9*] would be better, to get rid of the -P dependency. –  manatwork Feb 9 '12 at 9:39
show 2 more comments

after multiple attempts I have got below command.

crontab -l |sed -e '/#/d'|wc -l

more on sed

more on wc

share|improve this answer
    
I believe that '/#/d' would remove valid lines that happen to have a comment at the end. –  Arcege Feb 9 '12 at 6:28
    
though my solution works for my requirement,urs is more solid –  Balaswamy vaddeman Feb 9 '12 at 7:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.