2

So, I often find that I really want to know when I started a process (or script) after I discovered that it was taking ages.. meaning I couldn't pre-pend date before and after it on the command line (without interrupting something I now know takes ages).. and then the process may finish while I'm away from the terminal.

That got me thinking - is it possible to contort shell aliasing such that by default a 'date' is invoked before and after every command line entry? It would be super-useful to sometimes be able to scroll back in the terminal and get a feel of how long stuff took.

Do other people have clever ways of addressing this - I'm sure it's a common enough scenario.

In terms of solutions with this approach, I'm guessing it's related a bit to this Q. Run an interactive bash subshell with initial commands without returning to the ("super") shell immediately

where it's clear you can prepend commands.. but that's only a part of the solution. Perhaps pre-pending and then getting the shell prompt to show current time would be a fairly neat solution?

Thoughts?

user73225
  • 31
  • 2
  • http://unix.stackexchange.com/a/26797 – Ipor Sircer Jan 16 '17 at 13:51
  • excellent spot - very useful pointer to `ts` and `gnomon` as ways of time stamping different points output comes from a process. Gnomon looks particularly fun if you are wanting time deltas in a process. Mostly people are deprecating `date` as too slow and pointing out that it's possible to use much faster `printf` function within modern versions of bash. – user73225 Jan 16 '17 at 17:14

2 Answers2

4

You can simply modify your shell prompt PS1 variable to show current timestamp. One of the following can be used:

\t     the current time in 24-hour HH:MM:SS format
\T     the current time in 12-hour HH:MM:SS format
\@     the current time in 12-hour am/pm format
\A     the current time in 24-hour HH:MM format

You can find these with other options in man bash. Edit your .bashrc file to modify PS1 as desired.

You can then determine when any command is executed. Of course, if you stay idle for a long time before hitting enter, you will not get accurate time.

Edit: Another possibility is to use ts utility to show how your script is progressing. Execute your script as:

$ /path/to/script | ts

Each echo statement will be displayed with a timestamp.

Khaled
  • 36,533
  • 8
  • 72
  • 99
  • there's useful material on this at: http://unix.stackexchange.com/questions/35777/how-to-change-the-prompt-in-linux and there is also a handy `.bashrc` generator available at http://bashrcgenerator.com/ One thing worth noting though is there are some differences between .bashrc and .profile and when they go into effect. – user73225 Jan 16 '17 at 17:15
1

NOTE: this is not a direct answer to what OP is asking, but it can be useful to solve it's problem, perhaps.

For already started processes, you can retrieve the start date/time issuing a simple ps ax -F. It will show an output similar to the following:

apache    1733  1678  0 83467  7752   0 10:07 ?        S      0:00 /usr/sbin/httpd
apache    1734  1678  0 83467  7752   0 10:07 ?        S      0:00 /usr/sbin/httpd
apache    1735  1678  0 83467  7752   0 10:07 ?        S      0:00 /usr/sbin/httpd
apache    1736  1678  0 83467  7768   0 10:07 ?        S      0:00 /usr/sbin/httpd
apache    1737  1678  0 83467  7752   0 10:07 ?        S      0:00 /usr/sbin/httpd
apache    1738  1678  0 83467  7752   0 10:07 ?        S      0:00 /usr/sbin/httpd

As you can see, the 8th column show the start date/time.

shodanshok
  • 47,711
  • 7
  • 111
  • 180