LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 08-18-2005, 03:56 AM   #1
paterijk
LQ Newbie
 
Registered: Aug 2005
Posts: 3

Rep: Reputation: 0
bash: run a script at most 3 times simultanously


Hi

I would like a script (let us call it simu) to run at most 3 times simultanously (to avoid overloading my server).

My idea would be to write a second script which would run simu only if less then 3 simus are already running. If 3 simus are running, he should wait for another one to finish, before starting a new one.

Does anyone know how I could do this?

Thank you for your help

Patrick
 
Old 08-18-2005, 05:53 AM   #2
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,617

Rep: Reputation: 136Reputation: 136
Why not use a file to store a number and increment it following the number
of time you start your script ?
 
Old 08-18-2005, 06:38 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
with xinetd you can sepcify how many servers to start.
dunno about inetd.
 
Old 08-18-2005, 10:43 AM   #4
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
The problem you'll have is synchronisation. The solution has to be via some atomic operation, like a mv or a ln for example...

Yves.
 
Old 08-18-2005, 03:49 PM   #5
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 77
At the top of your script you could put:
Code:
check_instances() {
NUM_INSTANCES=$(ps aux | grep simu | wc -l)
if [[ $NUM_INSTANCES -ge 4 ]]; then
        echo "3 simu instances already running..."
        sleep 5
        check_instances
fi
}

check_instances
# the rest of your code here...
Here we check if your script is found 4 times in the process table rather than three, because "ps aux | grep simu | wc -l" is counted itself there. As written this function will check every 5 seconds, but you can change that by modifying the "sleep" value. Once there are less than 3 instances running the rest of your script will run...

There is a possibility for false-positives here (if the string "simu" is found elswhere in your process table) but this will dissappear if you choose a more unique name for your script.

Last edited by bulliver; 08-18-2005 at 03:53 PM.
 
  


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
run-parts gives error on bash script ChoKamir Programming 7 08-18-2014 07:31 AM
Run my bash script as a daemon. jaimese Linux - Newbie 12 02-10-2011 04:28 PM
how to run one bash script from within another? babag Programming 9 04-28-2005 01:12 AM
Bash script to alert by email 3 times then stop. pmpc00 Linux - General 2 11-04-2004 08:23 AM
Run script at login instead of bash uzi4u Linux - General 2 04-28-2004 03:31 PM


All times are GMT -5. The time now is 12:20 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration