2

I want to execute four scripts at the same time with nohup. These scripts are all in the same folder.

Would something like this work?:

nohup /.scrip1.sh & /.scrip2.sh & /.scrip3.sh & /.scrip4.sh 

Would this print a single nohup.out?

Kusalananda
  • 320,670
  • 36
  • 633
  • 936

3 Answers3

4

Prefix each script with nohup:

for script in ./script{1..4}.sh; do
    nohup "$script" &
done

According to the nohup manual, the output is appendend to the file called nohup.out, which means all output would be written to the same file. Note, however, that the output may be jumbled together as all scripts run and write concurrently to the file.

In order to avoid intermingled output, it may be better to write to individual files:

for script in ./script{1..4}.sh; do
    nohup "$script" >"$script.out" 2>&1 &
done

You may then use cat ./script{1..4}.sh.out to concatenate all the output files into a single stream once all scripts have finished executing. All background tasks have finished running when the wait command returns (assuming the jobs are still associated with the current shell session).


A variation of this:

nohup sh -c 'for script do "$script" >"$script.out" 2>&1 & done' sh ./script{1..4}.sh

The only difference here is that the nohup is applied to an in-line sh -c script that runs our loop given some arguments to execute. The output is still written to .out files as before.

This works because the sh -c script ignores the HUP signal (due to nohup), and the shells executing each script would also ignore that signal (the "ignore the HUP signal" is inherited).

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
0

You can write an other script to execute your four scripts in the same operation:

$ cat all.sh
bash scrip1.sh &
bash scrip2.sh &
bash scrip3.sh &
bash scrip4.sh &
$ cat scrip1.sh
echo hello1

Run nohup bash all.sh

$ cat nohup.out
hello1
hello2
hello3
hello4
ramius
  • 604
  • 1
  • 13
-1

nohup can only run a single program. To run multiple program under nohup you can use sh -c:

nohup sh -c "/.scrip1.sh & /.scrip2.sh & /.scrip3.sh & /.scrip4.sh"

As explained in this answer

The following will not protect the scripts from HUP signal.

To run multiple program in a single nohup instance you would need to run the scripts in a echo running a subshell echo $() which will look like like this :

nohup echo $(/.scrip1.sh & /.scrip2.sh & /.scrip3.sh & /.scrip4.sh)

However, the output of your script will be concatenated, so take that in consideration when writing the output.

EDIT:Typo

EDIT2: Added why my answer was not good.

EDIT3: Added a better answer.

  • The command substitution would be executed _before_ `nohup` starts (to expand to the arguments given to `nohup echo`), so none of the scripts would be ignoring the HUP signal. – Kusalananda Oct 12 '22 at 12:45
  • Indeed, I focused too much on the output part and forgot about the initial objective of using nohup (Ignoring HUP signal). I edited my answer. – Pierre Morcrette Oct 12 '22 at 13:05