MacOS – Slow mac when Terminal have been open for some time

macbook promacosperformanceterminal

When I have Terminal.app open for some time (mostly after 2-3 hours), everything starts getting slow. When I type there is a delay for each character, and all applications lag when I scroll. If I restart Terminal, it goes away and my mac is quick and snappy again. But the problems comes back after some hours. It's especially bad when I have a ssh connection open to one of my servers.

I use Terminal a lot (I'm a developer), so it bothers me to have to restart it.

Do you have any idea how to find the problem? I can't find anything about this when googling.

I'll attach some screenshots from Activity Monitor, taken while it happens, just incase it helps.

My machine: Retina MacBook Pro, 2.6 GHz Intel Core i7, 16 GB RAM.

CPUMEMORYDISKNETWORK

UPDATE 1

Content of my bash startup files:

/etc/profile:

# System-wide .profile for sh(1)

if [ -x /usr/libexec/path_helper ]; then
    eval `/usr/libexec/path_helper -s`
fi

if [ "${BASH-no}" != "no" ]; then
    [ -r /etc/bashrc ] && . /etc/bashrc
fi

export LC_ALL=en_US.UTF-8  
export LANG=en_US.UTF-8

/etc/bashrc:

# System-wide .bashrc file for interactive bash(1) shells.
if [ -z "$PS1" ]; then
   return
fi

PS1='\h:\W \u\$ '
# Make bash check its window size after a process completes
shopt -s checkwinsize
# Tell the terminal about the working directory at each prompt.
if [ "$TERM_PROGRAM" == "Apple_Terminal" ] && [ -z "$INSIDE_EMACS" ]; then
    update_terminal_cwd() {
        # Identify the directory using a "file:" scheme URL,
        # including the host name to disambiguate local vs.
        # remote connections. Percent-escape spaces.
    local SEARCH=' '
    local REPLACE='%20'
    local PWD_URL="file://$HOSTNAME${PWD//$SEARCH/$REPLACE}"
    printf '\e]7;%s\a' "$PWD_URL"
    }
    PROMPT_COMMAND="update_terminal_cwd; $PROMPT_COMMAND"
fi

~/.bash_profile:

export PATH=/usr/local/bin:$PATH
export JAVA_HOME=`/usr/libexec/java_home -v 1.7`

if [ -f ~/.bashrc ]; then . ~/.bashrc; fi

~/.bash_login: I don't have this file

~/.profile:

export JAVA_HOME=/Library/Java/Home

export CLICOLOR=1
export LSCOLORS=exfxcxdxbxegedabagacad

# MacPorts Installer addition on 2012-10-02_at_22:35:07: adding an appropriate PATH variable for use with MacPorts.
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
# Finished adapting your PATH environment variable for use with MacPorts.

homebrew=/usr/local/bin:/usr/local/sbin
export PATH=$homebrew:$PATH

export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8

~/.bashrc:

mygrants() {
  mysql -B -N $@ -e "SELECT DISTINCT CONCAT(
    'SHOW GRANTS FOR \'', user, '\'@\'', host, '\';'
    ) AS query FROM mysql.user" | \
  mysql $@ | \
  sed 's/\(GRANT .*\)/\1;/;s/^\(Grants for .*\)/## \1 ##/;/##/{x;p;x;}'
}

Best Answer

You're going to have to broaden your search. There's nothing in your screenshots that points to Terminal. Nor anything in my experience—I let Terminal run for days with no ill effect.

It's possible that you're starting some task that's running away with the processor. You could set Activity Monitor to show all processes hierarchically, and give the tasks running directly under Terminal closer scrutiny, but I'd be more inclined to think that whatever it is is something you've spawned off as a separate process (using & at the end of a command).

Does quitting Terminal give you your performance back right away? That would indicate that the process is still running as a subtask of Terminal.

Either way, set Activity Monitor to show All Processes, and sort the list by % CPU. See what floats to the top.

Is there anything unusual about your $PROMPT_COMMAND? This is a command that gets run by the shell just before it displays the prompt. Seems to me to be a place where a time-waster could hide.

ADDENDUM: I just noticed your comment "...when I have an ssh session open to one of my servers". Do you also accept incoming ssh sessions? It's possible that you're under attack. Someone is trying to open an ssh session back to you, and is guessing passwords. If they're guessing very fast, they can bring your machine to its knees.

I thought of that earlier, because I've seen that happen when I enable Remote Login and have a publicly visible IP address. Once the bad guys notice that your port 22 is open from the WAN (and they will, within hours), they'll slam it with login attempts. The (temporary) fix is to disable Remote Login for a few minutes. They'll get bored and turn their attention elsewhere, and you have a respite of a few hours until another bad guy notices the open port.

But you say the problem goes away as soon as you quit Terminal, which sounds like a different problem. UNLESS you have some watchdog on your system that automatically opens port 22 (i.e., enables Remote Login) only while Terminal is running, and automatically closes it when Terminal quits. Or I imagine it could be possible to configure a firewall so that it allows incoming connections to port 22 only while there is currently an outgoing connection to port 22. Or only from remote addresses that you have an outgoing connection to, and the server you're connecting to is infected. Quitting Terminal closes all your ssh sessions, which would make the firewall stop accepting ssh incoming sessions, breaking the attack. Just a guess.

To see if you're under this kind of attack, open Activity Monitor and filter the process list for "sshd". If you see lots of very short-lived sshd processes, someone is trying to guess your password. Even if you don't allow password access, they don't know that and will try anyway.