Start 2 commands, wait for the end of the second command, then kill the first command - bash

I'm trying to create a .sh script that does the following:
Start a first command ./first.sh
Start a second command ./second.sh
Wait for the end of the second command
Kill the first command
End of the script
I know how to start a command and i know how to start a command without blocking the script. But i don't know how to kill the first command with the second one is finished.

You can try this:
#!/bin/bash
./first.sh &
firstpid="$!"
./second.sh
kill "$firstpid"

Related

Pause script until command in other script is executed [Bash]

In script a I tell script b to execute a certain command.
Is there a way to pause script a until script b is done executing?
I don't want to use sleep, because every run of the script can be different.
Use:
wait <pid>
This will cause script A to wait for B to complete before continuing.

How to prevent nohup from "clogging" the command line?

I want to write a bash script that runs two commands in the background. I am using nohup for this:
nohup cmd1 &
nohup cmd2 &
However, only the 1st command runs in the background.
When I run nohup cmd1 & manually in the command line. First, I type nohup cmd1 & then hit enter; this starts the process:
But, then I need to hit enter again to be able to type another command:
I think this is "clogging" up the command line, and is causing my bash script to get stuck at the first nohup ... & command.
Is there a way to prevent this?
Nothing is "clogged". The first command, running in the background, prints some output after your shell prints its next prompt. The shell is waiting for you to type a command, even though the cursor is no longer on the same line as the prompt. That extra Enter is an empty command, causing the shell to print another prompt. It's harmless but unnecessary.
Let me say something to nohup because I'm not sure if you are certain about what it is doing. In short, the nohup command is not necessary to run a process in background. The ampersand at the end of the line is doing it.
nohup prevents the background process from receiving SIGHUP (hup for hang up) if you close the terminal where the starting shell runs it. SIGHUP would effectively terminate the process.
If started with nohup the process will not receive that event and will continue running, owned by the init process (pid 1) if the terminal will being closed.
Furthermore the nohup command will redirect standard output of the controlled process to a file, meaning it will not appear on screen any more. By default this file is called nohup.out.

`cat & wait` in a script proceeds immediately

From the command line, typing cat waits for user input.
But in the following script, wait ignores the background process.
#!/bin/bash
cat &
wait
echo "After wait"
This script immediately blasts right past the wait command. How can I make wait actually wait for the cat command to finish? I've tried waiting for the specific PID or job number, but the effect is the same.
That's because cat is exiting right away, because stdin is not inherited. Try this instead:
cat <&0 &

Nohup command to run a script and get control immediately for next script

I have used nohup command and execute a unix shell script from background but i want to execute the next command immediately before the previous shell script gets completed , i do not want to wait until and unless the shell script gets completed.
Is there any way , i tried with nohup but i m getting this:
nohup: appending output to `nohup.out'
and not getting control to run the next command. Is there any way to exit immediatley after calling a shell script and let it run in the background , execute the next command without using CTRL+C or force shutdown.
I have used the below command but
$ nohup sh dataload.sh &
[1] 14472
$ nohup: appending output to `nohup.out'
here I am not able to get the control to execute the next command
Just put it in the background: nohup your_command &

bash script order of execution

Do lines in a bash script execute sequentially? I can't see any reason why not, but I am really new to bash scripting and I have a couple commands that need to execute in order.
For example:
#!/bin/sh
# will this get finished before the next command starts?
./someLongCommand1 arg1
./someLongCommand2 arg1
Yes, they are executed sequentially. However, if you run a program in the background, the next command in your script is executed immediately after the backgrounded command is started.
#!/bin/sh
# will this get finished before the next command starts?
./someLongCommand1 arg1 &
./someLongCommand2 arg1 &
would result in an near-instant completion of the script; however, the commands started in it will not have completed. (You start a command in the background by putting an ampersand (&) behind the name.
Yes... unless you go out of your way to run one of the commands in the background, one will finish before the next one starts.

Resources