I use bash on Ubuntu 18.04. I have added a lot of functions and a lot of scripts are sourced in my bashrc. I am able to time the total time it takes to load my terminal, sometimes it takes 0.9 seconds to more than a minute. And this happens randomly, not that only at the first time it takes more time.

How can I log and analyze how much time each command in bashrc takes to execute. I hope that the logs are generated every time automatically and when it is slow I could analyse the cause of it.

Any suggestions as to what can be done.

Secondly, would preload daemon be useful to speedup the loading of the terminal. I am not sure about it.

  • It's not a duplicate. I want time measurement for each command individually. – Porcupine Feb 27 '19 at 23:38
  • 1
    Your terminal very probably starts up very quickly. You seem to be talking about your shell. – JdeBP Feb 27 '19 at 23:54
  • use the command time to time commands eg. time source ./somefuncsA.bash – X Tian Feb 28 '19 at 14:00

You could print timestamps using the Bash tracing mechanism, given that \t is expanded in $PS4:

$ PS4='+ \t> ' bash -x -c "sleep 1; sleep 2; sleep 3"
+ 18:41:05> sleep 1
+ 18:41:06> sleep 2
+ 18:41:08> sleep 3

It should be easy to apply this to tracing your .bashrc and/or .profile etc.

  1. You could put the time command before each command in your .bashrc
  2. To direct the output of the time command (and not the command output itself) to a log file, reference this question
  3. You'll probably want to echo out something useful to the log file before each command so that you can identify which command is being timed in the logs.
  4. (optional) You may also want to run date at the beginning of your .bashrc and direct the output to the log file so that you can see which login event is being timed.

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.