Bash script to verify if a process is running is not working












6















I have a simple script as below which checks if fail2ban service is running or not on Ubuntu 18.04:



#!/bin/bash

# Script to check if fail2ban service is running

if pgrep -x "fail2ban" > /dev/null
then
echo "Fail2ban is running"
else
echo "Fail2ban is not running"
fi


I have installed fail2ban in a test VM and is running on the VM. Here is a screenshot of systemctl status command.



But, when the run the above script, I get the result that "Fail2ban is not running". I am not sure if is with the script. I tried ps aux command too instead of pgrep. But, I still get the same result.










share|improve this question




















  • 4





    Why even try to cobble something together with ps, when you can use systemctl is-active fail2ban? FWIW your command probably fails because you specified an exact match (-x) but the process name is actually fail2ban.server

    – steeldriver
    Jan 18 at 15:55













  • Ok. Thanks. It worked, I changed the command to the above command. But, is there any way you know why pgrep will not work? Also, Is systemctl command available by default in all redhat systems?

    – skr
    Jan 18 at 15:58













  • Oh that is right. Thank you so much.

    – skr
    Jan 18 at 16:02











  • @steeldriver That's the service name, not the process name.

    – Lightness Races in Orbit
    Jan 18 at 18:16











  • @LightnessRacesinOrbit my mistake, the process is fail2ban-server (with a hyphen); pgrep -x fail2ban.server finds it because it's a regex match (. matches the -) and for example systemctl status fail2ban.service says Main PID: 2721 (fail2ban-server). The service name is fail2ban.service I believe.

    – steeldriver
    Jan 18 at 19:37
















6















I have a simple script as below which checks if fail2ban service is running or not on Ubuntu 18.04:



#!/bin/bash

# Script to check if fail2ban service is running

if pgrep -x "fail2ban" > /dev/null
then
echo "Fail2ban is running"
else
echo "Fail2ban is not running"
fi


I have installed fail2ban in a test VM and is running on the VM. Here is a screenshot of systemctl status command.



But, when the run the above script, I get the result that "Fail2ban is not running". I am not sure if is with the script. I tried ps aux command too instead of pgrep. But, I still get the same result.










share|improve this question




















  • 4





    Why even try to cobble something together with ps, when you can use systemctl is-active fail2ban? FWIW your command probably fails because you specified an exact match (-x) but the process name is actually fail2ban.server

    – steeldriver
    Jan 18 at 15:55













  • Ok. Thanks. It worked, I changed the command to the above command. But, is there any way you know why pgrep will not work? Also, Is systemctl command available by default in all redhat systems?

    – skr
    Jan 18 at 15:58













  • Oh that is right. Thank you so much.

    – skr
    Jan 18 at 16:02











  • @steeldriver That's the service name, not the process name.

    – Lightness Races in Orbit
    Jan 18 at 18:16











  • @LightnessRacesinOrbit my mistake, the process is fail2ban-server (with a hyphen); pgrep -x fail2ban.server finds it because it's a regex match (. matches the -) and for example systemctl status fail2ban.service says Main PID: 2721 (fail2ban-server). The service name is fail2ban.service I believe.

    – steeldriver
    Jan 18 at 19:37














6












6








6


1






I have a simple script as below which checks if fail2ban service is running or not on Ubuntu 18.04:



#!/bin/bash

# Script to check if fail2ban service is running

if pgrep -x "fail2ban" > /dev/null
then
echo "Fail2ban is running"
else
echo "Fail2ban is not running"
fi


I have installed fail2ban in a test VM and is running on the VM. Here is a screenshot of systemctl status command.



But, when the run the above script, I get the result that "Fail2ban is not running". I am not sure if is with the script. I tried ps aux command too instead of pgrep. But, I still get the same result.










share|improve this question
















I have a simple script as below which checks if fail2ban service is running or not on Ubuntu 18.04:



#!/bin/bash

# Script to check if fail2ban service is running

if pgrep -x "fail2ban" > /dev/null
then
echo "Fail2ban is running"
else
echo "Fail2ban is not running"
fi


I have installed fail2ban in a test VM and is running on the VM. Here is a screenshot of systemctl status command.



But, when the run the above script, I get the result that "Fail2ban is not running". I am not sure if is with the script. I tried ps aux command too instead of pgrep. But, I still get the same result.







bash process administrator






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 18 at 19:57









Pablo Bianchi

2,6251532




2,6251532










asked Jan 18 at 15:49









skrskr

313




313








  • 4





    Why even try to cobble something together with ps, when you can use systemctl is-active fail2ban? FWIW your command probably fails because you specified an exact match (-x) but the process name is actually fail2ban.server

    – steeldriver
    Jan 18 at 15:55













  • Ok. Thanks. It worked, I changed the command to the above command. But, is there any way you know why pgrep will not work? Also, Is systemctl command available by default in all redhat systems?

    – skr
    Jan 18 at 15:58













  • Oh that is right. Thank you so much.

    – skr
    Jan 18 at 16:02











  • @steeldriver That's the service name, not the process name.

    – Lightness Races in Orbit
    Jan 18 at 18:16











  • @LightnessRacesinOrbit my mistake, the process is fail2ban-server (with a hyphen); pgrep -x fail2ban.server finds it because it's a regex match (. matches the -) and for example systemctl status fail2ban.service says Main PID: 2721 (fail2ban-server). The service name is fail2ban.service I believe.

    – steeldriver
    Jan 18 at 19:37














  • 4





    Why even try to cobble something together with ps, when you can use systemctl is-active fail2ban? FWIW your command probably fails because you specified an exact match (-x) but the process name is actually fail2ban.server

    – steeldriver
    Jan 18 at 15:55













  • Ok. Thanks. It worked, I changed the command to the above command. But, is there any way you know why pgrep will not work? Also, Is systemctl command available by default in all redhat systems?

    – skr
    Jan 18 at 15:58













  • Oh that is right. Thank you so much.

    – skr
    Jan 18 at 16:02











  • @steeldriver That's the service name, not the process name.

    – Lightness Races in Orbit
    Jan 18 at 18:16











  • @LightnessRacesinOrbit my mistake, the process is fail2ban-server (with a hyphen); pgrep -x fail2ban.server finds it because it's a regex match (. matches the -) and for example systemctl status fail2ban.service says Main PID: 2721 (fail2ban-server). The service name is fail2ban.service I believe.

    – steeldriver
    Jan 18 at 19:37








4




4





Why even try to cobble something together with ps, when you can use systemctl is-active fail2ban? FWIW your command probably fails because you specified an exact match (-x) but the process name is actually fail2ban.server

– steeldriver
Jan 18 at 15:55







Why even try to cobble something together with ps, when you can use systemctl is-active fail2ban? FWIW your command probably fails because you specified an exact match (-x) but the process name is actually fail2ban.server

– steeldriver
Jan 18 at 15:55















Ok. Thanks. It worked, I changed the command to the above command. But, is there any way you know why pgrep will not work? Also, Is systemctl command available by default in all redhat systems?

– skr
Jan 18 at 15:58







Ok. Thanks. It worked, I changed the command to the above command. But, is there any way you know why pgrep will not work? Also, Is systemctl command available by default in all redhat systems?

– skr
Jan 18 at 15:58















Oh that is right. Thank you so much.

– skr
Jan 18 at 16:02





Oh that is right. Thank you so much.

– skr
Jan 18 at 16:02













@steeldriver That's the service name, not the process name.

– Lightness Races in Orbit
Jan 18 at 18:16





@steeldriver That's the service name, not the process name.

– Lightness Races in Orbit
Jan 18 at 18:16













@LightnessRacesinOrbit my mistake, the process is fail2ban-server (with a hyphen); pgrep -x fail2ban.server finds it because it's a regex match (. matches the -) and for example systemctl status fail2ban.service says Main PID: 2721 (fail2ban-server). The service name is fail2ban.service I believe.

– steeldriver
Jan 18 at 19:37





@LightnessRacesinOrbit my mistake, the process is fail2ban-server (with a hyphen); pgrep -x fail2ban.server finds it because it's a regex match (. matches the -) and for example systemctl status fail2ban.service says Main PID: 2721 (fail2ban-server). The service name is fail2ban.service I believe.

– steeldriver
Jan 18 at 19:37










2 Answers
2






active

oldest

votes


















12














You asked pgrep to exactly (-x) search for a process called fail2ban
but the output of systemctl status shows it is called
/usr/bin/python3 instead.



To check whether a systemd unit is running use



systemctl is-active --quiet fail2ban


That is:



if systemctl is-active --quiet fail2ban; then
echo "running"
else
echo "not running"
fi





share|improve this answer



















  • 2





    Alternatively, one could use pgrep -f '*fail2ban*' to pattern-match or pgrep -f '/usr/bin/python3 /usr/bin/fail2ban-server' to match exactly what screenshots reveal

    – Sergiy Kolodyazhnyy
    Jan 18 at 20:08






  • 2





    pgrep -f '*fail2ban*' could catch the pgrep command itself.

    – Dev
    Jan 18 at 20:16











  • At least on my 16.04 system, in spite of it being a python script, the process name (as seen in /proc/<pid>/comm) is fail2ban-server so pgrep -x fail2ban-server does work - although I see no reason to use it when systemctl is-active is available

    – steeldriver
    Jan 18 at 23:17



















0














The following shellscript running combines the result of





  • systemctl is-active and

  • ps -ef | ... | grep


in order to detect if a certain program (or a program name containing the search string) is running or not.



#!/bin/bash

if [ $# -ne 1 ]
then
echo "Usage: $0 <program-name>
$0 <part of program name>
Examples: $0 firefox
$0 term
$0 dbus
$0 'dbus-daemon --session'"
exit
fi

inversvid="033[7m"
resetvid="033[0m"
redback="033[1;37;41m"
greenback="033[1;37;42m"
blueback="033[1;37;44m"

runn=false
tmpfil=$(mktemp)

# check by systemctl

systemctl is-active --quiet "$1"
if [ $? -eq 0 ]
then
echo "systemctl is-active:"
runn=true
fi

# check by ps

ps -ef | tr -s ' ' ' ' | cut -d ' ' -f 8-9 | grep "$1" | grep -vE -e "$0 $1" -e "grep $1" | sort -u > "$tmpfil"

tmpstr=$(head -n1 $tmpfil)
#echo "$tmpstr"
if [ "$tmpstr" == "$1" ] || [ "${tmpstr##*/}" == "$1" ] || [ "${1##*/}" == "${0##*/}" ]
then
echo "ps -ef: active:"
runn=true
elif test -s "$tmpfil"
then
if $runn
then
echo "----- consider also ---------------------------------------------"
cat "$tmpfil"
echo "-----------------------------------------------------------------"
else
echo "----- try with: -------------------------------------------------"
cat "$tmpfil"
echo "-----------------------------------------------------------------"
fi
fi

if $runn
then
echo -e "$greenback $1 is running $resetvid"
else
inpath=$(which "$1")
if [ "$inpath" == "" ]
then
echo -e "$redback no path found to $1 $resetvid"
else
echo -e "$blueback $1 is not running $resetvid"
fi
fi


Make it executable and put it in a directory in PATH, if you wish. I put it into my bin directory and can used it without any path.



Usage:



$ running 
Usage: /home/sudodus/bin/running <program-name>
/home/sudodus/bin/running <part of program name>
Examples: /home/sudodus/bin/running firefox
/home/sudodus/bin/running term
/home/sudodus/bin/running dbus
/home/sudodus/bin/running 'dbus-daemon --session'


Examples:



$ running firefox
ps -ef: active:
firefox is running # green background - running

$ running term
----- try with: -------------------------------------------------
/usr/lib/gnome-terminal/gnome-terminal-server
xterm
x-terminal-emulator
-----------------------------------------------------------------
no path found to term # red background - path not found

$ running dbus
systemctl is-active:
----- consider also ---------------------------------------------
/usr/bin/dbus-daemon --session
/usr/bin/dbus-daemon --syslog
/usr/bin/dbus-daemon --system
/usr/bin/fcitx-dbus-watcher unix:abstract=/tmp/dbus-Nm2MSvuTZF,guid=25bad8d51276d088045625055c425080
-----------------------------------------------------------------
dbus is running # green background

$ running 'dbus-daemon --session'
ps -ef: active:
dbus-daemon --session is running # green background

$ running libreoffice
libreoffice is not running # blue background - not running





share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "89"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1110915%2fbash-script-to-verify-if-a-process-is-running-is-not-working%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    12














    You asked pgrep to exactly (-x) search for a process called fail2ban
    but the output of systemctl status shows it is called
    /usr/bin/python3 instead.



    To check whether a systemd unit is running use



    systemctl is-active --quiet fail2ban


    That is:



    if systemctl is-active --quiet fail2ban; then
    echo "running"
    else
    echo "not running"
    fi





    share|improve this answer



















    • 2





      Alternatively, one could use pgrep -f '*fail2ban*' to pattern-match or pgrep -f '/usr/bin/python3 /usr/bin/fail2ban-server' to match exactly what screenshots reveal

      – Sergiy Kolodyazhnyy
      Jan 18 at 20:08






    • 2





      pgrep -f '*fail2ban*' could catch the pgrep command itself.

      – Dev
      Jan 18 at 20:16











    • At least on my 16.04 system, in spite of it being a python script, the process name (as seen in /proc/<pid>/comm) is fail2ban-server so pgrep -x fail2ban-server does work - although I see no reason to use it when systemctl is-active is available

      – steeldriver
      Jan 18 at 23:17
















    12














    You asked pgrep to exactly (-x) search for a process called fail2ban
    but the output of systemctl status shows it is called
    /usr/bin/python3 instead.



    To check whether a systemd unit is running use



    systemctl is-active --quiet fail2ban


    That is:



    if systemctl is-active --quiet fail2ban; then
    echo "running"
    else
    echo "not running"
    fi





    share|improve this answer



















    • 2





      Alternatively, one could use pgrep -f '*fail2ban*' to pattern-match or pgrep -f '/usr/bin/python3 /usr/bin/fail2ban-server' to match exactly what screenshots reveal

      – Sergiy Kolodyazhnyy
      Jan 18 at 20:08






    • 2





      pgrep -f '*fail2ban*' could catch the pgrep command itself.

      – Dev
      Jan 18 at 20:16











    • At least on my 16.04 system, in spite of it being a python script, the process name (as seen in /proc/<pid>/comm) is fail2ban-server so pgrep -x fail2ban-server does work - although I see no reason to use it when systemctl is-active is available

      – steeldriver
      Jan 18 at 23:17














    12












    12








    12







    You asked pgrep to exactly (-x) search for a process called fail2ban
    but the output of systemctl status shows it is called
    /usr/bin/python3 instead.



    To check whether a systemd unit is running use



    systemctl is-active --quiet fail2ban


    That is:



    if systemctl is-active --quiet fail2ban; then
    echo "running"
    else
    echo "not running"
    fi





    share|improve this answer













    You asked pgrep to exactly (-x) search for a process called fail2ban
    but the output of systemctl status shows it is called
    /usr/bin/python3 instead.



    To check whether a systemd unit is running use



    systemctl is-active --quiet fail2ban


    That is:



    if systemctl is-active --quiet fail2ban; then
    echo "running"
    else
    echo "not running"
    fi






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 18 at 16:02









    PerlDuckPerlDuck

    6,61711535




    6,61711535








    • 2





      Alternatively, one could use pgrep -f '*fail2ban*' to pattern-match or pgrep -f '/usr/bin/python3 /usr/bin/fail2ban-server' to match exactly what screenshots reveal

      – Sergiy Kolodyazhnyy
      Jan 18 at 20:08






    • 2





      pgrep -f '*fail2ban*' could catch the pgrep command itself.

      – Dev
      Jan 18 at 20:16











    • At least on my 16.04 system, in spite of it being a python script, the process name (as seen in /proc/<pid>/comm) is fail2ban-server so pgrep -x fail2ban-server does work - although I see no reason to use it when systemctl is-active is available

      – steeldriver
      Jan 18 at 23:17














    • 2





      Alternatively, one could use pgrep -f '*fail2ban*' to pattern-match or pgrep -f '/usr/bin/python3 /usr/bin/fail2ban-server' to match exactly what screenshots reveal

      – Sergiy Kolodyazhnyy
      Jan 18 at 20:08






    • 2





      pgrep -f '*fail2ban*' could catch the pgrep command itself.

      – Dev
      Jan 18 at 20:16











    • At least on my 16.04 system, in spite of it being a python script, the process name (as seen in /proc/<pid>/comm) is fail2ban-server so pgrep -x fail2ban-server does work - although I see no reason to use it when systemctl is-active is available

      – steeldriver
      Jan 18 at 23:17








    2




    2





    Alternatively, one could use pgrep -f '*fail2ban*' to pattern-match or pgrep -f '/usr/bin/python3 /usr/bin/fail2ban-server' to match exactly what screenshots reveal

    – Sergiy Kolodyazhnyy
    Jan 18 at 20:08





    Alternatively, one could use pgrep -f '*fail2ban*' to pattern-match or pgrep -f '/usr/bin/python3 /usr/bin/fail2ban-server' to match exactly what screenshots reveal

    – Sergiy Kolodyazhnyy
    Jan 18 at 20:08




    2




    2





    pgrep -f '*fail2ban*' could catch the pgrep command itself.

    – Dev
    Jan 18 at 20:16





    pgrep -f '*fail2ban*' could catch the pgrep command itself.

    – Dev
    Jan 18 at 20:16













    At least on my 16.04 system, in spite of it being a python script, the process name (as seen in /proc/<pid>/comm) is fail2ban-server so pgrep -x fail2ban-server does work - although I see no reason to use it when systemctl is-active is available

    – steeldriver
    Jan 18 at 23:17





    At least on my 16.04 system, in spite of it being a python script, the process name (as seen in /proc/<pid>/comm) is fail2ban-server so pgrep -x fail2ban-server does work - although I see no reason to use it when systemctl is-active is available

    – steeldriver
    Jan 18 at 23:17













    0














    The following shellscript running combines the result of





    • systemctl is-active and

    • ps -ef | ... | grep


    in order to detect if a certain program (or a program name containing the search string) is running or not.



    #!/bin/bash

    if [ $# -ne 1 ]
    then
    echo "Usage: $0 <program-name>
    $0 <part of program name>
    Examples: $0 firefox
    $0 term
    $0 dbus
    $0 'dbus-daemon --session'"
    exit
    fi

    inversvid="033[7m"
    resetvid="033[0m"
    redback="033[1;37;41m"
    greenback="033[1;37;42m"
    blueback="033[1;37;44m"

    runn=false
    tmpfil=$(mktemp)

    # check by systemctl

    systemctl is-active --quiet "$1"
    if [ $? -eq 0 ]
    then
    echo "systemctl is-active:"
    runn=true
    fi

    # check by ps

    ps -ef | tr -s ' ' ' ' | cut -d ' ' -f 8-9 | grep "$1" | grep -vE -e "$0 $1" -e "grep $1" | sort -u > "$tmpfil"

    tmpstr=$(head -n1 $tmpfil)
    #echo "$tmpstr"
    if [ "$tmpstr" == "$1" ] || [ "${tmpstr##*/}" == "$1" ] || [ "${1##*/}" == "${0##*/}" ]
    then
    echo "ps -ef: active:"
    runn=true
    elif test -s "$tmpfil"
    then
    if $runn
    then
    echo "----- consider also ---------------------------------------------"
    cat "$tmpfil"
    echo "-----------------------------------------------------------------"
    else
    echo "----- try with: -------------------------------------------------"
    cat "$tmpfil"
    echo "-----------------------------------------------------------------"
    fi
    fi

    if $runn
    then
    echo -e "$greenback $1 is running $resetvid"
    else
    inpath=$(which "$1")
    if [ "$inpath" == "" ]
    then
    echo -e "$redback no path found to $1 $resetvid"
    else
    echo -e "$blueback $1 is not running $resetvid"
    fi
    fi


    Make it executable and put it in a directory in PATH, if you wish. I put it into my bin directory and can used it without any path.



    Usage:



    $ running 
    Usage: /home/sudodus/bin/running <program-name>
    /home/sudodus/bin/running <part of program name>
    Examples: /home/sudodus/bin/running firefox
    /home/sudodus/bin/running term
    /home/sudodus/bin/running dbus
    /home/sudodus/bin/running 'dbus-daemon --session'


    Examples:



    $ running firefox
    ps -ef: active:
    firefox is running # green background - running

    $ running term
    ----- try with: -------------------------------------------------
    /usr/lib/gnome-terminal/gnome-terminal-server
    xterm
    x-terminal-emulator
    -----------------------------------------------------------------
    no path found to term # red background - path not found

    $ running dbus
    systemctl is-active:
    ----- consider also ---------------------------------------------
    /usr/bin/dbus-daemon --session
    /usr/bin/dbus-daemon --syslog
    /usr/bin/dbus-daemon --system
    /usr/bin/fcitx-dbus-watcher unix:abstract=/tmp/dbus-Nm2MSvuTZF,guid=25bad8d51276d088045625055c425080
    -----------------------------------------------------------------
    dbus is running # green background

    $ running 'dbus-daemon --session'
    ps -ef: active:
    dbus-daemon --session is running # green background

    $ running libreoffice
    libreoffice is not running # blue background - not running





    share|improve this answer




























      0














      The following shellscript running combines the result of





      • systemctl is-active and

      • ps -ef | ... | grep


      in order to detect if a certain program (or a program name containing the search string) is running or not.



      #!/bin/bash

      if [ $# -ne 1 ]
      then
      echo "Usage: $0 <program-name>
      $0 <part of program name>
      Examples: $0 firefox
      $0 term
      $0 dbus
      $0 'dbus-daemon --session'"
      exit
      fi

      inversvid="033[7m"
      resetvid="033[0m"
      redback="033[1;37;41m"
      greenback="033[1;37;42m"
      blueback="033[1;37;44m"

      runn=false
      tmpfil=$(mktemp)

      # check by systemctl

      systemctl is-active --quiet "$1"
      if [ $? -eq 0 ]
      then
      echo "systemctl is-active:"
      runn=true
      fi

      # check by ps

      ps -ef | tr -s ' ' ' ' | cut -d ' ' -f 8-9 | grep "$1" | grep -vE -e "$0 $1" -e "grep $1" | sort -u > "$tmpfil"

      tmpstr=$(head -n1 $tmpfil)
      #echo "$tmpstr"
      if [ "$tmpstr" == "$1" ] || [ "${tmpstr##*/}" == "$1" ] || [ "${1##*/}" == "${0##*/}" ]
      then
      echo "ps -ef: active:"
      runn=true
      elif test -s "$tmpfil"
      then
      if $runn
      then
      echo "----- consider also ---------------------------------------------"
      cat "$tmpfil"
      echo "-----------------------------------------------------------------"
      else
      echo "----- try with: -------------------------------------------------"
      cat "$tmpfil"
      echo "-----------------------------------------------------------------"
      fi
      fi

      if $runn
      then
      echo -e "$greenback $1 is running $resetvid"
      else
      inpath=$(which "$1")
      if [ "$inpath" == "" ]
      then
      echo -e "$redback no path found to $1 $resetvid"
      else
      echo -e "$blueback $1 is not running $resetvid"
      fi
      fi


      Make it executable and put it in a directory in PATH, if you wish. I put it into my bin directory and can used it without any path.



      Usage:



      $ running 
      Usage: /home/sudodus/bin/running <program-name>
      /home/sudodus/bin/running <part of program name>
      Examples: /home/sudodus/bin/running firefox
      /home/sudodus/bin/running term
      /home/sudodus/bin/running dbus
      /home/sudodus/bin/running 'dbus-daemon --session'


      Examples:



      $ running firefox
      ps -ef: active:
      firefox is running # green background - running

      $ running term
      ----- try with: -------------------------------------------------
      /usr/lib/gnome-terminal/gnome-terminal-server
      xterm
      x-terminal-emulator
      -----------------------------------------------------------------
      no path found to term # red background - path not found

      $ running dbus
      systemctl is-active:
      ----- consider also ---------------------------------------------
      /usr/bin/dbus-daemon --session
      /usr/bin/dbus-daemon --syslog
      /usr/bin/dbus-daemon --system
      /usr/bin/fcitx-dbus-watcher unix:abstract=/tmp/dbus-Nm2MSvuTZF,guid=25bad8d51276d088045625055c425080
      -----------------------------------------------------------------
      dbus is running # green background

      $ running 'dbus-daemon --session'
      ps -ef: active:
      dbus-daemon --session is running # green background

      $ running libreoffice
      libreoffice is not running # blue background - not running





      share|improve this answer


























        0












        0








        0







        The following shellscript running combines the result of





        • systemctl is-active and

        • ps -ef | ... | grep


        in order to detect if a certain program (or a program name containing the search string) is running or not.



        #!/bin/bash

        if [ $# -ne 1 ]
        then
        echo "Usage: $0 <program-name>
        $0 <part of program name>
        Examples: $0 firefox
        $0 term
        $0 dbus
        $0 'dbus-daemon --session'"
        exit
        fi

        inversvid="033[7m"
        resetvid="033[0m"
        redback="033[1;37;41m"
        greenback="033[1;37;42m"
        blueback="033[1;37;44m"

        runn=false
        tmpfil=$(mktemp)

        # check by systemctl

        systemctl is-active --quiet "$1"
        if [ $? -eq 0 ]
        then
        echo "systemctl is-active:"
        runn=true
        fi

        # check by ps

        ps -ef | tr -s ' ' ' ' | cut -d ' ' -f 8-9 | grep "$1" | grep -vE -e "$0 $1" -e "grep $1" | sort -u > "$tmpfil"

        tmpstr=$(head -n1 $tmpfil)
        #echo "$tmpstr"
        if [ "$tmpstr" == "$1" ] || [ "${tmpstr##*/}" == "$1" ] || [ "${1##*/}" == "${0##*/}" ]
        then
        echo "ps -ef: active:"
        runn=true
        elif test -s "$tmpfil"
        then
        if $runn
        then
        echo "----- consider also ---------------------------------------------"
        cat "$tmpfil"
        echo "-----------------------------------------------------------------"
        else
        echo "----- try with: -------------------------------------------------"
        cat "$tmpfil"
        echo "-----------------------------------------------------------------"
        fi
        fi

        if $runn
        then
        echo -e "$greenback $1 is running $resetvid"
        else
        inpath=$(which "$1")
        if [ "$inpath" == "" ]
        then
        echo -e "$redback no path found to $1 $resetvid"
        else
        echo -e "$blueback $1 is not running $resetvid"
        fi
        fi


        Make it executable and put it in a directory in PATH, if you wish. I put it into my bin directory and can used it without any path.



        Usage:



        $ running 
        Usage: /home/sudodus/bin/running <program-name>
        /home/sudodus/bin/running <part of program name>
        Examples: /home/sudodus/bin/running firefox
        /home/sudodus/bin/running term
        /home/sudodus/bin/running dbus
        /home/sudodus/bin/running 'dbus-daemon --session'


        Examples:



        $ running firefox
        ps -ef: active:
        firefox is running # green background - running

        $ running term
        ----- try with: -------------------------------------------------
        /usr/lib/gnome-terminal/gnome-terminal-server
        xterm
        x-terminal-emulator
        -----------------------------------------------------------------
        no path found to term # red background - path not found

        $ running dbus
        systemctl is-active:
        ----- consider also ---------------------------------------------
        /usr/bin/dbus-daemon --session
        /usr/bin/dbus-daemon --syslog
        /usr/bin/dbus-daemon --system
        /usr/bin/fcitx-dbus-watcher unix:abstract=/tmp/dbus-Nm2MSvuTZF,guid=25bad8d51276d088045625055c425080
        -----------------------------------------------------------------
        dbus is running # green background

        $ running 'dbus-daemon --session'
        ps -ef: active:
        dbus-daemon --session is running # green background

        $ running libreoffice
        libreoffice is not running # blue background - not running





        share|improve this answer













        The following shellscript running combines the result of





        • systemctl is-active and

        • ps -ef | ... | grep


        in order to detect if a certain program (or a program name containing the search string) is running or not.



        #!/bin/bash

        if [ $# -ne 1 ]
        then
        echo "Usage: $0 <program-name>
        $0 <part of program name>
        Examples: $0 firefox
        $0 term
        $0 dbus
        $0 'dbus-daemon --session'"
        exit
        fi

        inversvid="033[7m"
        resetvid="033[0m"
        redback="033[1;37;41m"
        greenback="033[1;37;42m"
        blueback="033[1;37;44m"

        runn=false
        tmpfil=$(mktemp)

        # check by systemctl

        systemctl is-active --quiet "$1"
        if [ $? -eq 0 ]
        then
        echo "systemctl is-active:"
        runn=true
        fi

        # check by ps

        ps -ef | tr -s ' ' ' ' | cut -d ' ' -f 8-9 | grep "$1" | grep -vE -e "$0 $1" -e "grep $1" | sort -u > "$tmpfil"

        tmpstr=$(head -n1 $tmpfil)
        #echo "$tmpstr"
        if [ "$tmpstr" == "$1" ] || [ "${tmpstr##*/}" == "$1" ] || [ "${1##*/}" == "${0##*/}" ]
        then
        echo "ps -ef: active:"
        runn=true
        elif test -s "$tmpfil"
        then
        if $runn
        then
        echo "----- consider also ---------------------------------------------"
        cat "$tmpfil"
        echo "-----------------------------------------------------------------"
        else
        echo "----- try with: -------------------------------------------------"
        cat "$tmpfil"
        echo "-----------------------------------------------------------------"
        fi
        fi

        if $runn
        then
        echo -e "$greenback $1 is running $resetvid"
        else
        inpath=$(which "$1")
        if [ "$inpath" == "" ]
        then
        echo -e "$redback no path found to $1 $resetvid"
        else
        echo -e "$blueback $1 is not running $resetvid"
        fi
        fi


        Make it executable and put it in a directory in PATH, if you wish. I put it into my bin directory and can used it without any path.



        Usage:



        $ running 
        Usage: /home/sudodus/bin/running <program-name>
        /home/sudodus/bin/running <part of program name>
        Examples: /home/sudodus/bin/running firefox
        /home/sudodus/bin/running term
        /home/sudodus/bin/running dbus
        /home/sudodus/bin/running 'dbus-daemon --session'


        Examples:



        $ running firefox
        ps -ef: active:
        firefox is running # green background - running

        $ running term
        ----- try with: -------------------------------------------------
        /usr/lib/gnome-terminal/gnome-terminal-server
        xterm
        x-terminal-emulator
        -----------------------------------------------------------------
        no path found to term # red background - path not found

        $ running dbus
        systemctl is-active:
        ----- consider also ---------------------------------------------
        /usr/bin/dbus-daemon --session
        /usr/bin/dbus-daemon --syslog
        /usr/bin/dbus-daemon --system
        /usr/bin/fcitx-dbus-watcher unix:abstract=/tmp/dbus-Nm2MSvuTZF,guid=25bad8d51276d088045625055c425080
        -----------------------------------------------------------------
        dbus is running # green background

        $ running 'dbus-daemon --session'
        ps -ef: active:
        dbus-daemon --session is running # green background

        $ running libreoffice
        libreoffice is not running # blue background - not running






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 19 at 12:41









        sudodussudodus

        24.1k32875




        24.1k32875






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Ask Ubuntu!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1110915%2fbash-script-to-verify-if-a-process-is-running-is-not-working%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Human spaceflight

            Can not write log (Is /dev/pts mounted?) - openpty in Ubuntu-on-Windows?

            File:DeusFollowingSea.jpg