RootPhoenix
RootPhoenix

Reputation: 1767

Cannot send signal to another process in perl

I have to send a sigusr2 signal number 12 to a process named xyz in a cgi perl script. I do the following:

my $pid = `pidof xyz`;

kill 12, $pid or die "could not kill: $pid";

It dies when I press the button on which this command run.

How can I send signal to a process named "xyz" in a cgi perl script.

Upvotes: 1

Views: 388

Answers (1)

Sebastian
Sebastian

Reputation: 2560

You should extend your error message by $!:

my $pid = `pidof xyz`;
kill 12, $pid or die "could not kill $pid: $!";

$! contains the last system call error: http://perldoc.perl.org/perlvar.html#%24ERRNO

You should also check if any PID has been found:

my $pid = `pidof xyz`;
die 'No PID found for xyz!' unless $pid;
kill 12, $pid or die "could not kill $pid: $!";

A uid mismatch (as mentioned in the comments to your post) might be the reason. You could use ps to check:

my $pid = `pidof xyz`;
die 'No PID found for xyz!' unless $pid;
system "ps u $$ $pid";
kill 12, $pid or die "could not kill $pid: $!";

The output should look like this:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0 185388  5960 ?        Ss   08:31   0:01 /sbin/init splash
some      3159  0.0  0.0  27296  8836 pts/11   Ss   08:33   0:00 bash

The value of the USER column should be the same for both processes (the Perl process and the process being killed), otherwise you're not allowed to send any signal (except if your script runs as root, which isn't recommended).

Upvotes: 3

Related Questions