Cron Question

View: New views
8 Messages — Rating Filter:   Alert me  

Cron Question

by ElihuJ :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi all. I have a question about cron jobs that seem to be running to long or with multiple copies of itself. For example, I have a backup script that I run that seems to make multiple copies of itself. If I view the running processes I see numerous instances of the same cron job. Is there something I can do to limit this from happening? When it does, it drains my CPU and some of my other processes are non responsive. Any help would be appreciated. Thank you.

Re: Cron Question

by Albert Shih-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

 Le 02/09/2008 à 08:45:52-0700, ElihuJ a écrit
>
> Hi all. I have a question about cron jobs that seem to be running to long or
> with multiple copies of itself. For example, I have a backup script that I
> run that seems to make multiple copies of itself. If I view the running
> processes I see numerous instances of the same cron job. Is there something
> I can do to limit this from happening? When it does, it drains my CPU and
> some of my other processes are non responsive. Any help would be
> appreciated. Thank you.

That's not the to cron to do that. You must put in your script some flags.

For example if you using

        rsnapshot (in the ports)

he put a lock file in /var/run (or what's ever you want) and don't start if
the script find this file. When the script is end the file is erase.

Something like

if_the_lock_file_exit :

        exit 1

else

        touch lock_file

        my_script

        rm lock_file

fi.

Regards

--
Albert SHIH
SIO batiment 15
Observatoire de Paris Meudon
5 Place Jules Janssen
92195 Meudon Cedex
Heure local/Local time:
Mar 2 sep 2008 18:01:25 CEST
_______________________________________________
freebsd-questions@... mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscribe@..."

Re: Cron Question

by Paul Schmehl-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

--On September 2, 2008 6:03:51 PM +0200 Albert Shih <Albert.Shih@...>
wrote:

>  Le 02/09/2008 à 08:45:52-0700, ElihuJ a écrit
>>
>> Hi all. I have a question about cron jobs that seem to be running to
>> long or with multiple copies of itself. For example, I have a backup
>> script that I run that seems to make multiple copies of itself. If I
>> view the running processes I see numerous instances of the same cron
>> job. Is there something I can do to limit this from happening? When it
>> does, it drains my CPU and some of my other processes are non
>> responsive. Any help would be appreciated. Thank you.
>
> That's not the to cron to do that.
Actually, it could be.  If the script is started by cron and is still
running when the next job is scheduled, cron will start another process.
If they're both still running when the next job is scheduled, you'll have
three processes running, etc., etc.

The first thing I would do is run the script manually and see how long it
takes to complete.  Then set your cron jobs up to run with enough time
between them for the script to complete and exit before the next job
starts.


Paul Schmehl, If it isn't already
obvious, my opinions are my own
and not those of my employer.
******************************************
WARNING: Check the headers before replying

Re: Cron Question

by Dan Nelson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

In the last episode (Sep 02), Paul Schmehl said:

> --On September 2, 2008 6:03:51 PM +0200 Albert Shih wrote:
> >  Le 02/09/2008 à 08:45:52-0700, ElihuJ a écrit
> >> Hi all. I have a question about cron jobs that seem to be running
> >> to long or with multiple copies of itself. For example, I have a
> >> backup script that I run that seems to make multiple copies of
> >> itself. If I view the running processes I see numerous instances
> >> of the same cron job. Is there something I can do to limit this
> >> from happening? When it does, it drains my CPU and some of my
> >> other processes are non responsive. Any help would be appreciated.
> >> Thank you.
> >
> > That's not the to cron to do that.
>
> Actually, it could be.  If the script is started by cron and is still
> running when the next job is scheduled, cron will start another
> process.  If they're both still running when the next job is
> scheduled, you'll have three processes running, etc., etc.

I use the lockfile command ( from the procmail port ) to ensure that
recurring cron jobs don't overlap if one run takes too long. For
example, to run mrtg on a 1-minute cycle but prevent multiple mrtgs
from running if one run takes longer than 1 minute:

* * * * * /usr/local/bin/lockfile -r 1 -l 3600 /tmp/mrtg.LCK && ( nice -19 /usr/local/bin/mrtg /usr/local/etc/mrtg/mrtg.cfg ; rm /tmp/mrtg.LCK )

The -l 3600 tells lockfile to remove any lockfiles over an hour old (
if the machine was rebooted during an mrtg run for example )

--
        Dan Nelson
        dnelson@...
_______________________________________________
freebsd-questions@... mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscribe@..."

Re: Cron Question

by RW-8 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, 2 Sep 2008 11:40:37 -0500
Dan Nelson <dnelson@...> wrote:


> I use the lockfile command ( from the procmail port ) to ensure that
> recurring cron jobs don't overlap if one run takes too long. For
> example, to run mrtg on a 1-minute cycle but prevent multiple mrtgs
> from running if one run takes longer than 1 minute:
>
> * * * * * /usr/local/bin/lockfile -r 1 -l 3600 /tmp/mrtg.LCK &&
> ( nice -19 /usr/local/bin/mrtg /usr/local/etc/mrtg/mrtg.cfg ;
> rm /tmp/mrtg.LCK )
>
> The -l 3600 tells lockfile to remove any lockfiles over an hour old (
> if the machine was rebooted during an mrtg run for example )
>
you could also handle stale lock-files, without installing procmail,
like this:

   LCK=/tmp/foo.LCK
   find $LCK -mtime +3600s -delete
   if ![ -f "$LCK" ] ; then
       touch "$LCK"
       [ -f "$LCK" ] && foo
       rm "$LCK"
   fi

Presumably the lockfile command also eliminates the race between testing
for, and creating, the lock-file, but that's not really needed here.

_______________________________________________
freebsd-questions@... mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscribe@..."

Re: Cron Question

by Derek Ragona :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

At 10:45 AM 9/2/2008, ElihuJ wrote:

>Hi all. I have a question about cron jobs that seem to be running to long or
>with multiple copies of itself. For example, I have a backup script that I
>run that seems to make multiple copies of itself. If I view the running
>processes I see numerous instances of the same cron job. Is there something
>I can do to limit this from happening? When it does, it drains my CPU and
>some of my other processes are non responsive. Any help would be
>appreciated. Thank you.
>--
>View this message in context:
>http://www.nabble.com/Cron-Question-tp19272656p19272656.html
>Sent from the freebsd-questions mailing list archive at Nabble.com.

For longer running jobs I do a couple things.  I use a file to be sure only
one instance is running, but I also add signal handling.  The following is
written for ksh, but can be adapted to sh if needed:

=============================================================
#!/usr/local/bin/ksh
# uncomment the following line for debugging
#set -x


RUNNING_FILE=RUNNING_FILE=/tmp/my_cronjob_running
LOGFILE=LOGFILE=/tmp/my_cronjob.log
SENDTO=me@...
MAIL=/usr/bin/mail
TOUCH=/usr/bin/touch
RM=/bin/rm


# Print an epilog string and clear the RUNNING_FILE
function epilog {
   echo "We are all done scanning." >> $LOGFILE
         $MAIL -s "MyCronjob Report" $SENDTO < $LOGFILE
         if [ -f $RUNNING_FILE ]; then
                 $RM $RUNNING_FILE;
         fi
}

function got_signal {
   echo "Got a signal" >> $LOGFILE
   epilog
   exit
}


# Here pointers to signal handling subroutines are set
trap got_signal TERM HUP INT QUIT

if [ -f $RUNNING_FILE ]; then
         echo "mycronjob is already running"
else
         $TOUCH $RUNNING_FILE
         $RM $LOGFILE
         $TOUCH $LOGFILE
#       add your job to be done here . . .
#
         epilog
fi

=============================================================

>_______________________________________________
>freebsd-questions@... mailing list
>http://lists.freebsd.org/mailman/listinfo/freebsd-questions
>To unsubscribe, send any mail to "freebsd-questions-unsubscribe@..."
>
>--
>This message has been scanned for viruses and
>dangerous content by MailScanner, and is
>believed to be clean.

--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

_______________________________________________
freebsd-questions@... mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscribe@..."

Re: Cron Question

by ElihuJ :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks for the responses. I'm trying to use these lockfile scripts, but I'm not sure on how to go about doing so. Is this something that I add to my current script? Also how can I tell what the running file is named? For example the "/tmp/mrtg.lck" when running MRTG. Thank you again.

Re: Cron Question

by ElihuJ :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Can anyone help me with this? Thank you again.