i have 4 important services running on machine want see them time. have simple bash script running bash profile.
echo proc="nginx mysql php-fpm pptpd" p in $proc ps cax | grep $p > /dev/null if [ $? -eq 0 ]; echo -e "\e[92m$p running\e[0m" else echo -e "\e[101m$p not running \e[0m" fi done echo
the out put of script is:
nginx running mysql running php-fpm running pptpd running
how can make this?
nginx running - mysql running - php-fpm running - pptpd running
build status lines first array, , print array:
status=() p in $proc if ps cax | grep -q $p; status+=( " \e[92m$p running\e[0m " ) else status+=( " \e[101m$p not running \e[0m " ) fi done (ifs=-; echo -e "${status[*]}")
${status[*]}
expands every element in array joined first character of ifs
, set -
earlier. note used subshell (ifs=-; echo ...)
, changing ifs
doesn't affect rest of script.
other notes:
ps cax | grep $p > /dev/null if [ $? -eq 0 ];
can combined to:
if ps cax | grep -q $p;
which more concise , readable. consider using pgrep
instead.
Comments
Post a Comment