1

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.

Porcupine
  • 1,892

2 Answers2

2

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.

Toby Speight
  • 8,678
0
  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.
Crypteya
  • 494