Category Archives: Scripting

OSX 10.8 Not Booting on Retina Display

I went on a vacation for a week with my brand new retina display I just paid a little over 3500 for. I ran into a snag not 2 days into the trip. The machine would no longer boot into OSX. After several google searches. All turned useless. I spent several hours searching one last time and came across a interesting article that suggested moving the fsck asside. So here are the results that I hope will save other techies days of searching.

Symptoms:

  • Booting hangs at the apple logo
  • Repairing the Disk fails (fsck -fy, restarts several times and does not succeed)
    Error: Disk Utility can’t repair this disk…disk, and restore your backed-up files.
    Disk Utility Error
  • Re-install is impossible (this is because the Macintosh HD is read only)

Configuration:

Problem:

I did not want to re-format. This was simply not an option. I just built the machine less than a week before the problem. It’s impossible thats its a hardware file. So I figure it may have been related to a recent update OS X 10.8.3 update. However, I can not be 100% that was the cause. All I know everything went for a turn for the worse

Solution:

So this is a multipart solution. Each one took me several hours to test. I should note here I am highly experienced with OSX/Linux machines. So non of this is out of my territory. I was sure I was not “hurting” my hardware/software.

WARNING:  I WILL NOT BE RESPONSIBLE FOR LOST DATA FOR ANY REASON. USE THIS GUIDE AT YOUR OWN RISK OR FOR REFERENCE!!!

  1. Boot into single user mode, if you are using refit press F2 at the startup menu when you are on any the the “apple” icons, otherwise press Apple+S on startup. 
  2. Now we need to stop fsck from running at startup because it keeps shutting down your machine when it fails to fix the errors.
    To do this. I simply moved aside “fsck”.
    WARNING: You must be very careful. Type exactly as you see it below!!!

    • mv /sbin/fsck /sbin/fsck.back
      cp /usr/bin/true /sbin/fsck

    • (OPTIONAL STEP before rebooting)
      • You may want to try just:

        nvram boot-args=”-v”

        -x means safe mode, -v will print out a tone of crap, remember safe mode does not load the graphics drivers so it will be much slower.

      • nvram boot-args=”-x -v”

        be sure to disable the -x before you do the install step below

      • Once you completed you “should” be able to reboot so long as a “real” hardware failure did not happen.
  3. If you are able to boot into OS X you can continue to the next step, otherwise you probably have a hard drive / ssd failure at this point. For a brand new machine this is extremely unlikely. So you should “go no further” and bring your machine into Apple Support. If you able to run in single user mode. I would suggest backing up your data onto another drive before doing so. Most likely apple will wipe the drive when fixing your machine.
  4. Download 10.8 once you boot into OSX, you can do this by logging into the App Store (I have already purchased 10.8 for several other machines; however, I believe that if you say “buy” for 10.8 and you have 10.8 already installed you will not be changed, please someone post to confirm this to be true if you try this)
  5. (OPTIONAL STEP) If you took the optional step above, run this command in the Terminal.app (located in your /Applications/Utilities/ folder) before installing OS X:

    sudo nvram boot-args=”-v”

  6. Now, you should be able to run the setup, once you do OS X will automatically reboot
  7. Once booted, DO NOT continue the install, simply goto disk utility from the menu
    disk utility menu

    disk utility menu

    DO NOT PRESS ERASE AT ANYTIME!!!

    Disk Utility Repair

    Repair disk after you boot into the “installer” for osx. Be sure to run step 1 and 2 in the image before closing.

  8. Once that is complete, exit disk utility
  9. Now run the installer… Note, I had to reboot a few times for the installer to detect the Machintosh HD again.
  10. If all is done correctly, you should just boot into OSX normally again
  11. Verify everything is working and your files still exist.
  12. Now, open a terminal and disable the boot args:

    sudo nvram boot-args=””

    This will turn off safe mode and the verbose flag. If you took the optional step to run in safe mode.

  13. NOTE: You should not need to reset fsck if you did that step above. This is because the re-install should overwrite the “fake” file you put in its place. However, do confirm fsck is still working.
    • Open a terminal

      fsck ?

      fsck usage: fsck [-fdnypq] [-l number]

References:


Getting Sphinx to work with Maya modules

Lets say you want to have your API look pretty. However, your API interfaces with another program such as Maya. The problem here is that maya comes with its own python interpreter. With a little bit of hacking you can get it working.

Required before you begin:

  • Maya 2011 or higher (I have not tested 2010 or below, but you will need at minimum mayapy working)
  • Sphinx 2.x.x or higher (again, ive only tested with the build 2.0.6)

Step-By-Step

% mkdir docs
% cd docs
% sphinx-quickstart
# follow instructions, mostly just letting it create the default stuff (smash the return key until its complete)
# edit the conf.py that was created, and insert your python source path after your import modules:
sys.path.insert(0, os.path.abspath(‘..’))
# you need to turn on extensions in the conf.py as well:
extensions=[‘sphinx.ext.autodoc]
# so now you should be able to auto-generate the rcs files
% sphinx-apidoc -F -o ‘install_dir’ ‘source_dir’
# python script used to build the docs

  • Take special note of the #!/usr/bin/env mayapy, the mayapy is the trick here. If you are windows it might be: #!/C:/maya2013/bin/mayapy (or where ever the mayapy is located. The file should be setting right next to maya.exe)

build_sphinx_mayadoc.py:

#!/usr/bin/env mayapy
 import maya.standalone
 maya.standalone.initialize(name='python')
 import sphinx
 import sys
 import os
 if __name__ == '__main__':
 argv = sys.argv[1:]
 cwd = os.getcwd()
 argv.insert(0, sphinx.__file__)
 sphinx.main(argv)

# I also updated my Makefile to point to this script ‘build_sphinx_mayadoc.py’ instead of ‘sphinx’. This allows me now to do “make install” and all my Maya related modules won’t complain that they do not exist.

References

Full instructions are a bit vague:
http://sphinx-doc.org/tutorial.html
http://sphinx-doc.org/ext/autodoc.html


Keep Alive your Plex Media Server (PMS)

Let’s say you want to have a script run every 1 min to check to see if plex media server is running. If its not, re-launch it and send me an email when it does so. If its still running; don’t bother to inform me.

Sounds simple. And for the non-unix savvy people this might be a little hard to digest. You need 3 things:

  1. You need the script to check the Plex Media Server is running or not
  2. You need to install the script as a cronjob
  3. You need to have postfix setup so when the job fails it can send you an email

The Plex Media Script (plexchecker.py):


#!/usr/bin/python
__doc__ = """
Plex Media Server Startup if closed cronjob
"""
#
import os
import sys

import subprocess

PIPE = subprocess.PIPE

def start_plex():
    return os.system('open -a "Plex Media Server" &')

if __name__ == '__main__':
    # [] around P ignores self matching
    cmd = "ps aux |grep '/[P]lex Media Server' | awk '{print $2}' | head -1"

    pid = subprocess.Popen(cmd,shell=True, stdout=PIPE, stderr=PIPE).communicate()[0]
    pid = pid.strip()
    if pid == "":
        print cmd
        print "pid='%s'" % pid
        print "no process found, starting plex media server"
        sys.exit(start_plex())
        print "done."

    sys.exit(0)

I saved the script in my user accounts “bin” directory. Open a terminal (located in /Applications/Utilities/Terminal.app).
% mkdir ~/bin
* anytime you see “%” that means shell command.
And save the script in your ~/bin/ directory. You may use any text editor you wish. I personally use gvim but its a little hard for new users to use. Something better like BBEdit or Eclipse is recommended for new programmers.

Setup Your POSTFIX email:
Why do you need this step? Well, this is only needed if you want to recieve an email anytime your cronjob fails or succeeds. If you don’t care, you can skip down to the cronjob section.
The next part is setting up postfix. Note that everything here uses “gmail” but you can use any provider you want. I only recommend gmail since  you can setup a generic account that you send your alerts too. We need to create a text file for postfix first:
% sudo touch /etc/postfix/sasl_passwd
% sudo vi /etc/postfix/sasl_passwd

(type i – for insert mode), and put the following in the file:
smtp.gmail.com:587 user_account@gmail.com:password_for_your_email
– replace user_account, with your gmail account
– replace password_for_your_email with your gmail password
(type :wq, the : means vi is looking for a command, w means write, q means quite, so :wq does it all at once)
% sudo postmap -q smpt.gmail.com:587 /etc/postfix/sasl_passwd
This installs the username and password into the postfix db file so its encrypted

Now that you have the password setup. We need to configure the server:
% sudo vi /etc/postfix/main.cf
Scroll to the bottom of the file and add:

relayhost=smtp.gmail.com:587
smtpd_sasl_auth_enable = yes
smtpd_sasl_path = smtpd
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_type = cyrus
smtp_sasl_auth_enable = yes
# Enable Transport Layer Security (TLS), i.e. SSL.
smtp_use_tls=yes
smtp_tls_security_level=encrypt
tls_random_source=dev:/dev/urandom

( type :wq when done, remember the shift semi-colin is a command in vi)

Ok, now to start the server and test it:
% sudo postfix start
After, type:
% date | mail -s testing.a user_account@gmail.com
You should get an email after a few minutes; check your spam folder since gmail is probably thinking this is spam. You may have to re-read / re-do this a few times if you are new to linux. Even me (a pro) it took a few times before I got it working. Typo’s all over the place.

Check the internal mail server:
% mailq
Check the postfix for your logged in account
% mail

As always, use “man” to get the manual for these commands (e.g. man mail) for help.

Setup your Cronjob:
Phew, lots of info. Ok, so we have postfix started, we have our file we need for our cronjob. Now to setup the cron.
% crontab -e

# min(0-59) hour(0-23)  day(1-31)   month(1-12) day (0-6)
# *         *           *           *           *       # script to run
MAILTO=user_account@gmail.com
*/1 *   *   *   *   /Users/mac_account/bin/plexchecker.py 

Notice user_account@gmail, this tells the cron to send the email if something fails to this account. The /User/mac_account/ folder is your home folder. On linux it would be /home/user_account/ for example. To keep things simple you probally should have your mac_account on your machine be the same as you gmail account.

When done inserting the code above for crontab, type :wq, notice that crontab used vi, this is why its a good idea to know a little vi.

So, to summarize what will happen here. The cronjob will run every 1 minute, if the plex server is not running, nothing is printed by the “plexchecker.py” script. If “ANY” text is printed via the script to standard output (e.g. print statement in python) or any errors are found (e.g. standard error) the cron will send you an email to the account specified in “MAILTO”.

Lastly, please let me know if this works for you. It took a few hours for me to get this up and running. I will modify/update the page if you find errors or I am missing a step.