Solved

Contrling crontab in script

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
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 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.:
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

803 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