Crontab looping on linux unix

libertyforall2
libertyforall2 used Ask the Experts™
on
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.7/FAQ
  PID TTY      STAT   TIME COMMAND
12146 ?        S      0:00 crond
12147 ?        Ss     0:00 /bin/sh /share/huina/rhuff/hysplit/hysplit/kilauea/wo
12151 ?        S      0:00 /usr/sbin/sendmail -FCronDaemon -i -odi -oem -oi -t
12152 ?        S      0:00 sh switchscript12.sh
12155 ?        S      0:00 /usr/sbin/postdrop -r
12160 ?        S      0:00 sh getscript12.sh
12205 ?        S      0:00 crond
12206 ?        Ss     0:00 /bin/sh /share/huina/rhuff/hysplit/hysplit/kilauea/wo
12211 ?        S      0:00 /usr/sbin/sendmail -FCronDaemon -i -odi -oem -oi -t
12213 ?        S      0:00 sh switchscript12.sh
12218 ?        S      0:00 /usr/sbin/postdrop -r
12223 ?        S      0:00 sh getscript12.sh
12277 ?        S      0:00 sshd: rhuff@notty
12278 ?        Ss     0:00 csh -c /usr/libexec/openssh/sftp-server
12300 ?        S      0:00 /usr/libexec/openssh/sftp-server
12303 ?        S      0:00 crond
12304 ?        Ss     0:00 /bin/sh /share/huina/rhuff/hysplit/hysplit/kilauea/wo
12308 ?        S      0:00 /usr/sbin/sendmail -FCronDaemon -i -odi -oem -oi -t
12309 ?        S      0:00 sh switchscript12.sh
12312 ?        S      0:00 /usr/sbin/postdrop -r
12317 ?        S      0:00 sh getscript12.sh
12361 ?        S      0:00 crond
12362 ?        Ss     0:00 /bin/sh /share/huina/rhuff/hysplit/hysplit/kilauea/wo
12366 ?        S      0:00 /usr/sbin/sendmail -FCronDaemon -i -odi -oem -oi -t
12367 ?        S      0:00 sh switchscript12.sh
12370 ?        S      0:00 /usr/sbin/postdrop -r
12375 ?        S      0:00 sh getscript12.sh
12424 ?        S      0:00 crond
12425 ?        Ss     0:00 /bin/sh /share/huina/rhuff/hysplit/hysplit/kilauea/wo
12429 ?        S      0:00 /usr/sbin/sendmail -FCronDaemon -i -odi -oem -oi -t
12430 ?        S      0:00 sh switchscript12.sh
12432 ?        S      0:00 /usr/sbin/postdrop -r
12438 ?        S      0:00 sh getscript12.sh
12488 ?        S      0:00 crond
12489 ?        Ss     0:00 /bin/sh /share/huina/rhuff/hysplit/hysplit/kilauea/wo
12493 ?        S      0:00 /usr/sbin/sendmail -FCronDaemon -i -odi -oem -oi -t
12494 ?        S      0:00 sh switchscript12.sh
12496 ?        S      0:00 /usr/sbin/postdrop -r
12502 ?        S      0:00 sh getscript12.sh
12556 ?        S      0:00 crond
12557 ?        Ss     0:00 /bin/sh /share/huina/rhuff/hysplit/hysplit/kilauea/wo
12561 ?        S      0:00 /usr/sbin/sendmail -FCronDaemon -i -odi -oem -oi -t
12562 ?        S      0:00 sh switchscript12.sh
12563 ?        S      0:00 /usr/sbin/postdrop -r
12570 ?        S      0:00 sh getscript12.sh
12589 ?        S      0:00 sshd: rhuff@pts/1
12590 pts/1    Ss     0:00 -csh
12650 ?        S      0:00 crond
12651 ?        Ss     0:00 /bin/sh /share/huina/rhuff/hysplit/hysplit/kilauea/wo
12655 ?        S      0:00 /usr/sbin/sendmail -FCronDaemon -i -odi -oem -oi -t
12656 ?        S      0:00 sh switchscript12.sh
12658 ?        S      0:00 /usr/sbin/postdrop -r
12664 ?        S      0:00 sh getscript12.sh
12685 ?        S      0:06 wget http://mkwc.ifa.hawaii.edu/tiziana/rhuff/2010101
12717 ?        S      0:06 wget http://mkwc.ifa.hawaii.edu/tiziana/rhuff/2010101
12719 pts/1    S+     0:00 /bin/vi /tmp/crontab.XXXXJrDMmf
12721 ?        S      0:00 crond
12722 ?        Ss     0:00 /bin/sh /share/huina/rhuff/hysplit/hysplit/kilauea/wo
12726 ?        S      0:00 /usr/sbin/sendmail -FCronDaemon -i -odi -oem -oi -t
12727 ?        S      0:00 sh switchscript12.sh
12729 ?        S      0:00 /usr/sbin/postdrop -r
12735 ?        S      0:00 sh getscript12.sh
12740 ?        S      0:06 wget http://mkwc.ifa.hawaii.edu/tiziana/rhuff/2010101
12755 ?        S      0:03 wget http://mkwc.ifa.hawaii.edu/tiziana/rhuff/2010101
12782 ?        S      0:03 wget http://mkwc.ifa.hawaii.edu/tiziana/rhuff/2010101
12784 ?        S      0:02 wget http://mkwc.ifa.hawaii.edu/tiziana/rhuff/2010101
12786 ?        S      0:00 crond
12787 ?        Ss     0:00 /bin/sh /share/huina/rhuff/hysplit/hysplit/kilauea/wo
12791 ?        S      0:00 sh switchscript12.sh
12792 ?        S      0:00 /usr/sbin/sendmail -FCronDaemon -i -odi -oem -oi -t
12793 ?        S      0:00 /usr/sbin/postdrop -r
12800 ?        S      0:00 sh getscript12.sh
12805 ?        S      0:02 wget http://mkwc.ifa.hawaii.edu/tiziana/rhuff/2010101
12817 ?        S      0:01 wget http://mkwc.ifa.hawaii.edu/tiziana/rhuff/2010101
12821 ?        S      0:01 wget http://mkwc.ifa.hawaii.edu/tiziana/rhuff/2010101
12825 ?        S      0:00 sshd: rhuff@pts/2
12826 pts/2    Ss     0:00 -csh
12866 ?        S      0:00 wget http://mkwc.ifa.hawaii.edu/tiziana/rhuff/2010101
12885 pts/2    R+     0:00 ps -x
[rhuff@huina ~]$

There seems to be some kind of loop in my crontab. It all started when I accidentally deleted my cronjob and had to restart it. I thought perhaps I set it up run run every minute at 2 and 4 pm instead of just 2am & 2pm. I know how to delete the job and I can kill the crontab with crontab -r but it is possible there is a crontab installed somewhere accidentally that duplicates the job?

This is my crontab below. Also, besides seeing the current programs running with ps, it is possible to see if there are other cronjobs somewhere?

* 2,14 * * * /bin/sh /share/huina/rhuff/hysplit/hysplit/kilauea/working/kilaueabatch.sh
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Is there a way to use the crontab to run say a script and earlier versions of the script are already running then to kill the script so Could I put within the kilaueabatch.sh script itself some code that says, if there are other versions of this script running then kill all others EXCEPT this one? I'm wondering though that if there is something causing the script to loop that it would simply run the script and kill itself over and over again.

I would prefer to know whats causing the looping. I went through the code and Didn't seem to find anything in particular like at the end of kilaueabatch.sh a code that says run kilaueabatch.sh. Also, considering the problem occured when I accidentally deleted then reinstated the cronjob that it is causing this somehow.

Author

Commented:
14317 ?        Ss     0:00 /bin/sh -c /bin/sh /share/huina/rhuff/hysplit/hysplit/kilauea/working/kilaueabatch.sh 2> /share/huin
14318 ?        S      0:00 /bin/sh /share/huina/rhuff/hysplit/hysplit/kilauea/working/kilaueabatch.sh
Consultant
Commented:
Your crontab file does indeed schedule the job to run every minute from 2:00-2:59 and 14:00-14:59. To run it at 2:00 and 14:00, it should read:

0 2,14 * * * /bin/sh /share/huina/rhuff/hysplit/hysplit/kilauea/working/kilaueabatch.sh

Each user authorized to use crontab has a file in /var/spool/cron. The crontab commands only manage your own.

There are also system-wide cron directories:

/etc/cron.d
/etc/cron.hourly
/etc/cron.daily
/etc/cron.monthly
/etc/cron.weekly
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Kerem ERSOYPresident
Commented:
Hi,

It seems that your script takes longer time tiem then one minute to execute. Can't you just run the script say every 10 minutes ?

It takes you to modify your entry:
0/10 2,14 0 2,14 * * * /bin/sh /share/huina/rhuff/hysplit/hysplit/kilauea/working/kilaueabatch.sh

Otherwise you can create a lock mechanism in the log such as:

#/bin/bash

[  -f  /tmp/scriptrunning ]  && exit 1
touch /tmp/scriptruning
.
.
.
.
.
rm /tmp/scriptrunning && exit 0

In the beginning the script will check if a file called /tmp/scriptrunning exists then it the script would terminate. If not it would create the file and continue executing commands. When the script  completes the execution it would delete the file and quit with Status Code of 0. If there's a problem it would quit with exit code.
 
This would lock the script and prevent the execution of another instance.

Cheers,
K.

Author

Commented:
I have indeed fixed the looping problem It is only supposed to run every 12 hours. It actually takes 2-5 hours to run once. Sometimes there is a problem and it runs longer than 12 hours in which case, it needs to be killed. How would I do this?
Kerem ERSOYPresident
Commented:
Hi,

Add the following lines in the beginning of your script

PATH /bin:/usr/bin:$PATH
pgrep $(basename $0) | grep -v $$ | /xargs pkill -9  

This is getting the script name then getting all PID's other than the current and killng the rest.

But I'll suggest you to use the locking mechanism in the previous posting. Because this would kill the process if it is running more than 12 hours but we're not sure this instance would run shorter. This in turn might cause each run would kill the previous running long process and the job could never be finished successfully.

Cheers,
K.

Author

Commented:
If my file being activated is /share/huina/rhuff/hysplit/hysplit/kilauea/working/kilaueabatch.sh how would this be written?
Kerem ERSOYPresident
Commented:
Oops there was 2 typos on my test please use this one:

PATH /bin:/usr/bin:$PATH
pgrep $(basename $0) | grep -v $$ | xargs kill -9  
Kerem ERSOYPresident
Commented:
> If my file being activated is /share/huina/rhuff/hysplit/hysplit/kilauea/working/kilaueabatch.sh how would > this be written?

You don't need to modify anything because $(basename $0) will reduce the name to kilaueabatch.sh.  This is why I've getting the script name automatically instead of hardcoding it.

Cheers,
K.
Kerem ERSOYPresident
Commented:
BTW don't for get to modify your cronjob info with this:

0 2,14 * * * /share/huina/rhuff/hysplit/hysplit/kilauea/working/kilaueabatch.sh

Actually you don't need /bin/sh to be there as long as you prefix the contents of your cron script file with:

#!/bin/bash

Cheers,
K.

Kerem ERSOYPresident
Commented:
You can test the script by modifying the cron job as before so that it would launch an instance every minute. But this time the script will be killing the old instances.

* 2,14 * * * /share/huina/rhuff/hysplit/hysplit/kilauea/working/kilaueabatch.sh

Cheers,
K.

So when you check with "pgrep kilaueabatch.sh" you'll be getting only one pid that is currently running.
Commented:
crontab -e ( allows your to edit current crontab items )

edit with your favorite text editor /etc/crontab
Kerem ERSOYPresident
Commented:
@ReN501

> crontab -e ( allows your to edit current crontab items )
>
> edit with your favorite text editor /etc/crontab

Rhis is RHEL in question and crontab -e won't do for the root user.
Commented:
@KeremE

\crontab -e is a valid command under UNIX security , where this question was placed, i also put the alternative to editing the file directly.

Author

Commented:
Issue resolved

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial