nix, shell, perl, php, mysql and mac os x tips and tricks

Tuesday, November 27, 2012

Cron Job to be texted/emailed when a server has a high load average

You could feasibly add this to your cron tab. Better have an unlimited text plan.
* * * * *       THRESHOLD="10.00"; LOAD=$(/usr/bin/uptime | /bin/awk '{print $8}' | /bin/cut -d "," -f 1;); if /usr/bin/test $(/bin/echo "$LOAD > $THRESHOLD" | /usr/bin/bc -l) = 1 ; then /bin/echo "Load average too high: $LOAD " | /bin/mail -s "calliope load avg: $LOAD" 3304445555@vtext.com; else /bin/echo ""; fi;  > /dev/null 2>&1

Thursday, October 25, 2012

Use mod_rewrite RewriteCond to do something when a requested file (like an image) does not exist

RewriteCond %{REQUEST_URI} ^/images/sa/regular/(.*)\.JPG$ [NC]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule ^(.*) /tools/missing_image.jsp\?image=%1 [L]

Monday, June 18, 2012

UNMOUNTABLE_BOOT_VOLUME error when resizing Parallels disk

You can't actually re-size a disk using Parallels. At least, not a plain disk, and not using Parallels Desktop 7. It will cause windows to be unbootable, showing the blue screen of death error "UNMOUNTABLE_BOOT_VOLUME". What you have to do is un-check the "Resize file system" checkbox (Parallels VM configuration), and then make your change (assuming you are making it bigger). That will add unallocated space to your virtual disk. When in windows, you partition and format the space in the Disk Manager->Storage. Give it a drive letter and everything. You then use a partition manager tool like "EaseUS Partition Master Home Edition" (free) to merge the two partitions. Pretty risky, so be sure you have your whole VM backed up.

Thursday, May 3, 2012

change apple display factor for new windows on a mac

Change display factor for new windows
defaults write -g AppleDisplayScaleFactor 0.8
note: doesn't work anympore

Saturday, April 7, 2012

Ban an IP address from your *nix server

iptables -A INPUT -s IP-ADDRESS -j DROP

Friday, February 10, 2012

Check that curl is connecting through your proxy

http_proxy=http://localhost:8082 curl -C - -O -# -v -A 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13' http://www.cotse.com/proxycheck2.htm > index.html

Then open that html file in your browser

Friday, February 3, 2012

Replace windows specials characters like curly quotes and ellipses in MySQL

UPDATE `table` SET `column` = REPLACE(`column`, 0xE28098, "'");
UPDATE `table` SET `column` = REPLACE(`column`, 0xE28099, "'");
UPDATE `table` SET `column` = REPLACE(`column`, 0xE2809C, '"');
UPDATE `table` SET `column` = REPLACE(`column`, 0xE2809D, '"');
UPDATE `table` SET `column` = REPLACE(`column`, 0xE28093, '-');
UPDATE `table` SET `column` = REPLACE(`column`, 0xE28094, '--');
UPDATE `table` SET `column` = REPLACE(`column`, 0xE280A6, '...');
UPDATE `table` SET `column` = REPLACE(`column`, CHAR(145), "'");
UPDATE `table` SET `column` = REPLACE(`column`, CHAR(146), "'");
UPDATE `table` SET `column` = REPLACE(`column`, CHAR(147), '"');
UPDATE `table` SET `column` = REPLACE(`column`, CHAR(148), '"');
UPDATE `table` SET `column` = REPLACE(`column`, CHAR(150), '-');
UPDATE `table` SET `column` = REPLACE(`column`, CHAR(151), '--');
UPDATE `table` SET `column` = REPLACE(`column`, CHAR(133), '...');

Thursday, January 26, 2012

a few ways to force-unmount a disk image in OS X

Not that you should ever have to do this or anything.
sudo umount -f /Volumes/Yourdisk
sudo diskutil umount force /Volumes/Yourdisk
For this one you need to first get the disk location with the mount or df command:
hdiutil eject -force /dev/disk2sX

Friday, January 20, 2012

mod_rewrite magic! Redirecting "behind the scenes"

I've posted about this before, but I have found another use for this trick. A client wanted to put dynamic JSP function on one of their sites, but the site in question was a simple apache vhost that was not running the tomcat application server. I didn't want to create a new instance of the application server and basically have to move the site just to add this little function they wanted...So, I built the JSP pages on another server (on a completely different domain) and used this mod_rewrite trick to make it appear to the user that they haven't left the site. Beautiful! You'd put something like this in your .htaccess file:
RewriteEngine On
RewriteBase    /

# first, make sure all http hosts have the www.
RewriteCond %{HTTP_HOST} !^www\.yoursite\.com [NC]
RewriteRule ^(.*)$ http://www.yoursite.com/$1 [L,R=301]

# check for any hits to the specific application page you want to forward to.
# the second rewritecond is very important, otherwise ALL hits will be forwarded
# add additional rewritecond's for each app page you want forwarded
RewriteCond %{HTTP_HOST} ^www\.yoursite\.com [NC]
RewriteCond %{REQUEST_URI} ^/dynamic_catalog [NC]
RewriteRule (.*) http://www.yourapplicationsite.com/%{REQUEST_URI} [L,P]

Friday, January 13, 2012

Fixing perl environment problem on os x 10.7

When upgrading from 10.6 to 10.7 some of my perl modules seemed to break (namely DBD::mysql). I tried to re-install via cpan and couldn't do it. Couldn't even do a force install.

Fortunately, there are alot of perl mods available through the fink package manager. I ran:
sudo fink install dbd-mysql-pm5123
and it installed the module no problem. Great. I could then execute my perl programs from the command line. Problem is, the path where fink puts the perl mods is not available to cron. So, I had to specify the additional perl library paths in the crontab itself. I added this to the top of my crontab:
PERL5LIB=/sw/lib/perl5/5.12.3/darwin-thread-multi-2level:/sw/lib/perl5/5.12.3:/sw/lib/perl5/darwin-thread-multi-2level:/sw/lib/perl5:/sw/lib/perl5/darwin:$PERL5LIB
LC_ALL=C
Source: Peter's Mac OS X Notes

print standard library paths to your perl modules

perl -le 'print foreach @INC'

Fix for Postfix mail issue after upgrading from OS X 10.6 to 10.7

Postfix stops working. It throws errors like:
Jan 13 12:18:52 huggybunny postfix/master[2254]: warning: process /usr/libexec/postfix/smtp pid 2299 exit status 1
Jan 13 12:18:52 huggybunny postfix/master[2254]: warning: /usr/libexec/postfix/smtp: bad command startup -- throttling

I had it set up to replay through GMail. Looks like the /etc/postfix/sasl_password file was deleted during the upgrade. All I had to do was create it again per these instructions, reload postfix and it was good to go.

UPDATE: that link appears to have disappeared. Here's what you do:

Gmail Email Relay using Postfix on Mac OS X 10.5 Leopard


I use Postfix to relay system messages (e.g. a botched cronjob) over SSL to my Gmail account. These messages are otherwise “lost” because the email is being sent to a dormant mail transfer agent on my Mac. There’s way more that you can do with Postfix, but I’ll just cover the setup I did for my needs.

1. Create the Simple Authentication and Security Layer (SASL) password file.

sudo vi /etc/postfix/sasl_passwd

Enter and save the following

smtp.gmail.com:587 your.name@gmail.com:your.password
2. Create a Postfix lookup table for SASL.

sudo postmap /etc/postfix/sasl_passwd

3. Configure Postfix with

sudo vi /etc/postfix/main.cf

By default, everything is commented out. You can just append this to the end of file and save:

# Minimum Postfix-specific configurations.
mydomain_fallback = localhost
mail_owner = _postfix
setgid_group = _postdrop
relayhost=smtp.gmail.com:587
# Enable SASL authentication in the Postfix SMTP client.
smtp_sasl_auth_enable=yes
smtp_sasl_password_maps=hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options=

# Enable Transport Layer Security (TLS), i.e. SSL.
smtp_use_tls=yes
smtp_tls_security_level=encrypt
tls_random_source=dev:/dev/urandom

5. Start postfix with

sudo postfix start

If you get an error, address the parameter in main.cf that is in the error and use

sudo postfix reload

to refesh Postfix. If everything looks good, then send an email

date | mail -s test your.name@gmail.com

If you don’t get an email fairly quickly, enter

mailq

and you should see the email there along with a description of any problems. If the mail is not in the queue, you most likely got something wrong in main.cf so you’ll need to do some debugging. If your mail queue starts to fill up during this process, you can clear it with

sudo postsuper -d ALL

6. Once you have everything working, you can set Postfix to start on boot by adding a key to /System/Library/ LaunchDaemons/org.postfix.master.plist. If you have Property List Editor (or something similar), you can add the key RunAtLoad of type Boolean with a value that is checked.

Alternatively,

sudo vi /System/Library/LaunchDaemons/org.postfix.master.plist

and add the following just before the tag

RunAtLoad

Empty the Postfix mail queue

Deletes everything.
sudo postsuper -d ALL

Tuesday, January 3, 2012

Running Parallels 5 or 6 after upgrading to Mac OS X Lion (10.7)

Naturally, Parallels doesn't work after upgrading. It will show you an error like "Unable to load the Parallels Driver. Please restart your mac and try again." when you try to start a virtual machine. They are relentless about keeping you on the teat of their upgrades. Just like Intuit.

Anyway, I found a workaround. All I can say is that it worked for me. Don't blame me if it blows up your computer. I am doing this as "root" user rather than sudo-ing. Run these commands and it should start.
chgrp -R wheel "/Library/Parallels/Parallels Service.app/Contents/Kexts/10.6/";
sudo kextutil "/Library/Parallels/Parallels Service.app/Contents/Kexts/10.6/prl_hypervisor.kext";
sudo kextutil "/Library/Parallels/Parallels Service.app/Contents/Kexts/10.6/prl_hid_hook.kext";
sudo kextutil "/Library/Parallels/Parallels Service.app/Contents/Kexts/10.6/prl_usb_connect.kext";
sudo kextutil "/Library/Parallels/Parallels Service.app/Contents/Kexts/10.6/prl_netbridge.kext";
sudo kextutil "/Library/Parallels/Parallels Service.app/Contents/Kexts/10.6/prl_vnic.kext";

So, after that, you might (i.e. will probably) need to run this "Parallels Desktop Launcher" script. Perhaps even every time you run parallels (you could just replace the icon in your dock I suppose)

Launcher

When the Mac App store hangs on connection (10.6 or later)

  1. Close App Store and iTunes completly (blue circle under each should disappear)
  2. Open Terminal (Applications -> Utilities -> Terminal)
  3. Run the following:
    mv -f ~/Library/Preferences/com.apple.storeagent.plist ~/Library/Preferences/com.apple.storeagent.old.plist
    mv -f ~/Library/Preferences/com.apple.appstore.plist ~/Library/Preferences/com.apple.appstore.old.plist
    
  4. This will reset a number of preferences about the App Store.
  5. Restart your Mac! - This is key!
  6. Open App Store. you'll need to re-login