i am trying to run this script.the name of script is final.sh
after i run it:
#./final.sh &
i grep the command
# ps -a | grep bash
and i see more then one processes runing 3!!
how can i solve this problem?
my target script must always run in background and watch there are always
5 process that are runing and if there is less it should get them up untill 5.
#! /bin/bash while true ; do
proc_num=$(ps -a | grep bash | wc -l) while(( "$proc_num" < "5")) ;
do bash
#add a single process done
sleep 10
done
done
#let something else use the box done
"bash" is a shell, not a script. If you search for "bash" in the process list you will find your scripts (because the use "bash" as their environment), but also interactive shells, login shells, other running scripts, etc.. Better use a more specific search term, which specifically finds your scripts instances and nothing else.
Im very busy at the moment so have not much time to go in details...
The output of your ps might have helped us you know... (the complete!)
Look at the line 3 of your script...
I just put your script in order ( for me to read...):
#! /bin/bash
while true
do
proc_num=$(ps a | grep yourscript | wc -l)
echo "loop1: "proc_num=$proc_num
sleep 2
while (( $proc_num < 5 ))
do
echo "loop 2: "proc_num=$proc_num
sleep 2
yourscript & #add a single process
#sleep 1 # - this is for testing purpose... -#
#let proc_num=$proc_num+1
done
echo " out of loop 1 "
sleep 1
done
#let something else use the box done
Comment: ps -a doesnt give the same as ps a
you were missing a space after second while...
My question is more on line launching yourscript: does it go in background like a daemon or not? if it doesnt I see your program being stuck at this point ( but I may be wrong since I dont know what program it is...)
Since I could not test your script not knowing what is being grepped, I added for test purpose what is commented so you can try the increment and so validate your syntax.
I was very sollicitated yesterday and was very tired at the end of day so did not realise that your code had that issue ( since I added an increment in second loop...):
Once entering second loop, you have no means to change your proc_num value!
In fact the afftectation should be also in the second loop:
.
.
while (( $proc_num < 5 ))
do
echo "loop 2: "proc_num=$proc_num
sleep 2
yourscript & #add a single process, ampersand necessary?
#sleep 1 # - this is for testing purpose... -#
#let proc_num=$proc_num+1
proc_num=$(ps a | grep yourscript | wc -l) # otherwise in will never get out...
done
.
.
done