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
Are There Any Standard Exit Status Codes in Linux
How to Use Sed to Extract Substring
How to Fix Java.Lang.Module.Findexception: Module Java.Se.Ee Not Found
How to Write Standard Error to a File While Using "Tee" With a Pipe
Filter Log File Entries Based on Date Range
How to Convert Dos/Windows Newline (Crlf) to Unix Newline (Lf)
How to Keep Environment Variables When Using Sudo
How to Tar a Directory Without Retaining the Directory Structure
How to Find Which Position a Word Is in a String
How to Reload Google Chrome Tab from Terminal
How to Change the Output Color of Echo in Linux
How to Send a File as an Email Attachment Using Linux Command Line
Mount Smb/Cifs Share Within a Docker Container
How to Display Number to Two Decimal Places in Bash Function
How to Extract Duration Time from Ffmpeg Output
Passing Variable to Bash Script in a Jenkins Pipeline Job
How to Obtain the Number of Cpus/Cores in Linux from the Command Line