Question : Limit concurrent instances of application

Hello,

Is there a way to limit concurrent instances of an application? Specifically, I'd like to constrain the number of cvs instances that can run concurrently.

Thanks

Answer : Limit concurrent instances of application

There is no built-in way to limit the number of process instances.

You could help yourself with a little trick:

# rename cvs, adapt the path if necessary
mv /usr/bin/cvs /usr/bin/cvs.bin


Create a script that will run CVS, named /usr/bin/cvs

--------------- >8 ---------------
#!/bin/bash

lockfile=/var/lock/cvs_instances.lock
max_instances=5

# get number of running instances
if [ -r $lockfile ]; then
    num_instances=`awk '{printf $1}' $lockfile`
else
    num_instances=0
fi

# check if max is reached
if [ $num_instances -lt $max_instances ]; then
# if not, increase the number by 1
    awk '{printf("%d",$1+1)}' $lockfile >$lockfile
# run the real cvs binary with all parameters passed to the script
    exec /usr/bin/cvs.bin "$@"
# and decrease the number of running instances after the cvs has exited.
    awk '{printf("%d",$1-1)}' $lockfile >$lockfile    
fi
--------------- >8 ---------------

# Make sure the script has the right permissions:
chown root:root /usr/bin/cvs
chmod 0744 /usr/bin/cvs


Now you should be able to limit your number of cvs instances. It is configurable by changing the variable $max_instances in the script.
Random Solutions  
 
programming4us programming4us