Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Use Growl to monitor long-running shell commands UNIX
While waiting for long-running shell commands to finish, I often switch to Mail or Safari. Here's how I made Bash notify me via Growl whenever one of those commands finishes. It's a combination of a clever bash script by glyf and Growl's growlnotify shell script. Install Growl, including the growlnotify shell script (found in the Extras directory). Next, download preexec.bash.txt [hints mirror] and save it under ~/.preexec.bash, as an invisible file in your home directory (in case you choose a different filename or location, make sure to adjust it in the following script).

Then add the following code to your ~/.bashrc:

. ~/.preexec.bash

# called before each command and starts stopwatch
function preexec () {
	export PREEXEC_CMD="$BASH_COMMAND"
	export PREEXEC_TIME=$(date +'%s')
}

# called after each command, stops stopwatch
# and notifies if time elpsed exceeds threshold
function precmd () {
	stop=$(date +'%s')
	start=${PREEXEC_TIME:-$stop}
	let elapsed=$stop-$start
	max=${PREEXEC_MAX:-10}
	
	if [ $elapsed -gt $max ]; then
		growlnotify -n "iTerm" -m "took $elapsed secs" ${PREEXEC_CMD:-Some Command}
	fi
}

preexec_install

Now whenever a shell command takes longer than 10 seconds to finish, Growl will notify you about it. To change the threshold temporarily, use export PREEXEC_MAX=20. To change it permanently, just change it in the code you just appended to ~/.bashrc.

[robg adds: I haven't tested this one.]
  Post a comment  •  Comments (22)  
  • Currently 3.60 / 5
  You rated: 3 / 5 (5 votes cast)
 
[20,032 views]  View Printable Version
Start matlab sessions on Linux clusters from your Mac UNIX
Do you need to evaluate lots of all-day computations in parallel on different data sets (i.e., prediction features on EEGs of different epileptic patients)?

Do you have basic Linux scripting knowledge?

Do you want a script-based "three-click-Apple interface" that distributes your ever evolving matlab code, quickly changing control scripts, which is developed on your Mac (but not the static raw data), to your homes and various computer clusters, and run the computation job there with a single command?

Do you have an ssh connection from your Mac to the clusters that cannot be kept up all the time?

If so, I've written a detailed tutorial explaining how to do the above.

[kirkmc adds: Typically we would ask for permission to run the blog linked above as a hint here. However, this one is very long and very specific, and probably applies to only a few users. As such, we've just left the link to the original post in place. Untested by me.]
  Post a comment  •  Comments (1)  
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[5,668 views]  View Printable Version
Scripts to create encrypted backups to online services UNIX
I've been testing mozy.com for online backups, but they charge after you go over 2GB of data, and their Mac client is still in beta and not quite stable. Then I read about using a dreamhost account for storage, which interested me as I have a dreamhost account with 200GB+ of available storage. The guide I linked to above was published by Michael Lee, and the method he suggests uses rsync, which does incremental backups so it only backs up changed files. But unfortunately, it stores your data as is, which could be potentially read by anyone if they manage to get access to your dreamhost account.

A better solution is to use an open source package called duplicity, which uses librsync for incremental backups as well, but also uses gpg to encrypt your data. It does have some limitations related to hard links. That should hopefully not be an issue for backing up your documents and photos, but may mean it's not suitable for full system backup including the OS. See the duplicity docs for more info.

I'm using dreamhost, but this hint should work with any server to which you can write files.
read more (1,719 words)   Post a comment  •  Comments (20)  
  • Currently 3.00 / 5
  You rated: 3 / 5 (6 votes cast)
 
[25,906 views]  View Printable Version
Using locale in PHP on OS X UNIX
I was looking around, trying to find out how to get locale to work in PHP on OS X. After some searching, I found the solution and wanted to share this to save you some work. In Linux, one can do this:
setlocale(LC_ALL, 'nb_NO');
echo strftime(" %A %e %B %Y ");
To get it work in PHP on OS X, you must write it like this:
// set date format to Norwegian:
setlocale (LC_TIME, "no_NO.UTF-8");
// output the date
gmstrftime('%A %e %B %Y, %H:%M');
locale is probably not in your profile path, so you have to look in the locale folder to find your language:
$ ls /usr/share/locale
There you can find the languages you can use; some languages have different locale versions. Note that the encoding of the page must correspond to the locale you are using. That is, if you use no_NO.UTF-8, then you must have in your head:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
Take a look at some date formatting tips for gmstrftime for more help.

[robg adds: I haven't tested this one.]
  Post a comment  •  Comments (0)  
  • Currently 3.33 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[8,427 views]  View Printable Version
A script to add icon previews and retain time stamps UNIX
sips is a fantastic command line tool which can add icons to image files so that they don't have to be auto-generated when you open a folder that has Show Icon Preview enabled. However, I never used it on my important images because it updates the files' time stamps as well. This means that if I generate icons today for images I took last year, those files would show up in the Finder as having been created today.

I finally couldn't take it any more, so I wrote a bash script to generate icons while preserving file times. It works on a variety of test files that I've used, including filenames with spaces, but since I'm no bash guru, it could contain some pitfalls.

[robg adds: This worked for me in testing on a few different image files; the icons were created without changing the files' time stamps.]
  Post a comment  •  Comments (7)  
  • Currently 3.20 / 5
  You rated: 3 / 5 (5 votes cast)
 
[8,770 views]  View Printable Version
Fix broken SSH Public Key Authentication UNIX
Ever since I learned of SSH, I have wanted to set it up so that I could automatically run remote commands (like rsync) on the other Macs on our home network. The proper way to do this, of course, is with passphrase-free public key authentication. But try as I might, I simply could not convince SSH to use public key authentication! Eventually I discovered the reason in an obscure mailing list: If permissions are set incorrectly on the home directory, SSH will refuse perfectly good authentication keys.

The solution: Open Terminal and type chmod g-w ~/.

SSH is now entirely happy to authenticate using keys.

[robg adds: Running Disk Utility's permissions repair should also fix home folder permissions, though I'm not 100% positive about that.]
  Post a comment  •  Comments (10)  
  • Currently 2.75 / 5
  You rated: 4 / 5 (8 votes cast)
 
[19,613 views]  View Printable Version
A shell script to upload SSH keys to remote machines UNIX
This is a script that I initially found online and have modified to be a bit more useful. Basically it automates the process of uploading your SSH key to a remote host that you SSH into by doing the following:
  1. Creates a .ssh directory if there is not one already there, and sets the correct permissions on it.
  2. Puts your key in the authorized_keys file (and creates it if it was not there already), and changes the permissions on it.
Here's the code:

#!/bin/sh

KEY="$HOME/.ssh/id_dsa.pub"

if [ ! -f ~/.ssh/id_dsa.pub ];then
    echo "private key not found at $KEY"
    echo "* please create it with "ssh-keygen -t dsa" *"
    echo "* to login to the remote host without a password, don't give the key you create with ssh-keygen a password! *"
    exit
fi

if [ -z $1 ];then
    echo "Please specify user@host.tld as the first switch to this script"
    exit
fi

echo "Putting your key on $1... "

KEYCODE=`cat $KEY`
ssh -q $1 "mkdir ~/.ssh 2>/dev/null; chmod 700 ~/.ssh; echo "$KEYCODE" >> ~/.ssh/authorized_keys; chmod 644 ~/.ssh/authorized_keys"

echo "done!"

If you SSH into many machines, the script can save you a lot of manual work.

[robg adds: I tested this, and it works as described -- I edited it to reflect the fact that I have an RSA key, not a DSA key (so I just changed id_dsa.pub to id_rsa.pub). When you run the script, you'll be prompted for the password on the remote machine; after it runs, you can connect without a password (if you're not using a passphrase).]
  Post a comment  •  Comments (15)  
  • Currently 2.67 / 5
  You rated: 4 / 5 (9 votes cast)
 
[52,769 views]  View Printable Version
A simple way to open files in X11 applications UNIX
Here is the easiest way, to my knowledge, to associate file extensions with X11 applications: just use the open source X11 Extension program. On the disk image, you'll find a new ExtManager System Preferences panel. Install it, then open it in System Preferences. The next step is to define file extensions and commands associated with them (e.g. /sw/bin/gv for EPS files).

There might be a small problem if you're using applications from outside standard PATHs (e.g. from Fink); in that case, you have to pass the PATH environmental variable to the X11 window manager. For some reason, the Apple xterm fails to assign the proper values to PATH if they're included in the .xinitrc file, and it doesn't read the .bash_profile file either. As a workoround, I suggest creating a .bashrc file with a single line (assuming the paths are already defined in the .bash_profile file, which is a routine for Fink and MacPorts users):
source .bash_profile
You should also set up X11 to launch at login, and you can then assign X11 apps simply by right-clicking (luckily Apple people reinvented the wheel and found out that the mouse can have multiple buttons...) the file and choosing ExtManager as the application to use to open the file.

Another small hint: to get the mouse support in mc, add this line to .bash_profile:
export TERM=dtterm
[robg adds: I haven't tested this one.]
  Post a comment  •  Comments (9)  
  • Currently 3.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[10,262 views]  View Printable Version
A perl script to find large directories in a given folder UNIX
Here's a little perl script that can be used to find the top ten largest directories from the argument directories. To use it, save it as dirsize in your user's .bin folder (and make it executable with chmod a+x dirsize), modify your .bashrc to add ~/bin to your path, then run something like this in Terminal: dirsize ~/*. Here's the code:

#!/usr/bin/perl
use strict;
die "usage: $0 <directories>\n" unless @ARGV;
@ARGV = map { "'$_'" } @ARGV;
my @results = `du -hs @ARGV`;
@results = sort human_sort @results;
@results = @results[0..9];
print @results;

#---------------------------------------------------------------------------
sub human_sort {
	my ($size_a) = $a =~ /^(\S+)/;
	my ($size_b) = $b =~ /^(\S+)/;

	$size_a = $1 * 1024 if $size_a =~ /^(.*)k$/;
	$size_a = $1 * 1024 * 1024 if $size_a =~ /^(.*)M$/;
	$size_a = $1 * 1024 * 1024 * 1024 if $size_a =~ /^(.*)G$/;

	$size_b = $1 * 1024 if $size_b =~ /^(.*)k$/;
	$size_b = $1 * 1024 * 1024 if $size_b =~ /^(.*)M$/;
	$size_b = $1 * 1024 * 1024 * 1024 if $size_b =~ /^(.*)G$/;

	return $size_b <=> $size_a;
}

[robg adds: I tested this, and it seems to work as expected -- it will take a while to run if you set it to work on a large tree structure.]
  Post a comment  •  Comments (9)  
  • Currently 3.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[11,043 views]  View Printable Version
A script to ease scp for files with odd names UNIX
Here's a perl script that will escape filenames and send the files to a remote server using scp. I save it as scp-to-coppit.org in my user's bin folder, and make it executable (chmod a+x scp-to-coppit.org). Then from the command line, I can do this: scp-to-coppit.org File with weird char's.txt. Here's the code:
#!/usr/bin/perl

@ARGV = map { s/'/\\'/g; $_; } @ARGV;
my $files = "'" . join("' '", @ARGV) . "'";
my $results = `/usr/bin/scp -rBq $files dcoppit\@coppit.org:.`;
print "Output: $results" if $results ne '';
[robg adds: I haven't tested this one.]
  Post a comment  •  Comments (12)  
  • Currently 2.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[8,968 views]  View Printable Version