I'm running a command in CentOS and at the same time I want to do 2 things:

  1. write the run time of the command to a file and
  2. set a time limit which stops the command

I implemented the first one successfully. Here's how it is:

(time ./minisat+_64-bit_static opb_clauses.txt ) 2>>../measures.txt

As you can see, I used the time command to count the time needed by the command and then I forwarded the time results to a file measures.txt. Here's a sample of measures.txt:

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

I'm trying to add the second thing like this:

(timeout 1s time ./minisat+_64-bit_static opb_clauses.txt ) 2>>../measures.txt

The results of this command messed up my file and returned something like this:

0.00user 0.00system 0:00.00elapsed 0%CPU (0avgtext+0avgdata 6368maxresident)k
0inputs+0outputs (0major+440minor)pagefaults 0swaps

As you can see, the results of the "real time" is somehow gone and the other results are in a different format. How can I separate the results of commands time and timeout?

EDIT:A second thought is to use the ulimit command instead of timeout.I tried the same thing but with ulimit:

 (ulimit -t 10 time ./minisat+_64-bit_static opb_clauses.txt ) 2>>../measures.txt

but this time i don't have any result written to measures.txt


There must be something up with your environment because when I try the same example I get the expected results. For example:

# command #1
$ (time echo "hi") 2>> measures.txt

$ cat measures.txt 

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

# command #2
$ (timeout 1s time echo "hi") 2>> measures.txt

$ cat measures.txt 

real    0m0.000s
user    0m0.000s
sys 0m0.000s
0.00user 0.00system 0:00.05elapsed 0%CPU (0avgtext+0avgdata 552maxresident)k
56inputs+0outputs (1major+173minor)pagefaults 0swaps

Instead of structuring your command as 2 separate calls why not do it like so:

 time ./minisat+_64-bit_static opb_clauses.txt
 timeout 1s time ./minisat+_64-bit_static opb_clauses.txt
) 2>>../measures.txt
  • i tried to run it in 2 commands but i got a segmentation fault.Is it ok to have both commands in ()? – Dchris Jul 12 '13 at 16:01
  • also,i try your first example(echo "hi"etc) and i got the same result as my other results,so you must be right that is something with my environment.I am in Cent0S.Any ideas of solving this issue? – Dchris Jul 12 '13 at 16:03
  • 1
    @Dchris - yes you can have both commands inside the (). The first command will run, once it's done, the second command will then run. The () are called command groups, mywiki.wooledge.org/BashGuide/…. – slm Jul 12 '13 at 16:28

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.