Solved

Contrling crontab in script

Posted on 2006-10-30
8
256 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
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

830 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