Search code examples
terminalfishmpd

Fish Shell: Check and run a process if not running


I want to start a process [mpd] when I open up terminal, and check if it's already running when I open a second tab so it won't try to run it again. This is what I used to have on my .bashrc:

#start mpd automatically if not yet running.
if [[ -z "$(pgrep mpd)" ]]; then
  mpd
fi

I suppose is something like this, or maybe using test -f, but I can't figure out how to make it work:

# MPD
if pgrep mpd > /dev/null 
  command mpd
end

[this is located on my config.fish file].


Solution

  • You could add to your config.fish.

    if not pgrep -f mpd > /dev/null
        command mpd
    end
    

    Or if you are using fish >= 2.3.0 to ~/.config/fish/conf.d/* for a better separation of concerns.