redcell184 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I am newbie to perl and i am trying to create a simple script that checks the status of a process and if the process is not running i want it to execute a simple shell command to start the process backup. I wrote something up, but i don't know exactly how to correct the syntax error I am generating or if the whole structure of it is wrong. Can someone please assist me. Thanks in advance.
#!/usr/bin/perl $com1=`ps -eaf | grep bpps | grep -v grep | awk '{print $1}'`; $com2=`sh -c /export/home/weblogic/bai/bpps.sh start 2 > /dev/null 2>& +1` if ($com1 = 'weblogic') { print"bpps is running.\n"; } else { $com2 }

Replies are listed 'Best First'.
Re: Creating process restart script
by Fletch (Bishop) on Oct 31, 2007 at 12:57 UTC

    Backticks execute the command they contain immediately, so you're running the sh command in $com2 unconditionally.

    You might be better served by looking at setting up an entry in inittab and let that handle keeping things alive.

Re: Creating process restart script
by toolic (Bishop) on Oct 31, 2007 at 13:04 UTC
    In addition to what Fletch said, there are a few syntax errors I can point out:

    All statements in Perl must end with a semicolon.

    You need to use a string comparison operator (eq) instead of the assignment operator (=) in your if statement.

    Using the strictures (use warnings; use strict;) will help avoid other common mistakes.

    I think this is more like what you wanted:

    #!/usr/bin/perl use warnings; use strict; my $com1 = `ps -eaf | grep bpps | grep -v grep | awk '{print $1}'`; my $com2; if ($com1 eq 'weblogic') { print "bpps is running.\n"; } else { $com2 = `sh -c /export/home/weblogic/bai/bpps.sh start 2 > /de +v/null 2>&1`; }

    This code is untested.

      Ok i get it now, but the eq `weblogic` portion of the script is that a method to grep for the output? and thank you for breaking it down
        Since you have not provided any sample output of your ps command (piped through grep and awk), I can only guess that you expect it to return a single string on a single line. Based on the "if" statement in your original code, I assumed that if the output of ps was not equal to the magic literal string 'weblogic', then you wanted to execute your $com2 command.

        Does this answer your question?