Linux time save to file

how to redirect result of linux time command to some file

Probably you are using bash, and it has time built-in command. (see help time ) Your command should use /usr/bin/time to work properly (to discover where the binary of time is use which time ). Considering this, you must use: time -o out.txt -a .

10 Answers 10

-a is only understood by the time binary (/usr/bin/time), When just using time you’re using the bash built-in version which does not process the -a option, and hence tries to run it as a command.

/usr/bin/time -o foo.txt -a wget 'http://localhost:8080/upLoading.jsp' --timeout=0 

I believe this is actually the correct answer. When calling time, call /usr/bin/time instead of using the shell’s builtin which does not support the advanced options.

Checking man time , I guess what you need is

(Note you need both -a and -o).

[EDIT:] If you are in bash, you must also take care to write

(check manpage for explanation)

You can direct the stdout output of any commmand to a file using the > character. To append the output to a file use >>

Note that unless done explicitly, output to stderr will still go to the console. To direct both stderr and stdout to the same output stream use

 command 2>&1 outfile.txt (with bash) 
 command >& outfile.txt (with t/csh) 

If you are working with bash All about redirection will give you more details and control about redirection.

Consider /usr/bin/time as I commented above. And so, use something like /usr/bin/time -o outfile.txt -a .

\time 2> time.out.text command \time -o time.out.text command 

This answer based on earlier comments. It is tested it works. The advantage of the \ over /usr/bin/ is that you don’t have to know the install directory of time .

These answers also only capture the time, not other output.

Exactly the time from GNU writes it’s output to stderr and if you want to redirect it to file, you can use —output=PATH parameter of time

And if you want to redirect stdout to some file, you can use > filename to create file and fill it or >> filename to append to some file after the initial command.

If you want to redirect stderr by yourself, you can use $ command >&2 your_stderr_output

Читайте также:  Основы операционной системы gnu linux

Источник

bash script write executing time in a file

How can I do in bash script? I try with >> time.txt but that doesn’t work (the output does not go to file and does go to the screen).

4 Answers 4

Getting time in bash to write to a file is hard work. It is a bash built-in command. (On Mac OS X, there’s an external command, /usr/bin/time , that does a similar job but with a different output format and less recalcitrance.)

(time ./program.exe) 2> time.txt 

It writes to standard error (hence the 2> notation). However, if you don’t use the sub-shell (the parentheses), it doesn’t work; the output still comes to the screen.

Alternatively, and without a sub-shell, you can use:

Note the space after the open brace and the semi-colon; both are necessary on a single line. The braces must appear where a command could appear, and must be standalone symbols. (If you struggle hard enough, you’ll come up with . ;>|something or . ;>2>&1 . Both of these identify the brace as a standalone symbol, though. If you try . ;>xyz , the shell will (probably) fail to find a command called >xyz , though.)

I need to run more command in more terminal. If I do this:

 xterm -xrm '*hold: true' -e (time ./Program.exe) >> time.exe & sleep 2 

it doesn’t work and tells me Syntax error: «(» unexpected . How do I fix this?

You would need to do something like:

xterm -xrm '*hold: true' -e sh -c "(time ./Program.exe) 2> time.txt & sleep 2" 

The key change is to run the shell with the script coming from the argument to the -c option; you can replace sh with /bin/bash or an equivalent name. That should get around any ‘Syntax error’ issues. I’m not quite sure what triggers that error, though, so there may be a simpler and better way to deal with it. It’s also conceivable that xterm ‘s -e option only takes a single string argument, in which case, I suppose you’d use:

xterm -xrm '*hold: true' -e 'sh -c "(time ./Program.exe) 2> time.txt & sleep 2"' 

You can manual bash xterm as well as I can.

I’m not sure why you run the timed program in background mode, but that’s your problem, not mine. Similarly, the sleep 2 is not obviously necessary if the hold: true keeps the terminal open.

Источник

Store the output of date and watch command to a file

In order to do what you are looking for, a simple script (as @Ignacio pointed out) should do the trick:

while true do echo "$(date '+TIME:%H:%M:%S') $(ps aux | grep "pattern" | wc -l)" | tee -a logfile sleep 2 done 

I use tee instead of >> so that you can see the output on your terminal as well as capture it in your log.

Читайте также:  Обновление simply linux через терминал

I seem to be getting an error with the 1 in the first line. But when I changed it to true, it worked. However the output on the screen shows Time and count on two different lines, but the log file just shows the count only. Is there any way I can get Time and count on the same line in the logfile?

Works perfectly! Thanks. Is there any way I can add the timestamp to the logfile so that it gets stored in unique files?

You mean to the logfile name? You can do something like logfile.$(date +%Y%m%d) to create a new logfile every day.

This can easily be done using watch too without using any scripts.

watch -t -n 10 «(date ‘+TIME:%H:%M:%S’ ; ps aux | grep «pattern» | wc -l) | tee -a logfile»

Correct. I wrote what I had on a Mac, where watch isn’t available out of the box, and opted for the portable solution. Yours is much simpler.

In other words, include a pipe to tee -a logfile within the arg passed to watch . Very clean, thank you.

Thanks @Wildcard for your comment, I hadn’t notice that the double quotes included the tee command and, that actually help solving my remaining problem

watch is meant for output to a display. If you simply want to run a command every X seconds then you should just use a delay loop for that.

while true ; do somecommand ; sleep 2 ; done 

watch is an ncurses program, and is designed to be run in a console window (not redirected), which is why it’s creating a bunch of unprintable characters (those are the control characters that manage and move the cursor around for redrawing the screen).

You might try moving the date / grep commands into a script, and then call that script from a cronjob.

Ok, so I put it in a script and have the following code:

#!/bin/sh NOW=$(date '+%Y%m%d%H%M%S') LOGFILE="log.$NOW" while true do echo $(date '+[TIME: %H:%M:%S] Output: ' ; ps aux | grep "pattern" | wc -l ) | tee -a $LOGFILE sleep 2 done 

One easy alternative would be putting the command in a cmd.sh file with the following content:

#!/bin/bash (date '+TIME:%H:%M:%S' ; ps aux | grep "apache" | wc -l) >> logfile 

I came across this question when I was trying to get better/logged output from du -sh $data_path . I used the «while command, do sleep» pattern found here, but used some complex AWK to give the output I wanted.

while du -sh $data_path; do sleep 1; done | awk ' $1 != size < size=$1; path=$2; time=systime(); seconds=time-prevtime; if(seconds < 1000000000)< seconds=seconds" seconds" >else < seconds="" >print size, path, strftime("%m/%d/%Y@%H:%M:%S", time), seconds; prevtime=time >' 

I actually did this as a oneliner, which is why there are semicolons. But to make it readable, I broke it out. The output looks like:

502G /var/lib/cassandra/dump/ 05/22/2018@04:46:17 503G /var/lib/cassandra/dump/ 05/22/2018@04:46:59 42 seconds 504G /var/lib/cassandra/dump/ 05/22/2018@04:47:57 58 seconds 505G /var/lib/cassandra/dump/ 05/22/2018@04:48:55 58 seconds 506G /var/lib/cassandra/dump/ 05/22/2018@04:49:53 58 seconds 507G /var/lib/cassandra/dump/ 05/22/2018@04:50:50 57 seconds 508G /var/lib/cassandra/dump/ 05/22/2018@04:51:46 56 seconds 509G /var/lib/cassandra/dump/ 05/22/2018@04:52:44 58 seconds 510G /var/lib/cassandra/dump/ 05/22/2018@04:53:41 57 seconds 

Here is an example I just needed for a watch on a ps axf with a timestamp at the bottom of the entire output. I am watching for when Apache fails. I had to pipe to tee for each command, the ps and the date .

Читайте также:  Neural compute stick 2 linux

watch ‘ps axf | grep —line-buffered «[a]pache2″| tee —append logfile-apache-issue.log; date ‘+TIME:%H:%M:%S’ | tee —append logfile-apache-issue.log’

Sample Output of tail —follow logfile-apache-issue.log on the resulting file.

29862 ? S 0:00 \_ /usr/sbin/apache2 -k start 29863 ? S 0:00 \_ /usr/sbin/apache2 -k start 29864 ? S 0:00 \_ /usr/sbin/apache2 -k start 29865 ? S 0:00 \_ /usr/sbin/apache2 -k start 26635 pts/2 S+ 0:00 | \_ tail -n 1000 -f /var/log/apache2/error.log TIME:02:21:13 13622 ? SN 0:33 \_ /usr/sbin/apache2 -k start 25038 ? Ss 0:01 \_ /usr/sbin/apache2 -k start 29859 ? S 0:00 \_ /usr/sbin/apache2 -k start 29860 ? S 0:00 \_ /usr/sbin/apache2 -k start 29861 ? S 0:00 \_ /usr/sbin/apache2 -k start 29862 ? S 0:00 \_ /usr/sbin/apache2 -k start 29863 ? S 0:00 \_ /usr/sbin/apache2 -k start 29864 ? S 0:00 \_ /usr/sbin/apache2 -k start 29865 ? S 0:00 \_ /usr/sbin/apache2 -k start 26635 pts/2 S+ 0:00 | \_ tail -n 1000 -f /var/log/apache2/error.log TIME:02:21:15 13622 ? SN 0:33 \_ /usr/sbin/apache2 -k start 25038 ? Ss 0:01 \_ /usr/sbin/apache2 -k start 29859 ? S 0:00 \_ /usr/sbin/apache2 -k start 29860 ? S 0:00 \_ /usr/sbin/apache2 -k start 29861 ? S 0:00 \_ /usr/sbin/apache2 -k start 29862 ? S 0:00 \_ /usr/sbin/apache2 -k start 29863 ? S 0:00 \_ /usr/sbin/apache2 -k start 29864 ? S 0:00 \_ /usr/sbin/apache2 -k start 29865 ? S 0:00 \_ /usr/sbin/apache2 -k start 26635 pts/2 S+ 0:00 | \_ tail -n 1000 -f /var/log/apache2/error.log TIME:02:21:16 

Источник

Оцените статью
Adblock
detector