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!

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.

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.