I think you have two options here.
- Make a wrapper for i3status which checks if process runs.
- Or make a wrapper for your command which writes the PID to the file for you.
Simple shell script like this could solve it.
#!/bin/bash
PID_FILE=your_file.pid
trap "rm -f $PID_FILE" EXIT
your_command &
echo $! > $PID_FILE
wait $!
ret=$?
exit $ret
EDITED: the script above works only if you have only one instance
If you need to run more instaces of that process you can extend the script to something like this.
#!/bin/bash
PID_FILE=t.pid
COMMAND=sleep
ARGS=15
exit_h() {
kill -TERM $! 2> /dev/null
RUN=`pidof -o $! $COMMAND | cut -d' ' -f1`
if [ ! -z $RUN ];then
echo $RUN > $PID_FILE
else
rm -f $PID_FILE
fi
}
trap "exit_h" EXIT
$COMMAND $ARGS &
echo $! > $PID_FILE
wait $!
exit $?
The pid can be reused and script should still work (because the file gets deleted).
However there is possible race condition if all processes exit at the same time. There is a small chance that pidfile remains undeleted. But that still does not matter unless the pid in that file gets reused. So overall chance of this failure is close to zero i think. I would say it never happens in my life but it's possible so do not use this to control nuclear power plant.
I want to do this too. It would be perfect to have run_watch accept output of `pidof` command.