Log commands execution time

Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
I'd like to log execution time of my commands. Something like this:
#!/usr/bin/env bash
echo "$@" >> /tmp/times
exec 3>&2
(/usr/bin/time -f "%e" "$@" 2>&3) 2>>/tmp/times
The problem is that time spawns child process that polluted standard error. Can this be done without this side effect?
bash io-redirection time-utility
add a comment |Â
up vote
6
down vote
favorite
I'd like to log execution time of my commands. Something like this:
#!/usr/bin/env bash
echo "$@" >> /tmp/times
exec 3>&2
(/usr/bin/time -f "%e" "$@" 2>&3) 2>>/tmp/times
The problem is that time spawns child process that polluted standard error. Can this be done without this side effect?
bash io-redirection time-utility
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I'd like to log execution time of my commands. Something like this:
#!/usr/bin/env bash
echo "$@" >> /tmp/times
exec 3>&2
(/usr/bin/time -f "%e" "$@" 2>&3) 2>>/tmp/times
The problem is that time spawns child process that polluted standard error. Can this be done without this side effect?
bash io-redirection time-utility
I'd like to log execution time of my commands. Something like this:
#!/usr/bin/env bash
echo "$@" >> /tmp/times
exec 3>&2
(/usr/bin/time -f "%e" "$@" 2>&3) 2>>/tmp/times
The problem is that time spawns child process that polluted standard error. Can this be done without this side effect?
bash io-redirection time-utility
edited Oct 22 '17 at 21:09
Gilles
508k12010031533
508k12010031533
asked Oct 22 '17 at 20:29
sevo
557313
557313
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
8
down vote
accepted
If you use /usr/bin/time, then the only way to send the timed process's stderr and the time information to different channels is to tell the time utility to do it. You can't do it with redirections in the shell that calls time because the timed process's stderr and the output from time itself are sent to the same file descriptor.
The time utility that you're using (based on your use of -f, it's GNU coreutils) has an option -o to tell it to write to a different file, and -a to append to this file. This option also exists on FreeBSD and macOS.
#!/bin/sh
echo "$@" >>/tmp/times
exec time -f %e -a -o /tmp/times -- "$@"
Alternatively, you can use the shell keyword (in shells that have it, which includes bash, ksh and zsh). With the keyword, you can control where the time information goes through redirection, because you can specify a redirection as part of the timed process.
#!/bin/bash
echo "$@" >>/tmp/times
TIMEFORMAT='%R'
time "$@" 2>&3; 3>&2 2>>/tmp/times
After a quick test, only the secon works for me.GNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu)under Funtoo-Linux.
â Nikos Alexandris
Nov 3 '17 at 8:37
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
8
down vote
accepted
If you use /usr/bin/time, then the only way to send the timed process's stderr and the time information to different channels is to tell the time utility to do it. You can't do it with redirections in the shell that calls time because the timed process's stderr and the output from time itself are sent to the same file descriptor.
The time utility that you're using (based on your use of -f, it's GNU coreutils) has an option -o to tell it to write to a different file, and -a to append to this file. This option also exists on FreeBSD and macOS.
#!/bin/sh
echo "$@" >>/tmp/times
exec time -f %e -a -o /tmp/times -- "$@"
Alternatively, you can use the shell keyword (in shells that have it, which includes bash, ksh and zsh). With the keyword, you can control where the time information goes through redirection, because you can specify a redirection as part of the timed process.
#!/bin/bash
echo "$@" >>/tmp/times
TIMEFORMAT='%R'
time "$@" 2>&3; 3>&2 2>>/tmp/times
After a quick test, only the secon works for me.GNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu)under Funtoo-Linux.
â Nikos Alexandris
Nov 3 '17 at 8:37
add a comment |Â
up vote
8
down vote
accepted
If you use /usr/bin/time, then the only way to send the timed process's stderr and the time information to different channels is to tell the time utility to do it. You can't do it with redirections in the shell that calls time because the timed process's stderr and the output from time itself are sent to the same file descriptor.
The time utility that you're using (based on your use of -f, it's GNU coreutils) has an option -o to tell it to write to a different file, and -a to append to this file. This option also exists on FreeBSD and macOS.
#!/bin/sh
echo "$@" >>/tmp/times
exec time -f %e -a -o /tmp/times -- "$@"
Alternatively, you can use the shell keyword (in shells that have it, which includes bash, ksh and zsh). With the keyword, you can control where the time information goes through redirection, because you can specify a redirection as part of the timed process.
#!/bin/bash
echo "$@" >>/tmp/times
TIMEFORMAT='%R'
time "$@" 2>&3; 3>&2 2>>/tmp/times
After a quick test, only the secon works for me.GNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu)under Funtoo-Linux.
â Nikos Alexandris
Nov 3 '17 at 8:37
add a comment |Â
up vote
8
down vote
accepted
up vote
8
down vote
accepted
If you use /usr/bin/time, then the only way to send the timed process's stderr and the time information to different channels is to tell the time utility to do it. You can't do it with redirections in the shell that calls time because the timed process's stderr and the output from time itself are sent to the same file descriptor.
The time utility that you're using (based on your use of -f, it's GNU coreutils) has an option -o to tell it to write to a different file, and -a to append to this file. This option also exists on FreeBSD and macOS.
#!/bin/sh
echo "$@" >>/tmp/times
exec time -f %e -a -o /tmp/times -- "$@"
Alternatively, you can use the shell keyword (in shells that have it, which includes bash, ksh and zsh). With the keyword, you can control where the time information goes through redirection, because you can specify a redirection as part of the timed process.
#!/bin/bash
echo "$@" >>/tmp/times
TIMEFORMAT='%R'
time "$@" 2>&3; 3>&2 2>>/tmp/times
If you use /usr/bin/time, then the only way to send the timed process's stderr and the time information to different channels is to tell the time utility to do it. You can't do it with redirections in the shell that calls time because the timed process's stderr and the output from time itself are sent to the same file descriptor.
The time utility that you're using (based on your use of -f, it's GNU coreutils) has an option -o to tell it to write to a different file, and -a to append to this file. This option also exists on FreeBSD and macOS.
#!/bin/sh
echo "$@" >>/tmp/times
exec time -f %e -a -o /tmp/times -- "$@"
Alternatively, you can use the shell keyword (in shells that have it, which includes bash, ksh and zsh). With the keyword, you can control where the time information goes through redirection, because you can specify a redirection as part of the timed process.
#!/bin/bash
echo "$@" >>/tmp/times
TIMEFORMAT='%R'
time "$@" 2>&3; 3>&2 2>>/tmp/times
answered Oct 22 '17 at 21:09
Gilles
508k12010031533
508k12010031533
After a quick test, only the secon works for me.GNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu)under Funtoo-Linux.
â Nikos Alexandris
Nov 3 '17 at 8:37
add a comment |Â
After a quick test, only the secon works for me.GNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu)under Funtoo-Linux.
â Nikos Alexandris
Nov 3 '17 at 8:37
After a quick test, only the secon works for me.
GNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu) under Funtoo-Linux.â Nikos Alexandris
Nov 3 '17 at 8:37
After a quick test, only the secon works for me.
GNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu) under Funtoo-Linux.â Nikos Alexandris
Nov 3 '17 at 8:37
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f399779%2flog-commands-execution-time%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password