1. Perl
  2. builtin functions
  3. here

kill function - sends a signal to the process

You can use the kill function to kill the process.

kill(signal number, process ID);

Specify the signal number in the first argument.

Specify the process ID in the second argument.

The return value is the number of successful kill functions.

You can also specify multiple process IDs in a list, as shown below.

kill(signal number, process ID1, process ID2, ...);

Get the process ID

The process ID is specified in the second argument of the kill function, but in the case of Linux, this ID can be obtained by executing "ps -ef".

For example, in the following, the server process is started, but the second column from the left is the process ID. The third from the left is the process ID of the parent process.

kimoto 20220 25715 0 Dec25? 00:05:35/home/kimoto/labo/perltweet/script/perltweet

For example, the second number from the left, "20220", is the process ID. This will be the process ID you specify for kill.

Get signal number

A list of signals can be found by loading the Config module and using "$Config{sig_name}".

use Config;

use Data::Dumper;

print Dumper $Config{sig_name};

This number is the signal number because it is arranged in order from the first.

Check if the signal can be received

Signal number 0 is a special number that allows the target process to check if it can receive the signal.

my $success = kill(0, process ID);

This can be used to check if the child process is actually alive.

Doesn't work well for Windows fork child processes

Note that the kill function works fine for Linux/Unix-based fork child processes, but not for Windows fork child processes.

For details on the kill function, see kill function in the official Perl documentation.

Related Informatrion