Troubleshooting Mac OS X Hang on Shutdown

My MacBook Pro was not shutting down properly, and it took a fair amount of time to locate the cause of the problem. Google search results for “Mac OS X hangs on shutdown” were abundant, but none of the suggested resolutions fixed the problem my system was experiencing. I eventually found the cause of the problem, and I feel it’s worth sharing here. Hopefully the information in this article with be helpful to others.

The Problem

When I would shutdown or restart my Mac, the system would hang with the spinning grey beach ball on the display. Eventually I would have to use the power button to power off my Mac. This was happening under both Snow Leopard and Lion.

Troubleshooting the Problem

After months of dealing with this problem, it because apparent that the problem was related Parallels Desktop 7. I came to this conclusion because the problem only occurred after running a Parallels virtual machine for a few hours. As a quick resolution I uninstalled Parallels, installed VMware Fusion 4, and converted my Parallels virtual machines for use with Fusion. Everything was working great under Fusion 4—the shutdown problem was resolved, and I was happy with the performance of Fusion. However, after using Fusion for a while, I started noticing strange behavior with my CentOS virtual machines. For example, the Vim editor would hang occasionally, and at one point my ethernet bridge to the local system was going up and down repeatedly. The problem with the Ethernet connectivity and rumors of often corrupted Fusion virtual machines was enough to convince me to uninstall Fusion and switch back to Parallels. Of course after switching back to Parallels, my shutdown problems came back to haunt me.

As a developer who often works in a virtual machine environment, I don’t want to waste time troubleshooting an unstable virtual environment (Fusion). I also can’t risk losing hours of work. Parallels has always been a very stable solution, and I’ve never experienced any problems with any of my virtual machines.

The Solution

I eventually found that Kaspersky Anti-Virus 2011 was the cause of my problems. When I removed Kaspersky from my system by running the Kaspersky uninstall script, the shutdown problem was resolved. Once I was confident the shutdown problem was completely resolved, I tried reinstalling Kaspersky, and found the the shutdown problem eventually reappeared. So if you’re experiencing shutdown problems on a Mac that’s running Parallels and Kaspersky, uninstall Kaspersky!

If you uninstalled Kaspersky and you’re still experiencing shutdown problems, you should confirm that Kaspersky is completely uninstalled. Try running the following kextstat command to see if Kaspersky is still being loaded as a kernel extension.

kextstat -kl | awk ' !/apple/ { print $6 } '

If after running the above command you see any references to Kaspersky in the output, you probably didn’t use the Kaspersky uninstall script. You can access this uninstall script by running the Kaspersky installation dmg file—just select the option “Kaspersky Anti-Virus 2011 Uninstaller.app” instead of “Kaspersky Anti-Virus 2011.mpkg.”

Other Possible Solutions

If uninstalling Kaspersky Anti-Virus 2011 didn’t resolve your shutdown problem, try some of the following troubleshooting steps:

  • Try stopping the Kaspersky services “Kaspersky” and “kav,” and deleting the file “/.com_kaspersky_iswift_journal” prior to shutting down or restarting your system.
  • Try removing items from your login items. One way to eliminate anything in your login items is to document what applications you have listed in your startup items, and remove all of them (System Preferences > Users & Groups > Login Items). If your shutdown problem persists, add all of the login items back. Another (faster) option would be to move or rename “~/Library/Preferences/com.apple.loginitems.plist” (I’ve never done it this way, but it should work).
  • Try manually stopping various services before you shutdown or reboot your system. Use the Activity Monitor to view and stop the various services, or the appropriate commands in a terminal window. I recommend documenting which services you manually stopped so you can keep track of which ones you tried.
  • For a few months while I was experiencing these shutdown problems, I ran a custom shell script that would kill all of the services/processes that I suspected were causing the problem. This solution worked fairly well, but I grew tired of having to run the script every time I wanted to shutdown my system. An example kill script is shown below. It uses “ps aux” to pull a listing of processes, pipes that into “grep” to look for the suspected process by name, pipes that to “cut” to get the Process ID (PID), and then pipes that into “xargs” to pass each string to the utility “kill” to terminate the process.
ps aux | grep -i prl_ | cut -c 17-22 | xargs kill
ps aux | grep -i Kaspersky | cut -c 17-22 | xargs kill
ps aux | grep -i kav | cut -c 17-22 | xargs kill
cd /
rm .com_kaspersky_iswift_journal

If you’re like me and prefer to run Parallels Desktop7 and Kaspersky Anti-Virus on the same system, you can make the process of running this script a bit easier by creating an ApplsScript file that calls it. You can then copy the AppleScript file to the /Library/Scripts directory, and show the script icon in the top menu bar to have quick access to the script.

Example of running the shell script “cleanshutdown” from AppleScript

do shell script "/usr/local/bin/cleanshutdown" password "" with administrator privileges

Recommended Anti-Virus for Mac

If you prefer to continue using an anti-virus product on your Mac (I do), I highly recommend VirusBarrier X6 by Intego. VirusBarrier X6 has a very small memory footprint and includes tons of options and additional utilities, some of which will will appeal to security professionals who like to use traceroute (including a mapping function), advanced firewall options (similar to fwbuilder for iptables), and traffic analysis tools.

I switched to VirusBarrier X6 a couple of weeks ago, and I haven’t had one shutdown problem! The problem is solved!