Macelya Margarita
Macelya Margarita

Reputation: 1

Perl. Send signal: Invalid argument

I have a program,I know its pid and it has a signal processing:

 $SIG {INT} = sub {.... };

I have tried to send a signal from another program

my $pid = 5396;
kill 2, $pid or die $!;

and got an error "Invalid argument at ..."! I have tried to write

kill "INT",$pid or die $!;
kill "INT"=>$pid or die $!;

etc. but still have this error. Why?

Thanks in advance.

Upvotes: 0

Views: 688

Answers (2)

ikegami
ikegami

Reputation: 386371

Windows doesn't have signals. That's a unix thing. However, Perl does try to emulate them a little.

  • kill INT => $pid results in GenerateConsoleCtrlEvent(CTRL_C_EVENT, pid)
  • kill BREAK => $pid results in GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, pid)
  • kill TERM => $pid results in GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, pid)
  • kill KILL => $pid results in TerminateProcess(process_handle, sig)

GenerateConsoleCtrlEvent can only signal "a console process group that shares the console associated with the calling process", so it won't work unless the process to which you are trying to send the signal has a console and it's the same one as your Perl process.

You are getting that error because the process to which you are trying to send a signal doesn't have a console, and doesn't share the same console as the script sending the signal.

Upvotes: 1

TLP
TLP

Reputation: 67900

From perldoc -f kill

kill SIGNAL, LIST
kill SIGNAL
        Sends a signal to a list of processes. Returns the number of
        processes successfully signaled (which is not necessarily the
        same as the number actually killed).

The error is misleading. The kill function returns the number of processes successfully signalled, which is this case is probably zero 0. It does not have any valid information about why it failed, so it sets $! to a rather generic message.

If I were to guess, I would say that your $pid is incorrect. I get the same error when testing with non-existent pids. It does contain a number, though, because if you do not supply a number, you get this error:

Can't kill a non-numeric process ID at ...

Upvotes: 1

Related Questions