kiki
kiki

Reputation: 215

using time command in bash script and redirect the output

I'm trying to write a script that does

x =$ncore
numactrl -C $x ( time -p $exe ) > out.txt 2>&1

on the terminal ( time $ exe ) > out.txt 2>&1 worked as i wanted to (out.txt containing output of time and executable )

i'm using red hat 6.2 and time is not GNU version( i'm assuming from the fact that -a -o options don't work)

i want out.txt to have output from the executable and at the end have output from the time command.

the bash script is giving me problems with having ( so i used ( time -p $exe ) and now numactl sees ( as the executable.

is there a way to use numactl and time command together and have the output i want ?

Upvotes: 1

Views: 964

Answers (1)

Alan Curry
Alan Curry

Reputation: 14731

If numactrl wants a command, but you want to use some shell features, just give it the shell as a command:

numactrl -C $x bash -c "( time -p $exe ) > out.txt 2>&1"

When you runtime -p $exe from a bash prompt or within a bash -c, you're using the bash builtin version of time. The one with the -o option is an external command, so to use it from bash you have to specify command time or /bin/time or /usr/bin/time.

If you run numactrl -C $x time ... then it probably runs the external command, so -o should work in that case, but if not then you always have the bash -c method.

Note that the output format is different between the various versions of time. The GNU coreutils version prints more information than the bash builtin version.

Upvotes: 2

Related Questions