Restart Services Automatically
As you and I both know, what follows is the answer I posted to your question at Unix and Linux. I'm not sure if this might be considered in bad taste or not - and if so, I'll happily delete it - but at the other site the question was apparently closed as a cross-post in favor of this. And so I thought - maybe it belonged? Anyway, here goes:



What you're really doing wrong is duplicating your effort - basically every hardcoded occurrence of _agent or _server appears to be completely redundant. 

For example, if this is being run on a linux system, you can completely drop the grep_...() functions, and consolidate both check_...s into a single entity which might work like:

email(){ 
    mutt -s "Zabbix Auto-restart Script Just Ran" \&lt;user email\&gt;
}
prlog(){ 
    date +"%x %X:%tservice $1${2+%n************************}"
}
chk_run()
    while  [ "$#" -gt 0 ]
    do     if     ps -C zabbix_"$1"
           then   : "$(($1=1))"
           else   set zabbix_"$@"
                  service "$1" start || eval &gt;&amp;2 '
                  prlog   "$1 restart failed."  +; exit '"$?"
                  prlog   "$1 restarted."       +  &gt;&amp;2
                  prlog   "$1 restarted from $0." |email
           fi;    shift
    done


The key to that is you would just call chk_run with an argument list each member of which would indicate to it what it should be checking each iteration.

loop()
    until [ "$(($1&amp;&amp;$2))" -eq 1 ]
    do    chk_run "$@"
          sleep 2
    done  &gt;/dev/null 2&gt;&gt;"$log"
agentd=0 server=0 loop agentd server


POSIXly the only thing that should need altering there is the ps command - because POSIX doesn't specify the -C switch. And so you could just change the if line to look like:

if    ps -eocomm= |
      grep -xqF zabbix_"$1"


Aside from mutt, service, and the ps optimization, it should all be standard command language. At least one advantage to that is the #!/bin/bash hash-bang is completely unnecessary - there is no anchor here to some shell-specific extension, and so it should work pretty much exactly the same in all shells which strive for POSIX-compliance. That means that #!/bin/dash is a very simple optimization in this case.