Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


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

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


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

Old 08-18-2005, 04:53 AM   #2
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 5,388

Rep: Reputation: 408Reputation: 408Reputation: 408Reputation: 408Reputation: 408
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, 05:38 AM   #3
Senior Member
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,421

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

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

Old 08-18-2005, 02:49 PM   #5
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: 78
At the top of your script you could put:
check_instances() {
NUM_INSTANCES=$(ps aux | grep simu | wc -l)
if [[ $NUM_INSTANCES -ge 4 ]]; then
        echo "3 simu instances already running..."
        sleep 5

# 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 02:53 PM.


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

All times are GMT -5. The time now is 02:22 AM.

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