Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

cron job duplicate process by itself again and again

Posted on 2014-12-10
4
Medium Priority
?
462 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1000 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 38

Accepted Solution

by:
Gerwin Jansen, EE MVE earned 1000 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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

The purpose of this article is to show how we can create Linux Mint virtual machine using Oracle Virtual Box. To install Linux Mint we have to download the ISO file from its website i.e. http://www.linuxmint.com. Once you open the link you will see …
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

650 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