My Shell Script Stops After Exec

My shell script stops after exec

exec replaces the shell process. Remove it if you only want to call the command as a subprocess instead.

No code will run after `exec` in bash script

Because the exec command will replace the current bash process with the new command to be executed. It does not return to the calling process. So you should not use exec in your script.

  exec [-cl] [-a name] [command [arguments]]

If command is specified, it replaces the shell. No new process
is created. The arguments become the arguments to command. If
the -l option is supplied, the shell places a dash at the
beginning of the zeroth argument passed to command. This is
what login(1) does. The -c option causes command to be executed
with an empty environment. If -a is supplied, the shell passes
name as the zeroth argument to the executed command. If command
cannot be executed for some reason, a non-interactive shell
exits, unless the execfail shell option is enabled. In that
case, it returns failure. An interactive shell returns failure
if the file cannot be executed. If command is not specified,
any redirections take effect in the current shell, and the
return status is 0. If there is a redirection error, the return
status is 1.

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

Making bash script continue after exec $SHELL

It appears that you're trying to achieve multiple objectives by modifying the .bashrc file then calling exec $SHELL. Neither of those actions will modify the shell-in-which-this-script-is-running. To modify the current shell, you want to "source" the .bashrc file. Use the "dot notation" instead of calling exec $SHELL:

. ~/.bashrc

Good luck with this one!

My bash script won't execute commands after kill command

This should correct your issue :

PID=$( ps -ef | grep -E '[ ]logstash[ ]' | grep -v "grep" | head -1 | awk '{print $2}')
echo $PID
kill -9 $PID
echo "logstash process is stopped"
rm /home/user/test.csv
echo "test.csv is deleted."
rm /home/example.txt
echo "example.txt is deleted."

Regards!

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

Bash script execution suddenly stops after executing chmod

You are almost certainly misusing exec. It replaces the current process with a new one, which does the first chmod exits. Remove all the 'exec' calls from your script and just run the commands without it.

i.e.

    exec sudo chmod 775 $1 -R

to just

    sudo chmod 775 $1 -R

and so on.



Related Topics



Leave a reply



Submit