From 44feaff4622689c3dc71c1e053088cdf540e0b14 Mon Sep 17 00:00:00 2001 From: hadaq Date: Wed, 28 Jan 2009 19:27:48 +0000 Subject: [PATCH] restart script on remote machine(s). Script name is given as an argument. Sergey. --- plugins/restart.pl | 91 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100755 plugins/restart.pl diff --git a/plugins/restart.pl b/plugins/restart.pl new file mode 100755 index 0000000..3ae83d7 --- /dev/null +++ b/plugins/restart.pl @@ -0,0 +1,91 @@ +#!/usr/bin/perl -w + +######################################################## +# restart script on a given list of PCs # +# # +# Sergey Yurevich # +######################################################## + +use strict; +use warnings; +use Getopt::Std; + +our ($opt_s, $opt_p, $opt_k, $opt_h); +getopts('hs:p:k'); + +if($opt_h){ + &showHelp(); + exit(0); +} + +&checkArgs(); + +&main(); + +exit(0); + +#---------------- END --------------------- + +sub main +{ + + #--- on these PCs the script should be restarted: + my @PCList = ('lxg0429','lxg0433','lxg0435','lxg0437','lxg0439','lxg0440','lxg0441','lxg0442','lxg0443','lxg0444','lxg0445','lxg0446','lxg0447','lxg0448','lxg0452','lxg0453','lxg0454','lxg0455'); + + + foreach my $PC (@PCList) { + + print "restart on $PC ...\n"; + + if($opt_k){ + #--- first kill then restart + my $command = "ssh $PC \"pidof -x $opt_s\" "; + my $out = `$command`; + chomp($out); + + #--- is there anything to kill? + if($out){ + system("ssh $PC \"pidof -x $opt_s | xargs kill -9; sleep 1; $opt_s -d\" "); + } + else{ + system("ssh $PC \"$opt_s -d\" "); + } + } + else{ + #--- restart without killing + my $command = "ssh $PC \"$opt_s -d\" "; + system($command); + } + } +} + +sub checkArgs +{ + if( !defined($opt_s) ){ + print "You must provide -s option with arguments!\n"; + print "Read help: restart.pl -h\n"; + exit(0); + } +} + +sub showHelp +{ + print << 'EOF'; + + Restart Script + + This script restarts a script given as an argument. + The script is restarted on a list of machines listed + in @PCList array. Password-less SSH is used to login + to remote PCs, thus, you must set up private/public keys! + The remote script will be restarted under your 'user name'. + + Usage: restart.pl -s [-k] [-h] + + -s : Name of script to be restarted. Name must + contain a path to a script on remote PC! + -k : Try to kill a running script before restarting. + -h : Print this help. + +EOF +} -- 2.43.0