On the same machine (assuming there is a file system), you could use a "heart beat file". Have P1 update the file with the current time-stamp periodically, and P2 can check if that gets too old. You can also write the P1 process id in there, and P2 can check if that pid is still alive.
X=`cat /tmp/P1.heartbeat`
NOW=`date +%s`
DIFF=$(( $NOW - $X))
if [ $DIFF -gt 60 ] ; then
echo P1 died $DIFF seconds ago.
fi
You could setup a network socket listener on P2 that P1 connects to periodically. You'll need to specify the language you're using for a more specific code example.
As Thilo mentioned below, since they are on the same system you could use a simple file as the "I'm alive" message. Here's an example:
Have "P1" update the file with the current time every few seconds. Here is a shell example (sorry, I don't do C++ well):
rm /tmp/P1.heartbeat
date +%s > /tmp/P1.heartbeat
P2 then opens-reads-closes the file. It then compares it to the current time and takes the appropriate action if the time in the file is "too old".
Just make sure your P2 system closes the file after each read to make sure it re-reads the new file.
X=`cat /tmp/P1.heartbeat`
NOW=`date +%s`
DIFF=$(( $NOW - $X))
if [ $DIFF -gt 60 ] ; then
echo P1 died $DIFF seconds ago.
fi
发布评论
评论(2)
在同一台机器上(假设有文件系统),您可以使用“心跳文件”。让 P1 定期使用当前时间戳更新文件,P2 可以检查该文件是否太旧。您还可以在其中写入 P1 进程 id,P2 可以检查该 pid 是否仍然存在。
On the same machine (assuming there is a file system), you could use a "heart beat file". Have P1 update the file with the current time-stamp periodically, and P2 can check if that gets too old. You can also write the P1 process id in there, and P2 can check if that pid is still alive.
您可以在 P2 上设置 P1 定期连接的网络套接字侦听器。您需要指定更具体的代码示例所使用的语言。
正如 Thilo 下面提到的,由于它们位于同一系统上,因此您可以使用一个简单的文件作为“我还活着”消息。下面是一个示例:
You could setup a network socket listener on P2 that P1 connects to periodically. You'll need to specify the language you're using for a more specific code example.
As Thilo mentioned below, since they are on the same system you could use a simple file as the "I'm alive" message. Here's an example: