Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Contrling crontab in script

Posted on 2006-10-30
8
Medium Priority
?
260 Views
Last Modified: 2010-04-20

Hello,

In a script, user is allowed to set a schedule to make a backup but I need to limit it to one entry in crontab no more. What should I do?

should I count the number of lines in crontab and warn user or do you have a better idea?

Thanks,
A
0
Comment
Question by:akohan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 2

Accepted Solution

by:
Alticon-Brian earned 200 total points
ID: 17838207
What scripting language?

If you're keeping it to a straight bourne compatible shell you could do something like this:


proc_count=`grep scriptname /etc/crontab | wc -l`

Proc count will then have the number of times that the script is in there.  You can check and see if it's greater than zero, if it's not show a warning and run an exit(1) or something similar.

0
 

Author Comment

by:akohan
ID: 17838334

Yes. I'm using regular bash.
0
 
LVL 58

Assisted Solution

by:amit_g
amit_g earned 200 total points
ID: 17838499
if [ $(crontab -l | grep -v '^#' | wc -l) -le 1 ]
then
    echo "Less than or equal to 1 entry"
else
    echo "More than 1 entry"
fi
0
 
LVL 23

Assisted Solution

by:Mysidia
Mysidia earned 200 total points
ID: 17838614
I would take the strategy of marking the line to crontab added by the script, and remove that line,
before adding a line.

I.E. when your script adds the crontab line, also add a trailing comment

[blah blah]  #Line Automatically Added: Leave this note intact


crontab -l |egrep -v '#Line Automatically Added: Leave this note intact$'  | crontab -

0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 200 total points
ID: 17838768
To borrow from amit_g's solution, but removing the need for wc

if [ $(crontab -l | grep -cv '^#') -le 1 ]
then
    echo "Less than or equal to 1 entry"
else
    echo "More than 1 entry"
fi
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question