Solved

Contrling crontab in script

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

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…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
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…
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.:

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now