fork processes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting fork processes
# 8  
Old 02-29-2012
Code:
$ cat ssh.sh

#!/bin/bash

MAXPROCS=2
PROCS=$(seq 1 $MAXPROCS)

function ssh_hosts # Reads hosts from stdin, writes output to stdout
{
        while read HOST
        do
                ssh "$HOST" printf "$HOST" ';' uptime </dev/null
        done
}

# Loop until we run out of lines
while [ -z "$FINISHED" ]
do
        # Read a line for each thread and feed it into the right file
        for N in $PROCS
        do
                if read LINE
                then
                        echo "$LINE">>/tmp/$$-$N
                        continue
                fi

                FINISHED=1
                break
        done
done < hosts1

for N in $PROCS
do
        # Add these to the list of tempfiles to delete on exit
        FILES="$FILES /tmp/$$-out$N /tmp/$$-$N"

        # Make a process reading from /tmp/$$-$N and writing to /tmp/$$-out$N
        ssh_hosts < /tmp/$$-$N > /tmp/$$-out$N &
done

# Delete all temporary files on exit, even if we quit with ctrl-c
trap "rm -f $FIFOS $FILES" EXIT

wait    # Wait for readers to quit

cat /tmp/$$-out*      # Read all output to stdout

$ cat hosts1

172.16.0.154
172.16.0.62
172.16.0.222
172.16.0.182
172.16.0.90
172.16.0.226
172.16.0.118
172.16.0.158
172.16.0.50
172.16.0.130
172.16.0.86

$ ./ssh.sh

172.16.0.154 11:10:32 up 6 days, 21:36,  0 users,  load average: 0.01, 0.02, 0.00
172.16.0.222 11:18:44 up 15 days, 21:26,  0 users,  load average: 0.03, 0.03, 0.00
172.16.0.90 10:50:28 up 2 days, 26 min,  0 users,  load average: 0.06, 0.04, 0.00
172.16.0.118 11:04:55 up 12 days, 23:12,  0 users,  load average: 0.11, 0.07, 0.02
172.16.0.50 10:51:34 up 47 days, 19:47,  0 users,  load average: 0.04, 0.04, 0.00
172.16.0.86 11:02:14 up 22 days,  1:52,  0 users,  load average: 0.01, 0.01, 0.00
172.16.0.62 11:24:51 up 21 days,  2:27,  0 users,  load average: 0.00, 0.00, 0.00
172.16.0.182 10:15:50 up 2 days, 25 min,  0 users,  load average: 0.02, 0.04, 0.00
172.16.0.226 11:17:48 up 6 days, 21:37,  0 users,  load average: 0.00, 0.00, 0.00
172.16.0.158 11:21:46 up 2 days, 24 min,  0 users,  load average: 0.08, 0.02, 0.01
172.16.0.130 10:57:22 up 5 days, 19:42,  0 users,  load average: 0.00, 0.00, 0.00

$

This User Gave Thanks to Corona688 For This Post:
# 9  
Old 02-29-2012
Thanks, can you tell me how to do it in perl.
# 10  
Old 02-29-2012
how about my answer in the other thread you didn't read.
# 11  
Old 02-29-2012
This is all becoming confusing. I'm closing this thread. Please continue in the other one.

https://www.unix.com/shell-programmin...fork-perl.html

Anjan1, you have now three threads with the same title. Please start using descriptive titles. Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Making processes using fork

Can anyone help me with this? Create a parent process that gets from the command line n arguments arg1, arg2, ... , argn. The parent will create n/3 son processes, each of them will create a file with the name argi by concatenate the files argi+1 and argi+2. How can i concatenate those... (1 Reply)
Discussion started by: bunicu01
1 Replies

2. Programming

Issue when fork()ing processes

Hi guys! I'll simplify my problem. I have the following code: #include <fcntl.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <signal.h> #include <fcntl.h> #include <unistd.h> #include <sys/wait.h> #define max 25 #define buffdim 50 void p1(); void p2();... (2 Replies)
Discussion started by: pfpietro
2 Replies

3. Programming

Creating more processes with fork()

Hello people I need help How to make ONE process to create MORE (not one) processes with fork(). I tried several codes but do not work. Thanks (8 Replies)
Discussion started by: nekoj
8 Replies

4. Programming

fork(), parent and child processes???

Hi friends, I have a small question regarding unix system call fork, I hope you will solve my problem. Here is the small program $ cat fork1.c #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { int pid; int x = 0; x = x + 1; pid = fork(); if(pid < 0) {... (2 Replies)
Discussion started by: gabam
2 Replies

5. UNIX for Dummies Questions & Answers

Conserving processes: execve() vs fork()

Disclaimer: This is just a rainy day experiment. There is no expected "goal" other than to understand UNIX better. After reading about fork and exec, my understanding is that forking, as the UNIX shell does by design, consequentially may sacrafice some speed versus an approach that runs in... (1 Reply)
Discussion started by: uiop44
1 Replies

6. SCO

-sh: fork failed - too many processes in sco unix 5.0.5

Dear experts, I have done a re-installation of sco unix openserver 5.0.5 and managed to create users. The problem am facing is that of one user logging in more than 5 times. How can i overcome this problem. the system give the error below. -sh: fork failed - too many processes in sco unix... (5 Replies)
Discussion started by: njoroge
5 Replies

7. Solaris

Identifying and grouping OS processes and APP processes

Hi Is there an easy way to identify and group currently running processes into OS processes and APP processes. Not all applications are installed as packages. Any free tools or scripts to do this? Many thanks. (2 Replies)
Discussion started by: wilsonee
2 Replies

8. Shell Programming and Scripting

fork() and child processes

Hello, How many child processes are actually created when running this code ? #include <signal.h> #include <stdio.h> int main () { int i ; setpgrp () ; for (i = 0; i < 10; i++) { if (fork () == 0) { if ( i & 1 ) setpgrp () ; printf ("Child id: %2d, group: %2d\n",... (1 Reply)
Discussion started by: green_dot
1 Replies

9. Programming

fork() and child processes

Hello, How many child processes are actually created when running this code ? #include <signal.h> #include <stdio.h> int main () { int i ; setpgrp () ; for (i = 0; i < 10; i++) { if (fork () == 0) { if ( i & 1 ) setpgrp () ; printf ("Child id: %2d, group: %2d\n", getpid(),... (0 Replies)
Discussion started by: green_dot
0 Replies

10. UNIX for Advanced & Expert Users

Monitoring Processes - Killing hung processes

Is there a way to monitor certain processes and if they hang too long to kill them, but certain scripts which are expected to take a long time to let them go? Thank you Richard (4 Replies)
Discussion started by: ukndoit
4 Replies
Login or Register to Ask a Question