Link to home
Start Free TrialLog in
Avatar of jewee
jewee

asked on

Kill a process by name

In my perl script, I have the following:

my $processid = `ps -elf | pgrep \"SybProc\"`;

if ($processid)
{
    my $killprocess = system ("kill $processid");
    if ($killprocess != 0)
    {
         print "Cannot kill process\n";
    }
}

if the SybProc process exists, it kills it.  However, sometimes the system call  does not return 0 even if it successfully killed the process.
What is wrong with the implementation?
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jewee
jewee

ASKER

kill process takes the value of the process id.  I haven't used perl's kill function.
if the more than "SybProc" process is running, the script kills the first process but other process remain unkilled. so u amy need to change the script.

'ps -elf | pgrep \"SybProc\"` >> dump in a temp  file;
cut the process id filed from the temp file put it an array
then use the "kill $procid" in loop, so that it will kill one by one.

What does pgrep do?
what is $processid when the system call  does not return 0?