4

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).

3
  • 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. 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
3

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 2>/dev/null | sed 's/^ *//;/^[*@0-9]/!d' | wc -l
3
  • 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
  • 1
    I would add 2>/dev/null to the first command, so if there no job, then no error output. Feb 17 at 20:02
2
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.

1
  • 2
    This counts empty lines, lines with an indented comment, and lines containing environment variable definitions. Feb 9 '12 at 23:03
2

Why remove characters? Try the following:

$ crontab -l | grep -c "^[0-9*]"
7
  • says illegal option -p on my solaris box 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 ?
    – daisy
    Feb 9 '12 at 9:08
  • @manatwork , thanks , i've updated my answer
    – daisy
    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
0

after multiple attempts I have got below command.

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

more on sed

more on wc

2
  • 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 Feb 9 '12 at 7:04
0

You could do:

PATH=`getconf PATH`:$PATH # needed on Solaris to get POSIX compliant versions
                          # of the basic Unix utilities

crontab -l | LC_ALL=C grep -c '^[[:blank:]]*[0-9@*]'

That is count the lines that start with either ASCII decimal digits or * or @ (the latter not currently supported by Solaris cron but supported by a few other implementations) preceded by 0 or more blanks (in the C locale, that's either SPC or TAB).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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