LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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, 02: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, 04:53 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

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

Rep: Reputation: 66
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, 02: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: 78
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 02:53 PM.
 
  


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:04 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
Open Source Consulting | Domain Registration