Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

shell script using fork()

33 views
Skip to first unread message

smileso...@gmail.com

unread,
Sep 17, 2013, 12:34:50 PM9/17/13
to
Hi,
I want to write a shell script which does the following:

main
|
|--Which connects to different unix machines and execute a command create a file" parallely
|

I think we can do fork() and in the fork() I can connect to the client with ssh and touch(create) the file in the machines.

I am new to shell programming. Can anybody knows how to do it in shell script and have example of it to start with?

Regards
Pradeep

Jyrki Tikka

unread,
Sep 20, 2013, 5:12:32 PM9/20/13
to
smileso...@gmail.com writes:

> Hi,
> I want to write a shell script which does the following:
>
> main
> |
> |--Which connects to different unix machines and execute a command
> | create a file" parallely
> |

This can be as simple as:

for NODE in `cat nodenames` # or any other way to list the nodes
do
ssh $NODE touch /whatever/file/you/want &
done

--
jmt

Martijn Lievaart

unread,
Sep 21, 2013, 2:15:10 PM9/21/13
to
And maybe add a wait after it.

M4

Ignoramus23452

unread,
Oct 19, 2013, 10:09:20 AM10/19/13
to
I wrote a perl script that reads its input, and uses it as commands to
execute in parallel. It tries to guess the maximum number of parallel
jobs to run at any time, or accepts a --parallel argument telling it
how many jobs is the maximu,.

#!/usr/bin/perl

#
# This script reads its standard input, or from files given on
# command line (<>).
#
# It executes every line as a separate shell command. If --parallel
# argument is given, it executes as many jobs in parallel as
# possible, but no more tham "--parallel" at any given time.
#
# This can be helpful to speed up some tasks.
#
# The default level of parallel is based on the number of CPUs.
#
# You need module Parallel::ForkManager. It is available as
# an ubuntu package.
#
# Copyright(C) Igor Chudov, 2009. All rights reserved.
# This script is made available to the public under the latest
# GPL Version found at http://www.gnu.org/licenses/gpl.html
#
# No warranty is given or implied. Refunds will not be provided.
#
# Igor Chudov, http://igor.chudov.com/
#

use strict;
use warnings;

use Getopt::Long;
use Parallel::ForkManager;

my $parallel = undef;

GetOptions(
"parallel=i" => \$parallel,
);

unless( defined $parallel ) {
if( open( CPU, "/proc/cpuinfo" ) ) {
$parallel = 0;
while( <CPU> ) {
$parallel++ if /^processor\b/;
}
close( CPU );
} else {
$parallel = 0;
}
}

my $pm = new Parallel::ForkManager( $parallel );

while( <> ) {
chomp;
my $pid = $pm->start and next;
system( $_ );
$pm->finish;
}

$pm->wait_all_children;

smileso...@gmail.com

unread,
Dec 1, 2013, 4:22:30 AM12/1/13
to
Thanks for your help.Will it work with fedora and linux also?
0 new messages