LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-17-2009, 03:39 AM   #1
Techno Guy
Member
 
Registered: Dec 2008
Posts: 53

Rep: Reputation: 15
If a script is already running 3 times wait till one finishes before starting another


Hi, it's me again

I'v been googleing for about the last hour and a bit with little success.

What I want:
This post is using similar code to my other post: http://www.linuxquestions.org/questi...lders.-714468/

Ok so with that other post, all was working fine, until I realized that my server can't really handle any more then 2 or 3 of the "forloop.sh" script running at any one time, and when I had something like 5-7 of them running at once my server pretty much froze (yea I know, I have a pretty crappy server but it gets the job done... eventually )

So I thought, well why not just add a queue in so that say only 3 of them can work at a time, and once one of the 3 have finished (if there are any waiting) start it.

From what I found out from google it seems like the "wait" command would be the way to go?

Thoughts:
Current code:
Code:
inotifywait --monitor --format %f -e moved_to -e create /demo/ | while read file; do echo "New files moved in $file";

/forloop.sh $file &

#something like this??
$!
wait if ($! > 3)....Or something.. I'm not sure how it would go :(


done
Looking forward to some awesome help
 
Old 04-17-2009, 04:10 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529
# Find processes by commandname, PIDs in array:
PROCS=($(pgrep /forloop.sh));
# If array has more than 2 elements then holdThis, else doSomething
[ ${#PROCS} -gt 2 ] && holdThis || doSomething
...maybe something like that?


BTW, you also might want to post the contents of your forloop script. Optimising things might not help a lot but it just might. For instance for some stuff I'll use a shell that is way quicker starting up compared to Bash like Ash.

Last edited by unSpawn; 04-17-2009 at 04:12 AM.
 
Old 04-17-2009, 04:14 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Nope. That's not the correct way to use the wait statements. You have to do something like
Code:
wait $!
where $! is the PID of last job run in background. Since you want to run 3 jobs at once, you can store the PID of each job in a variable, then check the number of running processes and every time it is equal to 3, just wait for the first of the three to complete. Here is an example:
Code:
#!/bin/bash
count=0
inotifywait --monitor --format %f -e moved_to -e create /demo/ | while read file
do
  # Increment the count by one
  ((count++))

  echo "New files moved in $file";
  /forloop.sh $file &

  # Store the pid of last job in a variable (use indirect reference here)
  eval pid_$count=$!

  # Check the number of running processes
  if [ $(ps h -C forloop.sh | wc -l) -eq 3 ]
  then
    # build the name of the variable storing the pid of the first job
    pid=pid_$((count - 2))

    # wait for the job to complete
    wait ${!pid}
  fi
done
 
Old 04-21-2009, 08:58 PM   #4
Techno Guy
Member
 
Registered: Dec 2008
Posts: 53

Original Poster
Rep: Reputation: 15
Thanks colucix!

I gave it a go a few days ago, but it wasn't working proper, so today I had some time again and figured out why it wasn't working.

Turns out this line:
Code:
 if [ $(ps h -C forloop.sh | wc -l) -eq 3 ]
Should be like this (for my prepossess): I changed it so that it isn't equal to but greater then, because once I had more then 3 of that script running they would just start, but changing it to greater then fixed that
Code:
 if [ $(ps h -C forloop.sh | wc -l) -ge 3 ]
Thanks again for your help, I never thought it would be so few lines of code, I would of thought it to be about half a page, but you proved me wrong
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't wait 'till July 5th to the 8th! colinstu General 10 06-13-2007 01:37 AM
Start a script after GDM autologin, don't quit when script finishes Plastech Linux - Newbie 2 05-29-2007 11:15 AM
KDE Wifimanager starting 4+ times on startup Cohobin Linux - Software 0 09-11-2004 11:14 AM
Faster starting times for a 366mHz laptop. caesius_01 Linux - Software 5 03-20-2004 08:28 PM
High I/O Wait times and TCP buffers? oswald21 Linux - General 0 08-26-2003 11:22 AM


All times are GMT -5. The time now is 12:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration