openSUSE Forums > Programming/Scripting » Check process status by PID (running or dead)

Go Back   openSUSE Forums > Programming/Scripting
Forums FAQ Members List Search Today's Posts Mark Forums Read


Programming/Scripting Questions about programming, bash scripts, perl, php, cron jobs, ruby, python, etc.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-Oct-2009, 04:39
Puzzled Penguin
 
Join Date: Aug 2008
Posts: 30
ToLive hasn't been rated much yet
Default Check process status by PID (running or dead)

I'm writing my own service, it should run an app:
/usr/bin/socat PTY,link=/dev/ttyS0,raw,echo=0 tcp4:192.168.1.105:7000
And write it's PID to /var/run/socat1.pid.
How can I check if my app is running with a given PID from file? I need this to restart my service on status check (if PID is dead).

Thanks in advance!
Reply With Quote
  #2 (permalink)  
Old 07-Oct-2009, 05:51
hcvv's Avatar
Wise Penguin
 
Join Date: Jun 2008
Location: Netherlands
Posts: 1,904
hcvv 's reputation will be famous soon enoughhcvv 's reputation will be famous soon enoughhcvv 's reputation will be famous soon enough
Default Re: Check process status by PID (running or dead)

You can check with ps -p (and add other options to influence the ouput, see man ps). When it exists (return code) you could check the command (and more, depends on what you asked for as output) against what itshould be, because in theory the same PID is used again after cycling all of them.

BTW, When you want a process to be automaticaly restarted when it finishes (for whatever reason), you could make an entry in /etc/inittab for it.
__________________
Henk van Velden
Reply With Quote
  #3 (permalink)  
Old 07-Oct-2009, 06:54
Flux Capacitor Penguin
 
Join Date: Jun 2008
Location: GMT+10
Posts: 5,221
ken_yap has a reputation to be proud ofken_yap has a reputation to be proud ofken_yap has a reputation to be proud ofken_yap has a reputation to be proud ofken_yap has a reputation to be proud ofken_yap has a reputation to be proud ofken_yap has a reputation to be proud ofken_yap has a reputation to be proud ofken_yap has a reputation to be proud ofken_yap has a reputation to be proud of
Default Re: Check process status by PID (running or dead)

If the pid is of running process there will be a directory at /proc/<pid>. You should double check that it is indeed the right process by looking at its command line, by reading cmdline, one of the "files" in that directory.

Better still, as hcvv said, use inittab to respawn the process when it exits.
Reply With Quote
Reply

Bookmarks


Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




 

Search Engine Friendly URLs by vBSEO 3.3.0 RC2