The i3 FAQ has migrated to https://github.com/i3/i3/discussions. All content here is read-only.
Ask Your Question
0

Can I watch a process with i3status and run_watch without a pidfile?

asked 2012-11-27 00:07:23 +0000

anonymous user

Anonymous

I'd like to check if any process named 'foo' is running. If more than one is running, that's okay. This process doesn't create a PID file.

edit retag flag offensive close merge delete

Comments

I want to do this too. It would be perfect to have run_watch accept output of `pidof` command.

biocyberman gravatar imagebiocyberman ( 2015-07-14 09:56:00 +0000 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2012-11-27 01:04:00 +0000

loblik gravatar image

updated 2012-11-27 11:15:56 +0000

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.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2012-11-27 00:07:23 +0000

Seen: 495 times

Last updated: Nov 27 '12