Bash check if process is running if not start it

I have several scripts that start when my Raspberry Pi is booted, one of them is to start screen session with IRSSI, but sometimes it can happen that two irssi processes are running.

To avoid that I am using a wrapper script eirssi.sh with just:

ps -C irssi || irssi

This method works a lot of times, but there are cases where this is not working and you need a little bit more code to get it tuned properly for example if you want to check if you have command “rtorrent -D” running then following code is proven:

#!/bin/bash
  RESULT=`ps aux | sed -n "/rtorrent \-D/p"`
  if [ "${RESULT:-null}" = null ]; then
  echo "not running"
  else
  echo "running"
  fi

Simple shell script that will guarantee that your commands will not start twice.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.