LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-16-2006, 01:44 AM   #1
Harpune
Member
 
Registered: Sep 2002
Location: Seattle, WA
Distribution: Ubuntu
Posts: 101

Rep: Reputation: 15
Bash: check pid for active process (FreeBSD)


I have a bash script, this one:

Code:
#!/usr/local/bin/bash

pidfile=/var/dchub/pid
hubscript=/var/dchub/DCHub.py
python=/usr/local/bin/python

if [ -e $pidfile ]
then
  #check for running process
  hubpid=`cat $pidfile`
  if [ `kill -CHLD $hubpid >/dev/null 2>&1` ]
  then
    exit 0
  else
    echo "The hub is dead; long live the hub!"
    $python $hubscript
  fi
else
  echo "The hub is dead; long live the hub!"
  $python $hubscript  
fi
Its purpose is to check py-dchub's pid file and if it is non-existant or if their is no process associated with the pid in the file, to restart py-dchub. I have been able to gather that sending the SIGCHLD signal to a process will be ignored by the process, but you can test the exit status of kill to see if the process exists or not, but for some reason, kill is always returning '0'. Whenever I run the script it attempts to start py-dchub, whether or not it is already running. What am I doing wrong?

I am using bash 3.1.10_1 straight from the FreeBSD RELEASE-6.1 packages.

Thanks
 
Old 05-16-2006, 01:28 PM   #2
ataraxia
Member
 
Registered: Apr 2006
Location: Pittsburgh
Distribution: Debian Sid AMD64
Posts: 296

Rep: Reputation: 30
The backquotes around your "kill" make your "if" check the output of the kill, instead of its return code. Remove them.
 
Old 05-16-2006, 01:33 PM   #3
Harpune
Member
 
Registered: Sep 2002
Location: Seattle, WA
Distribution: Ubuntu
Posts: 101

Original Poster
Rep: Reputation: 15
EDIT: I took out the backticks and it still behaves similarly. I also checked other scripts online that claim to do the samet hing, and they all have backticks around the 'kill' command.

Also, I realized that I think I had my logic backwares in the internal 'if' statement. I think it should read something like this:

Code:
  if [ `kill -CHLD $hubpid >/dev/null 2>&1` ]
  then
    echo "The hub is dead; long live the hub!"
    $python $hubscript
  else
    exit 0
  fi
With the hub attempting to restart if the kill command exits '1' and not the other way around. I have made those changes, though, and my script is still not doing what it should be.

Any more ideas?

Last edited by Harpune; 05-16-2006 at 01:49 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting pid of a process!! vishamr2000 Programming 34 03-12-2015 07:12 AM
Obtain ip address and check for running process via Bash Script? xconspirisist Programming 10 09-12-2008 01:18 PM
how to get process status using pid? hongxing Linux - Software 2 04-11-2006 07:11 AM
Simple check for process, bash BruceC Programming 9 02-09-2006 03:57 AM
using bash to find the pid of a process called "name" poiuytrewq Linux - Newbie 3 10-25-2004 09:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration