I want to run an executable main and redirect all outputs to /dev/null, meanwhile I measure its runtime with time and write the results to runtime.out. Since the task is long I also have to run the whole thing with nohup.
I tried the following:
nohup time ./main &> /dev/null &> runtime.out &
This just outputs everything to runtime.out. I don't need the output of main just the runtime, saved into a file.


time has something made to be used for this:

nohup time -o runtime.out ./main &> /dev/null &

If it was scripted and didn't require a tty, I'd rather use setsid than nohup + &, because it "daemonizes" better, and can still be sent a HUP signal if needed.

setsid time -o runtime.out ./main </dev/null &>/dev/null

Also note that here (as in OP's question) time is /usr/bin/time, which has a different output format than bash's builtin time command. It appears that /usr/bin/time --portability gives a similar output if needed.


Through some trial & error and stealing inspiration from bash time with nohup , I came up with the following:

$ nohup bash -c 'time ./main &> /dev/null' > runtime.out &
[1] 23178
nohup: ignoring input and redirecting stderr to stdout
[1]+  Done                    nohup bash -c 'time ./main &> /dev/null' > runtime.out
$ cat runtime.out 

real    0m0.004s
user    0m0.002s
sys     0m0.002s

This will redirect stdout of time to runtime.out, print stdout of nohup to the terminal, and redirect stdout and stderr of main to /dev/null.


This appears to do the right thing:

$ time > runtime.out nohup ./main &> /dev/null &
  • When I try this, I see the stdout of time on the terminal and runtime.out is empty. (Where main is a simple shell script with an echo) – Kevin Kruse May 9 '18 at 16:17
  • Interesting, because when I ran that, I saw no output, and had a runtime.out file with the output of time therein. Redirecting main's output to main.out resulted in the expected two files rightly populated. – DopeGhoti May 9 '18 at 16:19
  • Strange. Could it be shell differences? I'm using GNU bash, version 4.2.46(2)-release – Kevin Kruse May 9 '18 at 16:49
  • 4.4.19(1)-release over here. :shrug: – DopeGhoti May 9 '18 at 16:53
  • sadly this does not do the job. – dnnagy May 10 '18 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.