Solved

cron job duplicate process by itself again and again

Posted on 2014-12-10
4
344 Views
Last Modified: 2014-12-10
Dear expert,

I have a question concerning with cron job. this is my code sample:

00 7-20 * * * /home/usr/./test.sh  # test.sh is an executable file

The code runs perfectly. I leave the script on for 4 to 5 hours to see if the script can run without any issue. When I check the process after few hours I see 5 same process running at same time.

I've checked online that flock can "fix" this issue:

I've applied this:

00 7-20 * * * flock -n /home/usr/test.sh -c /home/usr/./test.sh

somehow I got this error: /bin/bash: bad interpreter: Text file busy.

Is there away that I can reduce down the process that only one process is running when I activate my script through cron job? Thanks
0
Comment
Question by:Kinderly Wade
4 Comments
 
LVL 23

Expert Comment

by:savone
Comment Utility
I beleive the problem is your time.

7-20 means running it every hour between 7 and 20.  This would mean a new process is started every hour between those hours.
0
 

Author Comment

by:Kinderly Wade
Comment Utility
Is there a way for me to configure that I want the script to start 7:00 am and stop at 8:pm? THanks
0
 
LVL 4

Assisted Solution

by:Zsolt Pribusz
Zsolt Pribusz earned 250 total points
Comment Utility
You can do it with 2 crontab entry.
First you need to start your script, and then you need to kill it.
You can do this in many ways.
here is two sort example:

#Start:
0 7 * * * /home/usr/test.sh
#End:
0 20 * * * pkill -f test.sh  #This will kill any process with test.sh in its command line 

Open in new window


If you want to be more specific, then modify your test.sh to store it's pid to some file:
Add this line to begining of your script
echo $$ > /tmp/test_sh.pid

Open in new window


then you can kill process based on this file.
 0 20 * * * kill $(cat /tmp/test_sh.pid)

Open in new window

0
 
LVL 37

Accepted Solution

by:
Gerwin Jansen earned 250 total points
Comment Utility
I would not kill the script, it may be busy doing something. I would recommend to first check if the script is running already and if not, create the 'flag' file as suggested above. Then when the script finishes, remove the flag file. You could add some logging at the check part.

FLAGFILE=/tmp/test_sh.flag
# check
if ! [ -r ${FLAGFILE} ]
then
touch ${FLAGFILE} 
else
# add logging here (optional)
exit
fi

# start
(your code here)

# finish
rm -f ${FLAGFILE} 

Open in new window


Just make sure no one cleans the flag file for some reason, you could store it elsewhere.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you use Debian 6 Squeeze and you are tired of looking at the childish graphical GDM login screen that is used by default, here's an easy way to change it. If you've already tried to change it you've probably discovered that none of the old met…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

744 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

15 Experts available now in Live!

Get 1:1 Help Now