Shell Script Continues to Run Even After Exit Command

Shell script continues to run even after exit command

You're running echo and exit in subshells. The exit call will only leave that subshell, which is a bit pointless.

Try with:

#! /bin/sh
if [ $EUID -ne 0 ] ; then
echo "This script must be run as root" 1>&2
exit 1
fi
echo hello

If for some reason you don't want an if condition, just use:

#! /bin/sh
[ $EUID -ne 0 ] && echo "This script must be run as root" 1>&2 && exit 1
echo hello

Note: no () and fixed boolean condition. Warning: if echo fails, that test will also fail to exit. The if version is safer (and more readable, easier to maintain IMO).

Exit from bash script but keep the process running

Run that process in background:

#!/bin/sh
(php /home/stjc/app/artisan queue:listen --timeout=60 --tries=5) &

try adding an ampersand(&) at the end with brackets on either side of original command.

Edit:

: is a shell builtin which means NOP depending on your OS it might a problem try escaping the it in the php command and see if it works for you

#!/bin/sh
(php /home/stjc/app/artisan queue\:listen --timeout=60 --tries=5) &

also puting the full path to your php interpreter is strongly advised.

BASH script continues past EXIT command

An easy way to get around the problem that the while loop runs in a subprocess is to perpetrate a reversal: have while run in the same shell, an a sub-process to generate the files.

But how can we do that, if the receiver of the filenames from find has to be on the right of the | operator? The answer is that in GNU Bash, we have a language extension called "process substitution".

Process substitution is a piece of syntax that Bash converts into some string that looks looks and behaves like a file name, and is accepted that way by a command. When the program opens and reads that file (or, in the other direction, writes it), it communicates through a pipe with another process.

Sketch of the idea:

   while IFS= read -r -d '' MD5_FILESPEC; do
MD5_BASE=$(basename "$MD5_FILESPEC")
if [ "${#MD5_BASE}" -gt "$JOLIET_MAX" ]; then
myExit "[FAIL] Filename size ${#MD5_BASE} too long - $MD5_FILESPEC"
fi
done < <(find . -type f -print0)
# ^^^^^^^^^^^^^^^^^^^^^^^^^ this is the process substitution

Shell script leaving process after successful execution

It is mostly like due to the use of nohup utility. The problem with the using the command is, it forks a new process every time it is invoked from start_Server() function call.

From the man page

 nohup   No Hang Up. Run a command immune to hangups, runs the given 
command with hangup signals ignored, so that the command can
continue running in the background after you log out.

To kill all the process started by nohup you probably need to get the process id of the command started and kill it at the end of the script.

 /usr/bin/nohup $( ${SERVER_HOME}/bin/server.sh >> ${NOHUP_LOG_FILE} 2>&1 ) &
echo $! >> save_pid.txt # Add this line

At the end of the script.

 sendEmail

while read p; do
kill -9 $p
done <save_pid.txt

Why does my script suddenly exit after a command?

You enabled set -e aka errexit.

Your script will exit if one of the commands returns a non-zero exit code, and it may not always be obvious which command specifically fails:

  • Some may print a helpful error identifying itself and the problem
  • Some (like wget) may briefly mention an error way back in pagefuls of output
  • Some (like grep) may not show errors or any output at all, the script just exits

To know which command is causing a problem, run script with -x aka xtrace:

bash -x script.sh

Or add set -x to the script itself:

set -x
set -e
...

This will cause the script to print out each command being executed, so you can see which one was the last.

If you would like to ignore the exit status of a command, you can add || true:

# Causes exit if you lack read permission on any directory
find . -name '*.sh'

# Does not cause the script to exit
find . -name '*.sh' || true

If you would like to be alerted when set -e would trigger in your script, you can set a trap:

#!/bin/bash
set -e
# Show error if commands exit with non-zero
trap 'ret=$?; echo "$0:$LINENO: Error: set -e triggered"; exit $ret' ERR
# Would have failed silently
grep doesnotexist /etc/passwd
echo "This does not run"

When executed:

$ ./foo
./foo:6: Error: set -e triggered

bash script using && not stopping on error

The && only apply to next command, for a sequence, braces must be added:

#!/bin/bash
mkdir /root/simulatecomplexcommandthatreturns1 && {
sleep 5m
echo "let's go ahead and delete all the stuff"
find /blah/ -delete
}

or to avoid indent level the condition can be inverted

#!/bin/bash
mkdir /root/simulatecomplexcommandthatreturns1 || {
echo "something goes wrong"
exit 1
}

# ok, continue
sleep 5m
echo "let's go ahead and delete all the stuff"
find /blah/ -delete

Unable to exit shell script when using exec command

I did not find any elegant solution using exec. So i used xargs in find command and it is working perfectly fine. Shell exits with appropriate error message. I used this as my reference https://unix.stackexchange.com/questions/571215/force-xargs-to-stop-on-first-command-error

#!/usr/bin/env bash
set -euo pipefail
shopt -s execfail

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

echo "Do you want to Continue: [Yes/No]"

read action

if [ $action = "Yes" ]
then

echo "Executing scripts"
find ${SCRIPT_DIR}/folder2 -type f -name '*.sh' | xargs -I {} sh -c 'bash "$1" || exit 255' sh {}
echo $?
echo "This should also not be printed"

else
echo "nothing"
exit 1
fi


Related Topics



Leave a reply



Submit