Tuesday, December 14, 2004

Resurrecting copy/paste

If you (like me) occasionally suffer from losing your ability to copy/paste after you've been logged in for a LOOOONG time, "aramis" at Mac OS X hints has the answer:
I may be an unusual case, but I have a repetitive problem with Apple's Paste Board Server (pbs) dying on a semi-regular basis. The main symptom is the inability to copy and paste between applications. Logging out or rebooting will fix the problem every time, but a less disruptive solution is to spawn a new copy of /System -> Library -> CoreServices -> pbs. Some apps will connect to the new server automatically, though many (notably Firefox and TextEdit) need to be quit and restarted before they start working normally again.

How can you tell if pbs is dead? Open a Terminal window and run:

ps x | grep pbs

If pbs is dead, you'll see something like:

350 ?? Z 0:00.00 (pbs)
22200 std R+ 0:00.01 grep pbs

If pbs is alive, you'd see:

350 p4 S 0:00.29 /System/Library/CoreServices/pbs
22200 std R+ 0:00.01 grep pbs

If you see (pbs) in parentheses, it's dead! Zombied to be technical. If you see the full path to pbs listed out, then pbs is running fine. If you see both, then chances are you've already restarted pbs manually at some point since you last logged out or rebooted.

If your pbs is dead, you can starte a new one. Just open a Terminal window and type:

nohup /System/Library/CoreServices/pbs &

The nohup keeps pbs running even after you close the current Terminal window. I got sick of restarting pbs myself, so I came up with the following script -- I named it newpbs, and keep it in /usr/local/bin:


NEEDNEWPBS=`ps x | grep /System/Library/CoreServices/pbs | grep -v grep ; echo $?`

if [ "$NEEDNEWPBS" == "1" ] ; then
echo "PBS is dead. Starting a new one..."
nohup /System/Library/CoreServices/pbs > /dev/null &
osascript -e "tell application "Finder" to display dialog "Paste Board Server died and was restarted at `date`" buttons {"OK"}" > /dev/null &

newpbs checks to see if there's a non-zombied copy of pbs running. If there isn't, it starts a new one and displays a dialog to let you know what happened. You can always comment out or remove the osascript line if you're rather not get the pop-up. In order to check for pbs' death every five minutes, I added this script to my crontab by running crontab -e, and adding the following line:

*/5 * * * * /usr/local/bin/newpbs

While I'm still trying to track down the process that's killing my pbs, at least this gives me some added convenience.