Check Whether A Process Is Running Or Not

Discussion in 'Perl' started by pradeep, Aug 4, 2008.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in

    Introduction



    May times we require to check whether a process is running or not, may be for the purpose of maintaining a lock file for a process or for some other reason. Here today we'll discuss a technique in which we can do this checking provided the process/pid being checked is checked is run the same user as who is checking it or the checking script is running as root.

    The Technique



    We'll use the kill function in Perl to achieve or goal, we'll send a SIGCHLD signal to the process if the function returns a non-zero value then we know the signals were successfully sent, i.e. the process is running. We send the SIGCHLD signal so as not to harm the process being checked.

    Example script:
    Code:
     #!/usr/bin/perl
     
     # @file: check_process.pl - check whether a proccess is running or not.
     
     use strict;
     use POSIX;
     
     my $pid = shift;
     die "usage: check_process.pl pid\n" unless defined $pid;
     
     print "Process ($pid) is ",(kill(SIGCHLD,$pid)!=0)?'running':'not running';
     

    Uses



    We sometimes would need to check that only one instance of a programs runs, like a program that runs from cron.It's be really helpful we can check whether the last launch of the program from the cron is still running. I hope someone will find this really useful.

    Code:
     my ($lock_file) = $0 =~ m!/([^/]+)$!; ## filename for the lock file, the program name should be unique
     my $lock_file = "/tmp/$lock_file.pid"; ## tmp is usually writeable by anyone
     my $pid = $$; ## pid of the current script
     
     if(-e $lock_file)
     {
         open(FL,"<$lock_file");
         my $file_pid=<FL>;
         if(kill(SIGCHLD,$file_pid)!=0)
         {
             print "Already running ($file_pid)\n";
             exit;
         }
     }
     
     open(FL,">$lock_file");
     print FL "$pid";
     close(FL);
     
     
  2. debleena_doll2002

    debleena_doll2002 New Member

    Joined:
    Feb 5, 2008
    Messages:
    119
    Likes Received:
    0
    Trophy Points:
    0
  3. TrainnafalWag

    TrainnafalWag New Member

    Joined:
    Aug 7, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I read some of the posts and I think it is a great site.
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Winner of Article of the month (August)
     
  7. Maex

    Maex New Member

    Joined:
    Nov 14, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Sorry, but sending SIGCHLD to a process to check if it is running is plain wrong.
    There is explicit "0" to test if a process is accepting a signal at all, so the correct form would be
    kill(0,$pid)
    However note, that either way you will receive a !=0 result if you are not root and the process is not owned by you.
     
  8. PradeepKr

    PradeepKr New Member

    Joined:
    Aug 24, 2010
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.expertsguide.info
    Any justification to Maex's suggestions?
     
  9. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Maex is absolutely right, even I didn't know this at the time of writing this. Also, sending a SIGCHLD to the process will invoke any related signal handler which might be undesirable at times and have had a practical experience.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice