I have three scripts: mysql_monitor.sh, sip_monitor.sh, and check_docker.sh that each perform some test and show a message where the status is 'red' if not there and 'green' if not. These scripts are in another script called newmain.bash that is run inside a geek_scripts shell. If I run newmain.bash from the command line, then it detects if Docker is running or not, and puts the correct colors and highlighting on each of them. However, when it runs from the geek_scripts shell, it does not detect that Docker is running or not, always saying it is not running. Further, only the mysql_monitor.sh colors are correct. The others are NOT highlighted, but are muted.
Here are the scripts:
cat geek_scripts/mysql_monitor.sh#!/bin/bash
#br="$(tput -S <<<$'setaf 1\nbold\n')" # Bold Red
br="\033[1;31m";
#bg="$(tput -S <<<$'setaf 2\nbold\n')" # Bold Green
bg="\033[1;32m";
#ar="$(tput sgr0)" # Text attributes reset
ar="\033[0m ";
UP=$(pgrep mysqld | wc -l);
if [ $UP != 1 ];
then
printf "MYSQL is ${br}down.${ar}\n";
else
printf "MySQL is ${bg}running.${ar}\n";
fi
cat geek_scripts/sip_monitor.sh
#!/bin/bash
#br="$(tput -S <<<$'setaf 1\nbold\n')" # Bold Red
br="\033[1;31m";
#bg="$(tput -S <<<$'setaf 2\nbold\n')" # Bold Green
bg="\033[1;32m";
#ar="$(tput sgr0)" # Text attributes reset
ar="\033[0m ";
#
# Monitor the status of the System Integrity Protection (SIP)
#
#printf "`csrutil status | grep --color 'disabled'`\n";
if csrutil status | grep 'disabled' &> /dev/null; then
printf "System Integrity Protection status: ${br}disabled${ar}\n";
else
printf "System Integrity Protection status: ${bg}enabled${ar}\n";
fi
and
cat geek_scripts/check_docker.sh
#!/bin/bash
#br="$(tput -S <<<$'setaf 1\nbold\n')" # Bold Red
br="\033[1;31m";
#bg="$(tput -S <<<$'setaf 2\nbold\n')" # Bold Green
bg="\033[1;32m";
#ar="$(tput sgr0)" # Text attributes reset
ar="\033[0m ";
#
# Check if docker is available and running
#
## will throw and error if the docker daemon is not running and jump
## to the next code chunk
docker_state=$(docker info >/dev/null 2>&1)
if [[ $? -ne 0 ]]; then
printf "Docker is ${br}not running.${ar}\n";
else
printf "Docker is ${bg}running.${ar}\n";
fi
These are called from newmain.bash as:
cat geek_scripts/check_docker.sh
#!/bin/bash
#br="$(tput -S <<<$'setaf 1\nbold\n')" # Bold Red
br="\033[1;31m";
#bg="$(tput -S <<<$'setaf 2\nbold\n')" # Bold Green
bg="\033[1;32m";
#ar="$(tput sgr0)" # Text attributes reset
ar="\033[0m ";
#
# Check if docker is available and running
#
## will throw and error if the docker daemon is not running and jump
## to the next code chunk
docker_state=$(docker info >/dev/null 2>&1)
if [[ $? -ne 0 ]]; then
printf "Docker is ${br}not running.${ar}\n";
else
printf "Docker is ${bg}running.${ar}\n";
fi
The file geek_scripts/newmain.bash also has #!/bin/bash as the first line.
I'd like to understand what is happening and also how to get what I want, i.e. check_docker.sh show whether Docker is running or not from the command line, properly colored and in bold. Why doesn't it show the colors high lighted and above all, why doesn't it determine whether Docker is running correctly?
I know I have explicit color codes and will factor those out at some point, but they work when run in each individual file from the command line, AND also when I run newmain.bash from the command line, viz.
geek_scripts/newmain.bash
Fri Jul 19 20:04:09 EDT 2019
woo-va-air
Mac OS X 10.15 19A512f
8gb RAM
Intel(R) Core(TM) i7-3667U CPU @ 2.00GHz
Docker is not running.
MySQL is running.
System Integrity Protection status: disabled
up : 1 day, 10:27, RAM : , sys, 89.77% idle user sys
CPU usage: 35.64
PhysMem: 7387M used
Disk Space:
Used: 10% Available: 93Gi
Used: 59% Available: 93Gi
Used: 3% Available: 93Gi
Used: 40% Available: 1.0Ti
Latest: 2019-07-19-195351
The 'is not running', 'is running', and 'disabled' are colored bold-red, bold-green, and bold-red appropriately from the command line. In the geek-scripts display on the screen, only the MySQl message is bold-green, the others are regular-red, but again, if Docker is running, the command line shows it running, but the geek_script shows it as not running.
Why? and how to fix?
I found that the use of $? worked when I was running the check_docker.sh from the command line, but not when it was running inside another script run by Geek Tool. I took the advice I found in another question about using variables in if, then, else scripts and changed the check_docker.sh to read:
(610)[:~/bin/geek_scripts] >
cat check_docker.sh
#!/bin/bash
#br="$(tput -S <<<$'setaf 1\nbold\n')" # Bold Red
br="\033[1;31m";
#bg="$(tput -S <<<$'setaf 2\nbold\n')" # Bold Green
bg="\033[1;32m";
#ar="$(tput sgr0)" # Text attributes reset
ar="\033[0m ";
#
# Check if docker is available and running
#
## will throw and error if the docker daemon is not running and jump
## to the next code chunk
/usr/local/bin/docker info > ~/tmp/docker_status 2>&1
if grep -q "Cannot connect" ~/tmp/docker_status; then
printf "Docker is ${br}not running.${ar}\n";
else
printf "Docker is ${bg}running.${ar}\n";
fi
/bin/rm ~/tmp/docker_status;
Note that I replace the $? with the expression:
if grep -q "Cannot connect" ~/tmp/docker_status; then
and preceded that with:
/usr/local/bin/docker info > ~/tmp/docker_status 2>&1
I don't like having to write to the disk each time I evaluate this, but could not find a way to get the standard out and error out into a variable that I could use in the following grep statement. Would appreciate someone telling me how to do this. However, this works, works very well, and will do until I find a better way. From what I read the $? is the return code of the previous command run in the foreground. Perhaps, running in Geek Tool as a geek script does not run that original command in the foreground?
So, best advice when using scripts within scripts is to evaluate the functions in-line rather than collecting their value into a variable unless you can figure out how to get both std-out and error-out into the same variable.