A script that spawns multiple instance of itself.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting A script that spawns multiple instance of itself.
# 1  
Old 01-30-2011
A script that spawns multiple instance of itself.

Hi all,I have some questions of forking new process,the code is below.Any help will be appreciated.
Code:
  1 #! /bin/bash
  2 
  3 PIDS=$(pidof sh $0)
  4 P_array=( $PIDS )
  5 echo $PIDS
  6 let "instances = ${#P_array[*]}-1"
  7 
  8 echo "$instances instance(s)" of this script running."
  9 echo "[Hit Ctl-C to exit.]";echo
 10 
 11 sleep 1
 12 sh $0

On line 3,in my opinion,it should recursive forever because sh execute the script again,the following will never be run,however,the code did exit if i omit the code below line 3.Second question,why subtract 1 on line 6?Thx.
# 2  
Old 01-31-2011
What is this script supposed to do?
Code:
PIDS=$(pidof sh $0)

creates a child process; I do not see any wait command. What is it you want the script to do?

One standard way to create multiple children and wait for them:
Code:
#!/bin/bash
./myscript.sh arg1 &
./myscript.sh arg1 &
./myscript.sh arg1 &
wait

This creates three children, lets them run, and will not exit until all of them have completed.
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 01-31-2011
Thanks for your reply.My code is a example code from a book.Probably,it wants to show how to use pidof and how "child process" works.What i can't understand is "pidof sh $0",it starts a new process.why didn't the script goes to execute the new process then went into a endless recursive?
# 4  
Old 02-01-2011
pidof is a program that returns pids that are already running. A child process is needed to get pidof running. Then pidof scans the running processes are reports its findings then exits. It does not start anything else. Read the man page for pidof.
This User Gave Thanks to Perderabo For This Post:
# 5  
Old 02-01-2011
clear,Thanks all of you!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To trigger script on particular instance

Hi Guys, I have a main_script.sh which runs every day and scheduled in crontab. in the main script i read data from config file test.config apple mango orange main_script.sh for i in `cat test.config` do if then echo 'Apple' (3 Replies)
Discussion started by: Master_Mind
3 Replies

2. UNIX and Linux Applications

Configuring mysql for multiple instance only

Hello. I plan to use mysql with only instance database so I can stop one database for maintenance without stopping every thing. When one reads through the my.cnf config file, it is not clear if we must use at the same time a single database mysql plus any instances mysqld2 (for app1), mysqld3... (1 Reply)
Discussion started by: jcdole
1 Replies

3. Shell Programming and Scripting

No lock file: Preventing multiple instance of a script

I've been bitten by using a lock or pid file to prevent multiple instances of a script. A user typed kill -9, and the pid file didn't go away. You can't trap -9. So when he tried to restart, it said "already running", and I got trouble report. Argh. So here's what we came up with: # Stop if... (1 Reply)
Discussion started by: McFadden586
1 Replies

4. Shell Programming and Scripting

Multiple instance in tomcat

I need to install a tomcat6 with multiple instances like instance1,instance2 and instance3 in a server. I came to know that for that we need to install tomcat6,apache2.0,mod_jk1.2 and jre with tools.jar installed.And we need to create multiple instances with same web.xml and difference... (0 Replies)
Discussion started by: tuxslonik
0 Replies

5. UNIX for Advanced & Expert Users

Multiple Instance of Unix Shell Script

Hi All, I have a problem mentioned below. I have a script which performs line by line operations on several files. I have a temp_file storing the list of names of the file to be validated. Right not in while loop i validate these files one by one. Is there anyway that i can modify... (1 Reply)
Discussion started by: amitaryans
1 Replies

6. Shell Programming and Scripting

Help with multiple instance script checking

I am trying to debug the following script. It appears that when the check for script running occurs, it's finding the actual grep statement and causing the script believe the script is already running. This is deployed on two different servers where one works fine, the other doesn't. Any ideas? ... (2 Replies)
Discussion started by: DaddyMoose
2 Replies

7. UNIX for Advanced & Expert Users

Multiple Instance Of Same Process

Hi Everyone, I am using solaris 5.10. I have a java process running in server mode in unix. The problem is that it automatically forks i.e creates a child process. I mean suddenly two instances of that process start running , in which the process-id of first instance is the parent... (5 Replies)
Discussion started by: glamo_2312
5 Replies

8. UNIX for Dummies Questions & Answers

Multiple instance of same process

;)Hi Everyone, I am using solaris 5.10. I have a java process running in server mode in unix. The problem is that it automatically forks i.e creates a child process. I mean suddenly two instances of that process start running , in which the process-id of first instance is the parent... (0 Replies)
Discussion started by: glamo_2312
0 Replies

9. Shell Programming and Scripting

replace first instance(not first instance in line)

Alright, I think I know what I am doing with sed(which probably means I don't). But I cant figure out how to replace just the first occurance of a string. I have tried sed, ed, and grep but can't seem to figure it out. If you have any suggestions I am open to anything! (3 Replies)
Discussion started by: IronHorse7
3 Replies

10. Shell Programming and Scripting

Multiple PHP sessions within the same browser instance

Dear all..... I am currently writing a Help-Desk / Knowledge Base application using PHP/PostGreSQL. I authenticate the user using a quite elaborate mechanism of cookies. The problem is that using cookies (I also have a version using sessions with the same problem), I can only seem to get one... (4 Replies)
Discussion started by: zazzybob
4 Replies
Login or Register to Ask a Question