Solved

Contrling crontab in script

Posted on 2006-10-30
8
257 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 50 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 50 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 50 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 50 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

739 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