I have scripts a.sh and b.sh in which i have pass the ip as argument.
I tried running it as
sh -x a.sh 172.19.57.21 & b.sh 172.19.57.21 &
But I see only first script runs.
When you run sh -x a.sh 172.19.57.21 & b.sh 172.19.57.21 &:
sh -x a.sh 172.19.57.21 is one command, & sends it to background immediately
b.sh 172.19.57.21 is another command, again & puts it in background
The problem seems to me is that the script b.sh is not executable and as you are not running it as an argument to shell (unlike a.sh), it is failed in the PATH search.
You can run b.sh as shell's argument as well e.g:
sh a.sh 172.19.57.21 & sh b.sh 172.19.57.21 &
Or if both scripts are executables and have proper shebang:
./a.sh 172.19.57.21 & ./b.sh 172.19.57.21 &
I would recommend a wrapper to get the argument IP address once, and call required scripts from the wrapper, something like a tiny function would do:
wrapper() {
/path/to/a.sh "$#" &
/path/to/b.sh "$#" &
}
Now, you can just do e.g.:
wrapper 172.19.57.21
use ; between commands like
sh -x a.sh 172.19.57.21 &; sh -x b.sh 172.19.57.21 &
Related
Lets say I have 3 shell scripts First.sh , Second.sh and Third.sh. Then I created
Forth.sh with the content
nohup sh First.sh &
nohup sh Second.sh &
nohup sh Third.sh &
How to save PID created by each of the nohup command when I run nohup sh Forth.sh ?
thanks for you time 😊
Try $! right after you issue the nohup. It should return the child-pid. It will be overwritten every time you fork a child process.
Alternatively, you could save $$ from the child process (e.g. First.sh, Second.sh ...)
nohup sh First.sh &
FIRST_PID=$!
nohup sh Second.sh &
SECOND_PID=$!
nohup sh Third.sh &
THIRD_PID=$!
I have 3 shell scripts a.sh, b.sh and c.sh. the scripts a.sh and b.sh are to be run parallely and script c.sh should only run if a.sh and b.sh are run successfully ( with exit code 0).
Below is my code. The parallel execution is working fine but sequential execution of c.sh is out of order. It is executing after completion of a.sh and b.sh even if both the scripts are not returning exit codes 0. Below is the code used.
#!/bin/sh
A.sh &
B.sh &
wait &&
C.sh
How this can be changed to meet my requirement?
#!/bin/bash
./a.sh & a=$!
./b.sh & b=$!
if wait "$a" && wait "$b"; then
./c.sh
fi
Hey i did some testing, in my a.sh i had an exit 255, and in my b.sh i had an exit 0, only if both had an exitcode of 0 it executed c.sh.
you can try this :
.....
wait &&
if [ -z "the scripts a.sh or any commande u need " ]
then
#do something
C.sh
fi
I'm trying to debug my scripts. For example a.sh call b.sh.
#a.sh
echo "in a.sh"
source b.sh
#b.sh
echo "in b.sh"
If I'm sure b.sh is OK and just want to debug a.sh, I run as
bash -x a.sh
How to disable the display '-x' setting in b.sh, maybe modify b.sh as
#b.sh
x_option=$(get -x) # if there is such function
set +x
echo "in b.sh"
[ $x_optoin = 1 ] && set -x
From the bash reference manual:
The current set of options may be found in $-.
Which means you can look in that value for the current state of -x.
This question already has answers here:
Call a function using nohup
(6 answers)
Closed 9 years ago.
My question is similar to this one.
Using aliases with nohup
I took a lot of time customizing a function that I included in my .bashrc
I'd like it to run with nohup, because I'd like to run a command several times in this fashion.
for i in `cat mylist`; do nohup myfunction $i 'mycommand' & done
Any tips?
You can do this with functions (not aliases) by nohuping a bash -c (which is essentially the same as running an external bash script).
In order for this to work, you need to mark your function as exported:
# define the function
echo_args() {
printf '<%s> ' "$#"
printf "\n"
}
# mark it as exported
declare -fx echo_args
# run it with nohup
nohup bash -c 'echo_args "$#"' bash_ "an argument" "another argument"
The argument bash_ to nohup provides a "name" for the bash -c subshell; that is, it becomes the value of $0 in the subshell. It will be prepended to error messages (if any), so I try to use something meaningful.
nohup will not work with functions. You need to create a shell script which wraps and executes the function. The shell script then you can run with nohup
Like this:
test.sh
#!/bin/bash
function hello_world {
echo "hello $1, $2"
}
# call function
hello_world "$1" "$2"
chmod +x test.sh and then call it in your for loop:
for i in `cat mylist`; do
nohup ./test.sh $i 'mycommand' &
done
I have a large script called mandacalc which I want to always run with the nohup command. If I call it from the command line as:
nohup mandacalc &
everything runs swiftly. But, if I try to include nohup inside my command, so I don't need to type it everytime I execute it, I get an error message.
So far I tried these options:
nohup (
command1
....
commandn
exit 0
)
and also:
nohup bash -c "
command1
....
commandn
exit 0
" # and also with single quotes.
So far I only get error messages complaining about the implementation of the nohup command, or about other quotes used inside the script.
cheers.
Try putting this at the beginning of your script:
#!/bin/bash
case "$1" in
-d|--daemon)
$0 < /dev/null &> /dev/null & disown
exit 0
;;
*)
;;
esac
# do stuff here
If you now start your script with --daemon as an argument, it will restart itself detached from your current shell.
You can still run your script "in the foreground" by starting it without this option.
Just put trap '' HUP on the beggining of your script.
Also if it creates child process someCommand& you will have to change them to nohup someCommand& to work properly... I have been researching this for a long time and only the combination of these two (the trap and nohup) works on my specific script where xterm closes too fast.
Create an alias of the same name in your bash (or preferred shell) startup file:
alias mandacalc="nohup mandacalc &"
Why don't you just make a script containing nohup ./original_script ?
There is a nice answer here: http://compgroups.net/comp.unix.shell/can-a-script-nohup-itself/498135
#!/bin/bash
### make sure that the script is called with `nohup nice ...`
if [ "$1" != "calling_myself" ]
then
# this script has *not* been called recursively by itself
datestamp=$(date +%F | tr -d -)
nohup_out=nohup-$datestamp.out
nohup nice "$0" "calling_myself" "$#" > $nohup_out &
sleep 1
tail -f $nohup_out
exit
else
# this script has been called recursively by itself
shift # remove the termination condition flag in $1
fi
### the rest of the script goes here
. . . . .
the best way to handle this is to use $()
nohup $( command1, command2 ...) &
nohup is expecting one command and in that way You're able to execute multiple commands with one nohup