perl to shell convert


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl to shell convert
# 1  
Old 09-05-2012
Java perl to shell convert

Code:
#!/usr/bin/perl 
#**************************************************************
#
#                        PERL SCRIPT
# This script restarts the  Application
#
#
##**************************************************************
 
$ENV{'IFS'} = ' ';
#open(STDERR,">&STDOUT");
#select STDOUT; 
$|=1;
 
push(@INC, "/usr/share/perl/5.6.1");
push(@INC, "/usr/share/perl/5.8.0");
use Getopt::Long; 
use FileHandle;
use sigtrap 'handler' => \&signal_handler, 'normal-signals', 'error-signals', 'old-interface-signals';
use Errno qw(EAGAIN);

@optl = ("w=i", "p=i", "log=s");
 
GetOptions @optl;

chomp($echo      = `which echo`);
chomp($grep      = `which grep`);
chomp($ps	 = `which ps`);
chomp($evlsend   = `which evlsend`);
$evt_success     = "iden.ha.mpm.start_app";
$evt_failure     = "iden.ha.mpm.stop_app_failure";
$ospf_script     = "/usr/app/ha/admin/configureOSPF.sh";
$ospf_option     = "-s";
$dhcp_script     = "/etc/init.d/dhcp";
$dhcp_option     = "stop";
$tftp_script     = "/usr/app/ha/admin/tftp_setup.pl";
$tftp_option     = "--notftpd";
$sleep_time      = 2;
$retries         = 3;
$kill_success    = 0;
$failure         = 0;
$p_ppid          = getppid;
$c_ppid          = $$;
$start_app       = "/usr/app/ha/admin/start_app.pl";
$scr_log_dir     = "/usr/app/ha/tmp";
$exit_status     = 1;
$ckpt_clean      = "/usr/app/ha/admin/ckptclean.pl";
$stop_app_file   = "/usr/app/config/stop_app_task";
$stop_app_precfg = "/usr/app/config/stop_app_preconfig";
$ha_cleanup      = "/usr/app/ha/admin/ha_cleanup.pl";
$max_app_cfg     = 8;
chomp($killall	 = `which killall`);
chomp($pidof	 = `which pidof`);
$log_file        = "$scr_log_dir/restart_app.$$";
$app_log         = "/dev/null";
chomp($priocntl  = `which priocntl`);
$platform_type      = &get_install_value("PT_PLATFORM_TYPE", "cx2600");

if ($platform_type eq "ATCA-7367") {
    $dhcp_script        = "/etc/init.d/dhcpd";
}

`rm -rf \`find $scr_log_dir -name 'restart_app.*' -mtime +7 -print\``;
open(FILE, ">$log_file");
$ha_cleanup      = "/usr/app/ha/admin/ha_cleanup.pl";
autoflush FILE 1;

if($opt_log)
{
   $app_log = $opt_log;
   print  FILE "logging out to file ($app_log)...";
}
open(STDOUT, ">$app_log");
open(STDERR, ">&STDOUT");
FORK: {
    if($pid = fork) {
	if( $opt_w ) {
	    print FILE "waiting for child to complete...\n";
	    wait;
	    exit($?);
	}
#Return exit status of child
	exit(0);
    }elsif ($! == EAGAIN) {
	sleep 5;
	redo FORK;
    }elsif (defined $pid) { #I'm the child


# If I was not called with the -w option, it means that I was called
# from MPMT. Therefore I need to check and see whether a start_app.pl
# is running, if it is, I need to wait for a bit of time until the
# buff pools etc are created completely before I will kill all of it,
# otherwise, for some reason, the tasks do not get killed effectively.
#

           chomp(@start_script = `$ps ax | grep $start_app | grep -v grep | awk '{print \$1}'`);
           if(@start_script) {
	           $ts=localtime();
	           print FILE "$ts: Killing $start_app(@start_script)\n";
	           kill ("KILL", @start_script);
           }
#Kill PST
           chomp($pst = `$pidof pst`);
           if($pst) {
	           $ts=localtime();
               &run_script("$ts:KILL PST", "$killall -s KILL pst");
           }
# Check if another restart_app.pl process is running. If so, then sleep
# until the process dies or 120 seconds have passed. If latter, exit.
	&wait_for_process($0, 5, 12, 1);

#Call switchover script if it exists
        if( -e $ospf_script ) {
	    &switchover();
	}

#Stop the dhcp server on this card
        if( -e $dhcp_script) {
            my($script) = $dhcp_script . " " . $dhcp_option;
            &run_script("Stopping dhcp server", $script);
        }

#Stop the tftp server on this card
        if( -e $tftp_script) {
            my($script) = $tftp_script . " " . $tftp_option;
            &run_script("Stopping dhcp server", $script);
        }

#Call appropriate stop_app scripts to stop the app
        &post_event("iden.ha.stop_app");
	$count = 0;
	$count_succ = 0;
	for($i = 1; $i <= $max_app_cfg; $i++) {
	    $tmp_var = "";
	    $tmp_var = $stop_app_file . "$i";
	    if( -e $tmp_var ) {
		$count++;
		if( ($rv = &run_script("STOP APP $i", $tmp_var)) < 0 ){
		    print FILE "$tmp_var returned error [$rv]\n";
		    $failure++;
		}else{
		    $count_succ++;
		    print FILE "$tmp_var returned $rv success!\n";
		}
	    }
	}

        # All the processes are killed by now, lets cleanup /tmp
	#Check for preconfig script and run it if it exists
	if( -e $stop_app_precfg ) {
	    $mypid = $$;
	    $count = 0;
	    open(PRIO, "$priocntl -d $mypid|");
	    while(<PRIO>) {
		if($count == 1) {
		    $count++;
		}elsif($count == 2) {
		    chop;
		    @slist = split(/\s+/);
		    $slist[2] =~ s/SCHED_//g;
		    last;
		}elsif(/CLASS/) {
		    $count++;
		}
	    }
	    close(PRIO);

	    if( ($rv = &run_script("PRE-STOP sched_policy", "$priocntl -c OTHER -p 0 $mypid")) < 0) {
		print FILE "pre-stop sched_policy failed!!!\n";
		$sched=0;
	    }else{
		$sched++;
	    }
	    if( ($rv = &run_script("PRECFG", $stop_app_precfg)) < 0 ) {
		print FILE "$stop_app_precfg returned error [$rv]\n";
		$failure++;
	    }else{
		printf FILE "$stop_app_precfg returned success!\n";
	    }
	    if($sched == 1) {
		$rv = &run_script("POST-stop sched_policy", "$priocntl -c $slist[2] -p $slist[3] $mypid");
		if($rv != 0) {
		    print FILE "running POST-stop sched failed,	exiting [$rv]\n";
		    $failure++;
		}
	    }
	}

	if( -e $ckpt_clean ) {
	    if( (&run_script("Cleanup checkpoint", $ckpt_clean)) < 0 ) {
	        print FILE "ERROR: $ckpt_clean script failed...\n";
	    }
	}
	if( -e $ha_cleanup ) {
	    if( (&run_script("HA Cleanup checkpoint", $ha_cleanup)) < 0 ) {
	        print FILE "ERROR: $ha_clean script failed...\n";
	    }
	}

	if( !$failure ) {
	    $event = $evt_success;
	    $exit_status = 0;
	}else{
	    $event = $evt_failure;
	    $exit_status = -1;
	}

	if( $opt_w eq "" ) {
	    &post_event($event);
	}

	close(FILE);
	exit($exit_status);
    }else{
	print FILE "weird fork error $!\n";
	exit(-1);
    }
}

sub check_processes {
    my($str, $arr_ref) = @_;
    my($pids) = "";
    my($ps_line) = "";
    my(@arr) = @$arr_ref;
    my($val);

# This block eliminates the shell called from start_app.pl, the parent
# of this child, and the child itself.
# $ppid is the process id of the grand parent of the child (i.e. it
# could either be MPMT which calls the stop_app.pl or the shell called
# from start_app.pl which calls it as system(stop_app.pl).
# $c_ppid is the process of this forked child's parent which we also
# want to ignore.
#
    $ps_line = "| $grep $str | $grep -v grep ";
    foreach $val (@arr) {
	if( $val ne "" ) {
		$ps_line .= "| $grep -v ";
		$ps_line .= $val;
	}
    }

    open(PS, "$ps $ps_opt $ps_line|");
    
    while(<PS>) {
	chop;
	@ps_list = split(/\s+/, $_);
	$pids .= "$ps_list[1] ";
    }
    close(PS);
    return($pids);
}


sub switchover {
    my($script) = $ospf_script . " " . $ospf_option;
    if( (&run_script("ConfigureOSPF script", $script)) < 0 ) {
	print FILE "ERROR: switching over - $ospf_script returned $?\n";
	$failure++;
    }else{
	print FILE "Switchover successful...\n";
    }
}


sub run_script {
    my($name, $script) = @_;
    my($rc)     = 0;
    my($rv)     = -1;
    my($signal) = 0;
    my($scr);
    my($one);
    my($two);

    ($scr, $one, $two) = split(/\s+/, $script, 3);

    if( (-e $scr) != 1 ) {
	print FILE "ERROR: $script does not exist!!\n";
	return $rv;
    }

    $rc = 0xffff & system $script;
    $ts=localtime();
    printf FILE "$ts system(%s) returned 0x%#04x: \n", $name, $rc;

    if( $rc == 0 ) {
	print FILE "$name returned success...\n";
	$rv = 0;
    }elsif( $rc == 0xffff ) {
	print FILE "ERROR: $name could not execute!!!!\n";
    }elsif( ($rc & 0xff) == 0 ) {
	$rc >>= 8;
	print FILE "ERROR: $name returned with non-zero exit
	status ($rc)!!!\n";
    }else {
	print FILE "ERROR: $name Got signal $rc!!!\n";
    }
    return $rv;
}
    

sub post_event {
    my($event) = @_;
    my($count) = $retries;

    while ($count > 0) {
	$ts=localtime();
	if( system("$evlsend -f iden -t 0x8100 -m '$event'") != 0 ) {
	    print FILE "$ts:ERROR: could not post event $event...\n";
	    $count--;
	}else{
	    print FILE "$ts:Posted event $event\n";
	    $count = 0;
	}
    }
}

sub signal_handler {
    my($sig) = @_;
    
    print FILE "received signal SIG$sig...\n";
    &post_event($evt_failure);
    die;
}


sub wait_for_process {
    my($script, $s_time, $num, $quit) = @_;
    my($pid) = "";
    my($count) = 0;
    my($num_sec) = 0;
    my(@lst) = ("samt", $$, $c_ppid, $p_ppid);
    my($rv)  = -1;

    while(1) {
	$pid = &check_processes($script, \@lst);
	if($pid eq "") {
	    $rv = 0;
	    return($rv);
	}else{
	    print FILE "process $script found, sleeping for $s_time seconds\n";
	    sleep $s_time;
	    $count++;
	}
	if( $count >= $num ) {
	    last;
	}
    }

    $num_sec = $count * $s_time;    
    print FILE "ERROR: slept for longer than $num_sec seconds!!!!!\n";

    if($quit) {
	print FILE "exiting...\n";
	exit(-1);
    }else{
	print FILE "continuing...\n";
	return($rv);
    }
}

sub get_install_value {
  my ($delim, $value) = @_;
  my $path = "/etc";
  my $file = "install_values";

  $path = "/etc/pt" if (-f "/etc/pt/$file" && ! -f "$path/$file");
  $iv = "$path/$file";

  if (-f $iv) {
    open(F, $iv);
    while(<F>) {
      if($_ =~ /$delim=(.+)/) {
        $value = $1;
      }
    }
    close(F);
  }
  return $value;
}


Last edited by Franklin52; 09-05-2012 at 10:21 AM.. Reason: Code tags
# 2  
Old 09-05-2012
There is no effective 'perl to shell converter'. Putting this into a shell script would mean rewriting it from the ground up.

What have you tried?
# 3  
Old 09-06-2012
perl to shell

I don't know shell scripting SmilieSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl to shell convert

HI All, I am new in shell scripting ,can any one please help me to convert this scripts from Perl to Shell. Thank in advance. $customer=$ARGV; $rows=`hadoop fs -ls /ncip/$customer/|wc -l`; if($rows==3){ print "$customer directory exists , cleaning up ! \n"; `hadoop fs -rm... (1 Reply)
Discussion started by: Atul301
1 Replies

2. Shell Programming and Scripting

Convert perl to shell

Hi I am a novice in scripting .Please help me converting perl script into shell my ($ref) = shift; my $id; my $cnt=0; my $first =0; my $filename ; my $filename1 ; FOREACH:foreach my $r (qw(RET LSH PDM )) { $ref->{$cnt}->{num_errors} =0; $ref->{$cnt}->{num_warnings} =0;... (2 Replies)
Discussion started by: apassi
2 Replies

3. Shell Programming and Scripting

Convert shell script to Perl

Hello,,I have a very small script that contains these lines; and it works perfectly; however I need to use Perl now as I will need to feel variables from a MySQL table into this; to it would be nice to start by converting this first... find / -perm 777 \( -type f -o -type d \) -exec ls -lid {}... (1 Reply)
Discussion started by: gvolpini
1 Replies

4. Shell Programming and Scripting

Convert perl program to shell

Hi is there a way that i can convert this simple perl program into shell script #!usr/bin/perl -w use strict; use warnings; open INPUTFILE, "uniqprobecoordinates.out" || die "canot open the file $!"; open OUTPUTFILE, ">", "1_4reads.out"; while(<INPUTFILE>) { chomp; ... (3 Replies)
Discussion started by: bhargavpbk88
3 Replies

5. Shell Programming and Scripting

HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl

Hi all, Not sure if this should be in the programming forum, but I believe it will get more response under the Shell Programming and Scripting FORUM. Am trying to write a customized df script in Perl and need some help with regards to using arrays and file handlers. At the moment am... (3 Replies)
Discussion started by: newbie_01
3 Replies

6. Shell Programming and Scripting

Convert perl-statement to /bin/sh shell

Hi, I'm doing a small shellscript which is going to take each line in a "queue file" and do stuff to them. I can do the script easily, but I'd like this one to be a bit prettier. Consider the following perl statement: ... foreach my $line (@filedata) { my ($a, $b, $c) = split(/\t/,... (4 Replies)
Discussion started by: brightstorm
4 Replies

7. Shell Programming and Scripting

convert perl code to shell script

This is about how to Monitoring folder for new files using shell script im doing a project using smsserver tools 3. i have used a perl script to handle incoming messages. the content of each message must be directed to a java program. this program generates the answer to reply to the user... (2 Replies)
Discussion started by: x34
2 Replies

8. Shell Programming and Scripting

Shell/Perl script to convert to Capitalize case

I need a shell script which will convert the given string within a <title> tag to Capitalize case. E.g "<title>hi man: check this out</title>" to "<title>Hi Man: Check This Out</title>" (11 Replies)
Discussion started by: parshant_bvcoe
11 Replies

9. Shell Programming and Scripting

Help! Need to convert bash shell to perl

Hello All. I am very new to Linux and I am currently interning. I have been working on a project for 2 weeks now and I have had no success. I have to convert bash shell into perl to decrypt and store files. Here is the code in Linux and Bash. Any help would be greatly appreciated. $... (0 Replies)
Discussion started by: freak
0 Replies

10. Shell Programming and Scripting

convert the below perl sript to shell script

perl script: my $logdir = '/smp/dyn/logfiles/fsm/mp/mp'; $logdir = $logdir ."/mp${toDate}*"; i tried to make it..as below .. but not working .. date +%m%d%y logdir = /smp/dyn/logfiles/fsm/mp/mp logdir=$logdir/mp"$date" but it was not working..... can someone please help me out in... (1 Reply)
Discussion started by: mail2sant
1 Replies
Login or Register to Ask a Question