how to capture command error message in variable for if block - linux

Hi below is my code for bash shell script, in this I want to capture error message for if clause, when it says either job is already running or unable to start the job to a variable, how it is possible in below script, or any other way for the below functionality
if initctl start $i ; then
echo "service $i started by script"
else
echo "not able to start service $i"
fi

You can for example use the syntax msg=$(command 2>&1 1>/dev/null) to redirect stderr to stdout after redirecting stdout to /dev/null. This way, it will just store stderr:
error=$(initctl start $i 2>&1 1>/dev/null)
if [ $? -eq 0 ]; then
echo "service $i started by script"
else
echo "service $i could not be started. Error: $error"
fi
This uses How to pipe stderr, and not stdout?, so that it catches stderr from initctl start $i and stores in $error variable.
Then, $? contains the return code of the command, as seen in How to check if a command succeeded?. If 0, it succeeded; otherwise, some errors happened.

Use '$?' variable
It stores any exit_code from the previous statement
See http://www.tldp.org/LDP/abs/html/exitcodes.html for more information
initctl start $i
retval=$?
if [ $retval -eq 0 ]; then
echo "service $i started by script"
else
echo "not able to start service $i"
fi

Related

Bash: Continuously check processes running or not with some parameters

I want to create script that will check continuously process status running or not. If process will not run, script should catch it and restart it in a seconds. How can I run continuously this script on the system and how should I change the script via ---> Important points: it should take as parameters these: 1-seconds to wait between attempts to restart service, 2- number of attempts and 3- generate logs of events. Nginx is a example for the process. It can be replaceable.
#!/bin/bash
SERVICE="nginx"
if pgrep -x "$SERVICE" >/dev/null
then
echo "$SERVICE is running"
else
echo "$SERVICE stopped"
# start nginx if stopped
echo "$SERVICE starting"
systemctl start $SERVICE
fi
You can try something like this:
#!/bin/bash
service="nginx"
seconds=2
retries=3
until (( retries-- == 0 ))
do
property=$(systemctl show --property MainPID "$service")
if [[ $property == MainPID=0 ]]
then
echo "$service stopped"
echo "$service starting"
systemctl start "$service" >& /dev/null
else
echo "$service is running"
exit
fi
sleep "$seconds"
done
echo "$service is broken"

Bash exit status not working in script

I've written a script that starts, stops and sends status of Apache, with messages dependent on the output of the commands.
I have most of it correct, but my errors are not printing out correctly. In other words, even if I do not have Apache loaded, "stopping" it still shows a successful message.
I need help getting my error messages to print when necessary.
#!/bin/bash
echo -e "\e[1;30mApache Web Server Control Script\e[0m"
echo
echo "Enter the operation number to perform (1-4): "
echo " 1 - Start the httpd server"
echo " 2 - Restart the httpd server"
echo " 3 - Stop the httpd server"
echo " 4 - Check httpd server status"
echo
echo -n "===> "
read NUMBER
EXITSTATUS=$?
echo
if [ $NUMBER -eq "1" ]; then
systemctl start httpd
if [ $EXITSTATUS -eq "0" ]; then
echo -e "\e[1;32mThe return value of the command 'systemctl
start httpd' was 0.\e[0m"
echo -e "\e[1;32mThe Apache web server was successfully
started.\e[0m"
else
echo -e "\e[1;31mThe return value of the command 'systemctl
start httpd' was 5.\e[0m"
echo -e "\e[1;31mThe Apache web server was not successfully
started.\e[0m"
fi
fi
if [ $NUMBER -eq "2" ]; then
systemctl restart httpd
if [ $EXITSTATUS -eq "0" ]; then
echo -e "\e[1;32mThe return value of the command 'systemctl
restart httpd' was 0.\e[0m"
echo -e "\e[1;32mThe Apache web server was successfully
restarted.\e[0m"
else
echo -e "\e[1;31mThe return value of the command 'systemctl
restart httpd' was 5.\e[0m"
echo -e "\e[1;31mThe Apache web server was not successfully
restarted.\e[0m"
fi
fi
if [ $NUMBER -eq "3" ]; then
systemctl stop httpd
if [ $EXITSTATUS -eq "0" ]; then
echo -e "\e[1;32mThe return value of the command 'systemctl
stop httpd' was 0.\e[0m"
echo -e "\e[1;32mThe Apache web server was successfully
stopped\e[0m."
else
echo -e "\e[1;31mThe return value of the command 'systemctl
stop httpd' was 5.\e[0m"
echo -e "\e[0;31mThe Apache web server was successfully
stopped.\e[0m"
fi
fi
if [ $NUMBER -eq "4" ]; then
systemctl status httpd
if [ $EXITSTATUS -eq "0" ]; then
msg=$(systemctl status httpd)
else
echo -e "\e[1;31mThe Apache web server is not currently
running.\e[0m"
echo $(msg)
fi
fi
if [[ $NUMBER != [1-4] ]]; then
echo -e "\e[1;31mPlease select a valid choice: Exiting.\e[0m"
fi
exit 0
The variable EXITSTATUS doesn't contain the exit code of the systemctl calls, but that of the read command. You could rewrite it either as
systemctl start httpd
EXITSTATUS=$?
if [ $EXITSTATUS -eq 0 ]; then
[...]
or more simply as
systemctl start httpd
if [ $? -eq 0 ]; then
[...]
Storing the value of $? in a variable is only necessary if you either want to use it afterwards in another place (e. g. as exit code of your own script), or have to make other calls before branching on the value.
You're not setting your variable $EXITSTATUS after running the commands, so it maintains its original value (the exit status of read NUMBER).
Since you only care about whether the command succeeded or not, better would be to avoid using it entirely and change the conditions to e.g.:
if systemctl restart httpd; then
# it was successful ($? would be 0)
fi

can shell script make itself run in background after running some steps?

I have BBB based custom Embedded Linux based board with busybox shell(ash)
I have a situation where my script must run in background with following condition
There must only one instance of the script.
wrapper script need to know if script started successfully in background or not.
There is another wrapper script which starts and stops my script, wrapper script is as mentioned below.
#!/bin/sh
export PATH=/bin:/sbin:/usr/bin:/usr/sbin
readonly TEST_SCRIPT_PATH="/home/testscript.sh"
readonly TEST_SCRIPT_LOCK_PATH="/var/run/${TEST_SCRIPT_PATH##*/}.lock"
start_test_script()
{
local pid_of_testscript=0
local status=0
#Run test script in background
"${TEST_SCRIPT_PATH}" &
#---------Now When this point is hit, lock file must be created.-----
if [ -f "${TEST_SCRIPT_LOCK_PATH}" ];then
pid_of_testscript=$(head -n1 ${TEST_SCRIPT_LOCK_PATH})
if [ -n "${pid_of_testscript}" ];then
kill -0 ${pid_of_testscript} &> /dev/null || status="${?}"
if [ ${status} -ne 0 ];then
echo "Error starting testscript"
else
echo "testscript start successfully"
fi
else
echo "Error starting testscript.sh"
fi
fi
}
stop_test_script()
{
local pid_of_testscript=0
local status=0
if [ -f "${TEST_SCRIPT_LOCK_PATH}" ];then
pid_of_testscript=$(head -n1 ${TEST_SCRIPT_LOCK_PATH})
if [ -n "${pid_of_testscript}" ];then
kill -0 ${pid_of_testscript} &> /dev/null || status="${?}"
if [ ${status} -ne 0 ];then
echo "testscript not running"
rm "${TEST_SCRIPT_LOCK_PATH}"
else
#send SIGTERM signal
kill -SIGTERM "${pid_of_testscript}"
fi
fi
fi
}
#Script starts from here.
case ${1} in
'start')
start_test_script
;;
'stop')
stop_test_script
;;
*)
echo "Usage: ${0} [start|stop]"
exit 1
;;
esac
Now actual script "testscript.sh" looks something like this,
#!/bin/sh
#Filename : testscript.sh
export PATH=/bin:/sbin:/usr/bin:/usr/sbin
set -eu
LOCK_FILE="/var/run/${0##*/}.lock"
FLOCK_CMD="/bin/flock"
FLOCK_ID=200
eval "exec ${FLOCK_ID}>>${LOCK_FILE}"
"${FLOCK_CMD}" -n "${FLOCK_ID}" || exit 0
echo "${$}" > "${LOCK_FILE}"
# >>>>>>>>>>-----Now run the code in background---<<<<<<
handle_sigterm()
{
# cleanup
"${FLOCK_CMD}" -u "${FLOCK_ID}"
if [ -f "${LOCK_FILE}" ];then
rm "${LOCK_FILE}"
fi
}
trap handle_sigterm SIGTERM
while true
do
echo "do something"
sleep 10
done
Now in above script you can see "---Now run the code in background--" at that point I am sure that either lock file is successfully created or instance of this script is already running. So Then I can safely run other code in background and wrapper script can check for lockfile and find out if the process mentioned in the lock file is running or not.
can shellscript itself make it to run in background ?
if not is there a better way to meet all the conditions ?
I think you can look into job control built-in, specifically bg.
Job Control Commands
When processes say they background themselves, what they actually do is fork and exit the parent. You can do the same by running whichever commands, functions or statements you want with & and then exiting.
#!/bin/sh
echo "This runs in the foreground"
sleep 3
while true
do
sleep 10
echo "doing background things"
done &

Check all commands exit code within a bash script

Consider the case where I have a very long bash script with several commands. Is there a simple way to check the exit status for ALL of them easily. So if there's some failure I can show which command has failed and its return code.
I mean, I don't want to use the test for each one of them checks like the following:
my_command
if [ $status -ne 0 ]; then
#error case
echo "error while executing " my_command " ret code:" $?
exit 1
fi
You can do trap "cmd" ERR, which invokes cmd when a command fails. However this solution has a couple of drawbacks. For example, it does not catch a failure inside a pipe.
In a nutshell, you are better off doing your error management properly, on a case by case basis.
You can write a function that launches:
function test {
"$#"
local status=$?
if [ $status -ne 0 ]; then
echo "error with $1" >&2
fi
return $status
}
test command1
test command2
One can test the value of $? or simply put set -e at the top of the script which will cause it to exit upon any command which errors.
#!/bin/sh
set -xe
my_command1
# never makes it here if my_command1 fails
my_command2

Bash script to check multiple running processes

I'm made the following code to determine if a process is running:
#!/bin/bash
ps cax | grep 'Nginx' > /dev/null
if [ $? -eq 0 ]; then
echo "Process is running."
else
echo "Process is not running."
fi
I would like to use my code to check multiple processes and use a list as input (see below), but getting stuck in the foreach loop.
CHECK_PROCESS=nginx, mysql, etc
What is the correct way to use a foreach loop to check multiple processes?
If your system has pgrep installed, you'd better use it instead of the greping of the output of ps.
Regarding you're question, how to loop through a list of processes, you'd better use an array. A working example might be something along these lines:
(Remark: avoid capitalized variables, this is an awfully bad bash practice):
#!/bin/bash
# Define an array of processes to be checked.
# If properly quoted, these may contain spaces
check_process=( "nginx" "mysql" "etc" )
for p in "${check_process[#]}"; do
if pgrep "$p" > /dev/null; then
echo "Process \`$p' is running"
else
echo "Process \`$p' is not running"
fi
done
Cheers!
Use a separated list of of processes:
#!/bin/bash
PROC="nginx mysql ..."
for p in $PROC
do
ps cax | grep $p > /dev/null
if [ $? -eq 0 ]; then
echo "Process $p is running."
else
echo "Process $p is not running."
fi
done
If you simply want to see if either one of them is running, then you don't need loo. Just give the list to grep:
ps cax | grep -E "Nginx|mysql|etc" > /dev/null
Create file chkproc.sh
#!/bin/bash
for name in $#; do
echo -n "$name: "
pgrep $name > /dev/null && echo "running" || echo "not running"
done
And then run:
$ ./chkproc.sh nginx mysql etc
nginx: not running
mysql: running
etc: not running
Unless you have some old or "weird" system you should have pgrep available.

Resources