How do I start and restart a program via a perl script?

G

grocery_stocker

The following scrpt is supposed to continuously scan the *nix who list
to see if a particular person enters the party chatline. If they
enter, then the script is supposed to trigger the nope program. When
than person leaves, the nope program is supposed to be killed and the
script goes back to scanning to see if that person enters the party
chanline again.

However, I can't seem to get it to work correclty. Ideas?

#!/usr/bin/perl

my $pid;

while (True)
{
if(`w | grep cdalten | grep party`) {
$pid = open(FH, "/home/guest/cdalten/nope2 &|");
#print $pid;
kill $pid;
}
kill $pid;
sleep(1);
}
 
G

grocery_stocker

You should always enable warnings when developing Perl code.

--


I enables warnings, but it still really hasn't shed light into the
problem. Here is what I get..

#!/usr/bin/perl
use warnings;

my $pid = -1;

while (True)
{
local *FH;

if(`w | grep nambla | grep party`) {
$pid = open(FH, "/home/guest/cdalten/nope2 &|") or die
"$!";
#print $pid;
kill $pid;
}
else {
kill $pid;
}
sleep(1);
}

% ./scan.pl
Bareword found in conditional at ./scan.pl line 20.
^C
 
J

Jürgen Exner

Tad J McClellan said:
You should always enable warnings when developing Perl code.

Yeah, but even that ugly bare word is still a true value, so the loop
will loop as expected.

To the OP: much worse are:
- open() without testing for success
- therefore potentially undefined $pid, causing spurious errors when
using this undefined value in the kill()
- and the intended logic is just beyond me:

if(`w | grep cdalten | grep party`) {

If something is found then

$pid = open(FH, "/home/guest/cdalten/nope2 &|");

start some program

kill $pid;

and kill it immediately (why start it in the first place if you are
killing it immediately?).
\ }

and regardless if something is found or not

kill $pid;

kill the last program, no matter that it had been killed anyway already


To answer the question in the Subject: you use system() (or maybe exec()
or backticks or ...) for both tasks

To answer the question in the body (which interestingly enough has
little to do with the Subject):

Your design is missing vital parts and will never work that way.
When starting that external program store that person and the associated
PID in a hash. Then in every loop check if that person is still online.
And only if he is not online any longer then call the kill with the PID
you stored earlier.
However, that is a poor design. There really should be some better way
to stop your system but to kill a process.

jue
 
T

Tad J McClellan

grocery_stocker said:
I enables warnings,


I didn't think it was necessary to point out the natural corollary
to enabling warnings, but I guess it is...

You should always enable warnings when developing Perl code, and then
modify your code so that is does not generate any warning messages.

Bareword found in conditional at ./scan.pl line 20.


So fix it already!

while ('True')
or
while (1)
or
while ('infinite loop')
or
while ('forever')
 
M

Martijn Lievaart

I enables warnings, but it still really hasn't shed light into the
problem. Here is what I get..

#!/usr/bin/perl
use warnings;

my $pid = -1;

while (True)
{
local *FH;

if(`w | grep nambla | grep party`) {
$pid = open(FH, "/home/guest/cdalten/nope2 &|") or die
"$!";
#print $pid;
kill $pid;
}
else {
kill $pid;
}
sleep(1);
}

% ./scan.pl
Bareword found in conditional at ./scan.pl line 20. ^C

The error does not match the code you posted.....

M4
 
X

Xho Jingleheimerschmidt

grocery_stocker said:
The following scrpt is supposed to continuously scan the *nix who list
to see if a particular person enters the party chatline. If they
enter, then the script is supposed to trigger the nope program. When
than person leaves, the nope program is supposed to be killed

Killed by whom?
and the
script goes back to scanning to see if that person enters the party
chanline again.

However, I can't seem to get it to work correclty. Ideas?

What is it doing instead of working correctly?
#!/usr/bin/perl

my $pid;

while (True)

Not only should you turn on strict and warnings, you should also take
care of the problems they indicate.
{
if(`w | grep cdalten | grep party`) {
$pid = open(FH, "/home/guest/cdalten/nope2 &|");

Because of the &, perl will open a shell and give the shell your command
(minus the pipe, i think) to run. The return will be the pid of this
shell. The shell will then start nope2. On unix-like systems, the
shell will then exit, because & tells it not to wait for nope2.

#print $pid;
kill $pid;

You are killing the shell, not the nope2 that the shell started.
Depending on the vagaries of the scheduler, you might be attempting to
kill it before it spawned nope2, after it spawned nope2 but before it
exited, or after it finished its job and exited.

Xho
 
G

grocery_stocker

Killed by whom?



What is it doing instead of working correctly?






Not only should you turn on strict and warnings, you should also take
care of the problems they indicate.


Because of the &, perl will open a shell and give the shell your command
(minus the pipe, i think) to run. The return will be the pid of this
shell. The shell will then start nope2. On unix-like systems, the
shell will then exit, because & tells it not to wait for nope2.


You are killing the shell, not the nope2 that the shell started.
Depending on the vagaries of the scheduler, you might be attempting to
kill it before it spawned nope2, after it spawned nope2 but before it
exited, or after it finished its job and exited.

Okay, so how would I kill nope2 ?
 
X

Xho Jingleheimerschmidt

grocery_stocker said:
Okay, so how would I kill nope2 ?

I don't know what nope2 does, or how it does it, or why it does it, so I
don't know how one would go about deciding when it is done doing it.
I would think nope2 would decide for itself when it was doing whatever
it is doing, and would exit.

If you omitted the & from your forked open code, then the pid you got
back would be that of nope2, and so your kill statement would be killing
nope2. Whether it would be doing it at the right time is doubtful, but
only you can answer that.

Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top