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

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 &

Related

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

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"

Bash script to run a detached loop that sequentially starts backgound processes

I am trying to run a series of tests on a remote Linux server to which I am connecting via ssh.
I don't want to have to stay logged in the ssh session during the runs -> nohup(?)
I don't want to have to keep checking if one run is done -> for loop(?)
Because of licensing issues, I can only run a single testing process at a time -> sequential
I want to keep working while the test set is being processed -> background
Here's what I tried:
#!/usr/bin/env bash
# Assembling a list of commands to be executed sequentially
TESTRUNS="";
for i in `ls ../testSet/*`;
do
MSG="running test problem ${i##*/}";
RUN="mySequentialCommand $i > results/${i##*/} 2> /dev/null;";
TESTRUNS=$TESTRUNS"echo $MSG; $RUN";
done
#run commands with nohup to be able to log out of ssh session
nohup eval $TESTRUNS &
But it looks like nohup doesn't fare too well with eval.
Any thoughts?
nohup is needed if you want your scripts to run even after the shell is closed. so yes.
and the & is not necessary in RUN since you execute the command with &.
Now your script builds the command in the for loop, but doesn't execute it. It means you'll have only the last file running. If you want to run all of the files, you need to execute the nohup command as part of your loop. BUT - you can't run the commands with & because this will run commands in the background and return to the script, which will execute the next item in the loop. Eventually this would run all files in parallel.
Move the nohup eval $TESTRUNS inside the for loop, but again, you can't run it with &. What you need to do is run the script itself with nohup, and the script will loop through all files one at a time, in the background, even after the shell is closed.
You could take a look at screen, an alternative for nohup with additional features. I will replace your testscript with while [ 1 ]; do printf "."; sleep 5; done for testing the screen solution.
The commands screen -ls are optional, just showing what is going on.
prompt> screen -ls
No Sockets found in /var/run/uscreens/S-notroot.
prompt> screen
prompt> screen -ls
prompt> while [ 1 ]; do printf "."; sleep 5; done
# You don't get a prompt. Use "CTRL-a d" to detach from your current screen
prompt> screen -ls
# do some work
# connect to screen with batch running
prompt> screen -r
# Press ^C to terminate the batch (script printing dots)
prompt> screen -ls
prompt> exit
prompt> screen -ls
Google for screenrc to see how you can customize the interface.
You can change your script into something like
#!/usr/bin/env bash
# Assembling a list of commands to be executed sequentially
for i in ../testSet/*; do
do
echo "Running test problem ${i##*/}"
mySequentialCommand $i > results/${i##*/} 2> /dev/null
done
Above script can be started with nohup scriptname & when you do not use screen or simple scriptname inside the screen.

How to run "script -a xxx.txt" properly in a shell script?

I have a shell script and I want the session text to be saved automatically every time the script runs, so I included the command "script -a output.txt" at the beginning of my script. However, the script stops running after this line of code, which only displays a "bash-3.2$" on the screen and won't go on. Any ideas?
Thanks in advance!
The problem is script starts a separate sub-shell than the one that is running the actual script, to club them together. Use the -c flag in script
-c, --command command
Run the command rather than an interactive shell. This makes
it easy for a script to capture the output of a program that
behaves differently when its stdout is not a tty.
Just do,
script -c 'bash yourScript.sh' -a output.txt

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.

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