Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 04-17-2009, 02:39 AM   #1
Techno Guy
Registered: Dec 2008
Posts: 59

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:

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 "" 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?

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

/ $file &

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

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

Rep: Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546
# Find processes by commandname, PIDs in array:
PROCS=($(pgrep /;
# 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 03:12 AM.
Old 04-17-2009, 03:14 AM   #3
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Nope. That's not the correct way to use the wait statements. You have to do something like
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:
inotifywait --monitor --format %f -e moved_to -e create /demo/ | while read file
  # Increment the count by one

  echo "New files moved in $file";
  / $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 | wc -l) -eq 3 ]
    # 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}
Old 04-21-2009, 07:58 PM   #4
Techno Guy
Registered: Dec 2008
Posts: 59

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:
 if [ $(ps h -C | 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
 if [ $(ps h -C | 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


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 12:37 AM
Start a script after GDM autologin, don't quit when script finishes Plastech Linux - Newbie 2 05-29-2007 10:15 AM
KDE Wifimanager starting 4+ times on startup Cohobin Linux - Software 0 09-11-2004 10:14 AM
Faster starting times for a 366mHz laptop. caesius_01 Linux - Software 5 03-20-2004 07:28 PM
High I/O Wait times and TCP buffers? oswald21 Linux - General 0 08-26-2003 10:22 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:39 PM.

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