Re: [FR] save command times and exit status in history automatically
Hi My thoughts regarding which properties / fields can be useful to have in history (originally pertaining to another shell but hope it's helpful): https://github.com/ngs-lang/ngs/wiki/History-Design#commands-history Regards, Ilya On 8 Nov 2019, at 1:47, Ángel mailto:an...@16bits.net>> wrote: On 2019-11-07 at 12:18 -0800, Daniel Colascione wrote: Maybe what I really want is something like $?, but instead of containing exit status, it would contain information from a struct rusage (derived from wait4) for the last command. Or something like that anyway. I would like having those variables, too. There was a related discussion some time ago, and a workaround was mentioned (using PROMPT_COMMAND and traps if I remember correctly), but I didn't like that approach too much. Would having such variables solve your needs? (using PROMPT_COMMAND to copy them into a file, perhaps). I would expect that storing the values from last wait4 would be much less intrusive than messing with history. Kind regards
Re: [FR] save command times and exit status in history automatically
On 2019/11/07 12:18, Daniel Colascione wrote: >>> statuses? This information is practically free to collect. >>> >> Because by the time you gather this information, the command has already >> been saved completely. >> >> There have been various proposals to extend the timestamp with additional >> information, but it's all data you can gather when the timestamp is saved >> before the command is executed. >> > Why not also, optionally, save command execution times and exit This sounds like you want accounting info (?). Example: I executed a perl command (using 'tperl') that exited with a status of 12. The output I got (some spaces trimmed) was: perl |v3| 1.00| 0.00| 1.00| 5013| 201| 11536.00| 0.00| 98283| 98014|12|pts/2 |Sun Nov 10 03:20:17 2019 That could be merged with history output (my HIST format is HISTTIMEFORMAT='%m%d@%H%M%S: ') 55325 1110@032011: tperl exit 12;' 55326 1110@032020: dump-acct -r /var/account/pacct |more 55327 1110@032419: man 5 acct 55328 1110@033045: history |tail Documented in manpage 'acct(5)': Fields: #define ACCT_COMM 16 typedef u_int16_t comp_t; struct acct { char ac_flag; /* Accounting flags */ u_int16_t ac_uid; /* Accounting user ID */ u_int16_t ac_gid; /* Accounting group ID */ u_int16_t ac_tty; /* Controlling terminal */ u_int32_t ac_btime; /* Process creation time (seconds since the Epoch) */ comp_tac_utime; /* User CPU time */ comp_tac_stime; /* System CPU time */ comp_tac_etime; /* Elapsed time */ comp_tac_mem; /* Average memory usage (kB) */ comp_tac_io;/* Characters transferred (unused) */ comp_tac_rw;/* Blocks read or written (unused) */ comp_tac_minflt;/* Minor page faults */ comp_tac_majflt;/* Major page faults */ comp_tac_swaps; /* Number of swaps (unused) */ u_int32_t ac_exitcode; /* Process termination status (see wait(2)) */ char ac_comm[ACCT_COMM+1]; /* Command name (basename of last executed command; null-terminated) */ char ac_pad[X];/* padding bytes */ }; enum { /* Bits that may be set in ac_flag field */ AFORK = 0x01, /* Has executed fork, but no exec */ ASU = 0x02, /* Used superuser privileges */ ACORE = 0x08, /* Dumped core */ AXSIG = 0x10/* Killed by a signal */ }; The comp_t data type is a floating-point value consisting of a 3-bit, base-8 exponent, and a 13-bit mantissa. A value, c, of this type can be converted to a (long) integer as follows: v = (c & 0x1fff) << (((c >> 13) & 0x7) * 3); The ac_utime, ac_stime, and ac_etime fields measure time in "clock ticks"; divide these values by sysconf(_SC_CLK_TCK) to convert them to seconds.
Re: [FR] save command times and exit status in history automatically
On 2019-11-07 at 12:18 -0800, Daniel Colascione wrote: > Maybe what I > really want is something like $?, but instead of containing exit > status, it would contain information from a struct rusage (derived > from wait4) for the last command. Or something like that anyway. I would like having those variables, too. There was a related discussion some time ago, and a workaround was mentioned (using PROMPT_COMMAND and traps if I remember correctly), but I didn't like that approach too much. Would having such variables solve your needs? (using PROMPT_COMMAND to copy them into a file, perhaps). I would expect that storing the values from last wait4 would be much less intrusive than messing with history. Kind regards
Re: [FR] save command times and exit status in history automatically
> On 2019 Nov 7 , at 3:18 p, Daniel Colascione wrote: > > On Thu, Nov 7, 2019 at 12:09 PM Chet Ramey wrote: >> >> On 11/5/19 12:49 PM, Daniel Colascione wrote: >>> Right now, bash history saves only the command line actually executed. >> >> This isn't quite the case. What it saves is the line returned from >> readline, before it's expanded or executed. > > Fair enough. > >>> Why not also, optionally, save command execution times and exit >>> statuses? This information is practically free to collect. >> >> Because by the time you gather this information, the command has already >> been saved completely. >> >> There have been various proposals to extend the timestamp with additional >> information, but it's all data you can gather when the timestamp is saved >> before the command is executed. > > That's how history works today, yes. I'm wondering whether it'd be > possible to maintain an auxiliary history that included not only the > command lines read, but also their execution time and outcome. Doing > something in PROMPT_COMMAND doesn't seem quite accurate. Maybe what I > really want is something like $?, but instead of containing exit > status, it would contain information from a struct rusage (derived > from wait4) for the last command. Or something like that anyway. The > larger point is that the shell could gather this information > proactively almost for free and I don't think user code running in > existing shell extension points can do the same job. > Conceptually, this is quite a leap. Right now, the history list doesn't require *any* effort beyond logging the input. What you are suggesting may be readily accessible, but it's still *additional* work that must be done. Furthermore, a few issues off the top of my head: * How would handle background jobs? Would they appear in the order they are started, or the order they are completed? * Suppose I start a loop like for x in 1 2 3; do someCommand "$x" done Do you want to store the result of the for loop? Each of the commands run in the loop? What would that look like? * Would you want some way of linking the result of a `wait -n` command with the command it waited for? What would that look like? * Would you want to store *any* output from a command in your log? I think the design of such a feature would require a lot of careful thought to be useful. -- Clint
Re: [FR] save command times and exit status in history automatically
On Thu, Nov 7, 2019 at 12:09 PM Chet Ramey wrote: > > On 11/5/19 12:49 PM, Daniel Colascione wrote: > > Right now, bash history saves only the command line actually executed. > > This isn't quite the case. What it saves is the line returned from > readline, before it's expanded or executed. Fair enough. > > Why not also, optionally, save command execution times and exit > > statuses? This information is practically free to collect. > > Because by the time you gather this information, the command has already > been saved completely. > > There have been various proposals to extend the timestamp with additional > information, but it's all data you can gather when the timestamp is saved > before the command is executed. That's how history works today, yes. I'm wondering whether it'd be possible to maintain an auxiliary history that included not only the command lines read, but also their execution time and outcome. Doing something in PROMPT_COMMAND doesn't seem quite accurate. Maybe what I really want is something like $?, but instead of containing exit status, it would contain information from a struct rusage (derived from wait4) for the last command. Or something like that anyway. The larger point is that the shell could gather this information proactively almost for free and I don't think user code running in existing shell extension points can do the same job.
Re: [FR] save command times and exit status in history automatically
On 11/5/19 12:49 PM, Daniel Colascione wrote: Right now, bash history saves only the command line actually executed. This isn't quite the case. What it saves is the line returned from readline, before it's expanded or executed. Why not also, optionally, save command execution times and exit statuses? This information is practically free to collect. Because by the time you gather this information, the command has already been saved completely. There have been various proposals to extend the timestamp with additional information, but it's all data you can gather when the timestamp is saved before the command is executed. -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/