[linux] How can I use nohup to run process as a background process in linux?

I use this command to run my work.

(time bash executeScript 1 input fileOutput $> scrOutput) &> timeUse.txt

While, 1 is a number of process that I use to run this work. I have to change the number of process for each run. At each time it use long time to complete. Then I want to run it as background process.

How can I do it?

I tried:

nohup ((time bash executeScript 1 input fileOutput $> scrOutput) &> timeUse.txt)

But it doesn't work.

This question is related to linux

The answer is


  • Use screen: Start screen, start your script, press Ctrl+A, D. Reattach with screen -r.

  • Make a script that takes your "1" as a parameter, run nohup yourscript:

    #!/bin/bash
    (time bash executeScript $1 input fileOutput $> scrOutput) &> timeUse.txt
    

You can write a script and then use nohup ./yourscript & to execute

For example:

vi yourscript

put

#!/bin/bash
script here

you may also need to change permission to run script on server

chmod u+rwx yourscript

finally

nohup ./yourscript &

In general, I use nohup CMD & to run a nohup background process. However, when the command is in a form that nohup won't accept then I run it through bash -c "...".

For example:

nohup bash -c "(time ./script arg1 arg2 > script.out) &> time_n_err.out" &

stdout from the script gets written to script.out, while stderr and the output of time goes into time_n_err.out.

So, in your case:

nohup bash -c "(time bash executeScript 1 input fileOutput > scrOutput) &> timeUse.txt" &