Parth Shah
Parth Shah

Reputation: 1475

How do I use nohup and time together?

I want to be able to use nohup and time together in a bash shell while running mpiexec such that the output (stdout), errors (stderr) and time all end up in 1 file. Here is what I am using right now:

nohup bash -c "time mpiexec.hydra -np 120 -f nodefile ./executable -i 1000 > results.log 2>&1"

However, what is happening is that time goes to a file called nohup.out and output and error goes to results.log

Has anyone figured this out?

Upvotes: 2

Views: 3749

Answers (2)

davidxxx
davidxxx

Reputation: 131346

You could enclose the whole command between curcly braces to redirect the whole output { ... ; } such as :

{ nohup bash -c "time mpiexec.hydra -np 120 -f nodefile ./executable -i 1000" ; } > results.log 2>&1

Gnu reference : Bash provides two ways to group a list of commands to be executed as a unit. When commands are grouped, redirections may be applied to the entire command list.

(...) : creates a subshell
{...} : doesn't

EDIT : Here not sure that it is required.
I think that the issue is that your enclosing " " is too broad.
You should redirect only the result of the nohup command such as :

 nohup "foo-cmd -bar argFooBar" > results.log 2>&1

So try without enclosing the redirection from the command passed to bash :

nohup bash -c "time mpiexec.hydra -np 120 -f nodefile ./executable -i 1000" > results.log 2>&1

Upvotes: 4

Jakob Lindskog
Jakob Lindskog

Reputation: 130

Man page for nohup

To save output to FILE, use 'nohup COMMAND > FILE'

Simple example:

$ nohup bash -c "time echo 'this is stdout' > results.log 2>&1" >> results.log
nohup: ignoring input and redirecting stderr to stdout
$ cat results.log
this is stdout

real    0m0.001s
user    0m0.000s
sys 0m0.000s

So, in your case, try this:

nohup bash -c "time mpiexec.hydra -np 120 -f nodefile ./executable -i 1000 > results.log 2>&1" >> results.log

Upvotes: 1

Related Questions