Exit from Bash Script But Keep The Process Running

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.

Any way to exit bash script, but not quitting the terminal

The "problem" really is that you're sourcing and not executing the script. When you source a file, its contents will be executed in the current shell, instead of spawning a subshell. So everything, including exit, will affect the current shell.

Instead of using exit, you will want to use return.

How to wait in bash for several subprocesses to finish, and return exit code !=0 when any subprocess ends with code !=0?

wait also (optionally) takes the PID of the process to wait for, and with $! you get the PID of the last command launched in the background.
Modify the loop to store the PID of each spawned sub-process into an array, and then loop again waiting on each PID.

# run processes and store pids in array
for i in $n_procs; do
./procs[${i}] &
pids[${i}]=$!
done

# wait for all pids
for pid in ${pids[*]}; do
wait $pid
done

Exit running script without exiting shell

Instead of launching your script as . script.sh, you can launch it bash script.sh. When you launch it with bash then a child process for bash will be opened and your script will execute in the child shell and exit statements will make child shell closed and have no scope for parent shell or main shell.

In a Bash script, how can I exit the entire script if a certain condition occurs?

Try this statement:

exit 1

Replace 1 with appropriate error codes. See also Exit Codes With Special Meanings.

Exit a bash script when one of the subprocesses exits

The following script monitors test child processes (in the example, sleep+false and sleep+true) and reports their PID and exit code:

#!/bin/bash

set -m

trap myhandler CHLD

myhandler() {
echo sigchld received
cat /tmp/foo
}

( sleep 5; false; echo "exit p1=$?" ) > /tmp/foo &
p1=$!
echo "p1=$p1"

( sleep 3; true; echo "exit p2=$?" ) > /tmp/foo &
p2=$!
echo "p2=$p2"

pstree -p $$
wait

The result is:

p1=3197
p2=3198
prueba(3196)─┬─prueba(3197)───sleep(3199)
├─prueba(3198)───sleep(3201)
└─pstree(3200)
sigchld received
sigchld received
exit p2=0
sigchld received
exit p1=1

It could be interesting to use SIGUSR1 instead of SIGCHLD; see here for an example: https://stackoverflow.com/a/12751700/4886927.

Also, inside the trap handler, it is posible to verify which child is still alive. Something like:

myhandler() {
if kill -0 $p1; then
echo "child1 is alive"
fi
if kill -0 $p2; then
echo "child2 is alive"
fi
}

or kill both childs when one of them dies:

myhandler() {
if kill -0 $p1 && kill -0 $p2; then
echo "all childs alive"
else
kill -9 $p1 $p2
fi
}

exiting shell script with background processes

From memory a login shell will be kept around even when it finishes if any of its still running children have standard (terminal) file handles open. Normal (sub process) shells do not seem to suffer from this. So see if changing your nohup line to the following makes any difference.

nohup myInScript.sh >some.log 2>&1 </dev/null &

On Centos5 I do not get the problem if I run parent.sh. But I do if I run ssh localhost parent.sh. In this case the I/O redirection I showed above solves the problem.



Related Topics



Leave a reply



Submit