Bash script runs one command before previous. I want them one after the other
The problem here is that your command in backticks is being run locally, not on the remote end of the SSH connection. Thus, it runs before you've even connected to the remote system at all! (This is true for all expansions that run in double-quotes, so the $foo
in echo $foo
as well).
Use a quoted heredoc to protect your code against local evaluation:
ssh user@$remoteServer bash -s <<'EOF'
cd ~/a/b/c/;
echo -e 'blah blah'
sleep 1 # Added this just to make sure it waits.
foo=`grep something xyz.log |sed 's/something//g' |sed 's/something-else//g'`
echo $foo > ~/xyz.list
exit
EOF
If you want to pass through a variable from the local side, the easy way is with positional parameters:
printf -v varsStr '%q ' "$varOne" "$varTwo"
ssh "user@$remoteServer" "bash -s $varsStr" <<'EOF'
varOne=$1; varTwo=$2 # set as remote variables
echo "Remote value of varOne is $varOne"
echo "Remote value of varTwo is $varTwo"
EOF
Is it possible for bash commands to continue before the result of the previous command?
Yes, if you do nothing else then commands in a bash script are serialized. You can tell bash to run a bunch of commands in parallel, and then wait for them all to finish, but doing something like this:
command1 &
command2 &
command3 &
wait
The ampersands at the end of each of the first three lines tells bash to run the command in the background. The fourth command, wait
, tells bash to wait until all the child processes have exited.
Note that if you do things this way, you'll be unable to get the exit status of the child commands (and set -e
won't work), so you won't be able to tell whether they succeeded or failed in the usual way.
The bash manual has more information (search for wait
, about two-thirds of the way down).
Bash script running program lines one after another
I think Bash is trying to eat some of your Python options. To fix that, wrap each line in quotes and put eval
at the front:
#!/bin/bash
eval 'python /path/to/someprogramm.py analyze --probes /path/to/myfile.bed --rpkm_dir /path/to/RPKM/ --output /path/to/hdf5/analysis.hdf5 --write_svals /path/to/SVD/singular_values.txt'
eval 'python /path/to/someprogramm.py call --input /path/to/hdf5/analysis.hdf5 --output /path/to/calls.txt'
eval 'python /path/to/someprogramm.py plot --input /path/to/hdf5/analysis.hdf5 --calls /path/to/calls.ng.txt --outputdir /path/to/call_images/'
echo 'Command sequence finished succesfully'
Run one command after another, even if I suspend the first one (Ctrl-z)
The following should do it:
(command1; command2)
Note the added parentheses.
Run a script in parallel until another one finishes
You can use this loop:
#!/bin/bash
python A.py &
while [[ $(jobs -pr) ]]; do
python B.py
done
jobs -pr
lists the process IDs (-p
) of running jobs (-r
). If the output is empty, the backgrounds command has finished.
Execute a job only when the previous has finished
I assume you are in a linux environment so you should be able to use the run-one
command ( ubuntu run-one ) in conjunction with you bash script in a crontab.
e.g.
* * * * * cd /path/to/your/script && run-one ./your-script.sh
If not available you should be able to install if with your package manager
how to wait for first command to finish?
Shell scripts, no matter how they are executed, execute one command after the other. So your code will execute results.sh
after the last command of st_new.sh
has finished.
Now there is a special command which messes this up: &
cmd &
means: "Start a new background process and execute cmd
in it. After starting the background process, immediately continue with the next command in the script."
That means &
doesn't wait for cmd
to do it's work. My guess is that st_new.sh
contains such a command. If that is the case, then you need to modify the script:
cmd &
BACK_PID=$!
This puts the process ID (PID) of the new background process in the variable BACK_PID
. You can then wait for it to end:
while kill -0 $BACK_PID ; do
echo "Process is still active..."
sleep 1
# You can add a timeout here if you want
done
or, if you don't want any special handling/output simply
wait $BACK_PID
Note that some programs automatically start a background process when you run them, even if you omit the &
. Check the documentation, they often have an option to write their PID to a file or you can run them in the foreground with an option and then use the shell's &
command instead to get the PID.
Run bash script from another script without waiting for script to finish executing?
Put &
at the end of the line.
./script1.sh & #this doesn't blocks!
./script2.sh
How to run a command before a Bash script exits?
Here's an example of using trap:
#!/bin/bash -e
function cleanup {
echo "Removing /tmp/foo"
rm -r /tmp/foo
}
trap cleanup EXIT
mkdir /tmp/foo
asdffdsa #Fails
Output:
dbrown@luxury:~ $ sh traptest
t: line 9: asdffdsa: command not found
Removing /tmp/foo
dbrown@luxury:~ $
Notice that even though the asdffdsa line failed, the cleanup still was executed.
Start script after another one (already running) finishes
Polling is probably the way to go, but it doesn't have to be horrible.
pid=$(ps -opid= -C your_script_name)
while [ -d /proc/$pid ] ; do
sleep 1
done && ./your_other_script
Related Topics
How to Find Lines Containing a String in Linux
Rm Fails to Delete Files by Wildcard from a Script, But Works from a Shell Prompt
Remove the Last Page of a PDF File Using PDFtk
Listen on a Network Port and Save Data to a Text File
Difference Between Netstat and Ss in Linux
How to View Log Files in Linux and Apply Custom Filters While Viewing
How to Count Number of Unique Values of a Field in a Tab-Delimited Text File
Difference Between Vm.Dirty_Ratio and Vm.Dirty_Background_Ratio
How to Make Stdout and Stderr Output Be of Different Colors in Xterm or Konsole
How to Speed Up Linux Kernel Compilation
Why Does the Linux Kernel Repository Have Only One Branch
What Is the Reason and How to Avoid the [Fin, Ack] , [Rst] and [Rst, Ack]
How to Monitor Data on a Serial Port in Linux
Linux Distribution Binary Compatibility
Building Linux Kernel on MAC Os X