Any Perl Guru Or Less Newbie Than Me?

liunx

Guest
Hello,<br /><br />I never ever programmed in perl before but for the last few days, I have been trying to create a small script to check if an app is ruuning and if it's not to run it. And I will call this script in a cron every 5mins.<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->#!/usr/bin/perl<br />$cmd="ps -A | grep xmms | grep -v grep";<br />$output=qx/$cmd/;<br />if ($output!=~m/$xmms/) {<br />qx/xmms/;<br />}<!--c2--></div><!--ec2--><br /><br />The problem seem to come from the beginning, as if it was not understanding my first command: ps -A | grep xmms | grep -v grep<br /><br />I asked on a programming ofurm, but noone answered me :/ perhaps I will be more lucky here <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/unsure.gif" style="vertical-align:middle" emoid=":unsure:" border="0" alt="unsure.gif" /> <br /><br />Ludo<!--content-->
I have no idea about what could be wrong, as I know nothing about PERL.<br /><br />But I do have one question: why the "grep -v grep" command at the end? Won't the output of the script be just the same without it?<!--content-->
Good question Raul <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><br /><br />In fact I created this script in using several scripts find on Google...<br />I just tried to take off this last part and you are right... it changes nothing! but even without it, it's not working.<br /><br />One small mistake at the time, I'm sure I will get this damned script working! lol<br /><br />Ludo<!--content-->
The following line is suspicious:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->if ($output!=~m/$xmms/)<!--c2--></div><!--ec2--><br /><br />Is it your intention to have this if statement be true if "xmms" is not found in the output? If so, it would need to be:<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->if ($output !~ m/xmms/)<!--c2--></div><!--ec2--><br /><br />The proper syntax for "match if not" is "!~". In the above, $xmms will match the variable $xmms which is not defined as you have presented your script.<!--content-->
Hi Ludo,<br /><br />I'm curious as to why you want to write it in Perl? Couldn't you just do it with a shell script? I'm assuming you are wanting to run this on your own machine, right? Something like this:<br /><br /><!--coloro:blue--><span style="color:blue"><!--/coloro-->CMD=`ps -A | grep xmms`<br />if [ ${CMD} = "" ]<br />then<br /> execute the command<br />fi<!--colorc--></span><!--/colorc--><!--content-->
 
Top