Solved

cron job duplicate process by itself again and again

Posted on 2014-12-10
4
358 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
ID: 40492276
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
ID: 40492322
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
ID: 40492415
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
ID: 40492545
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
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 match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

896 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