dannyman.toldme.com

About Me : Free Style : Good Reads : News and Reaction : Photographs : Technical : Travels : Unsorted

Mac OS X »

A really slow version of Unix with a nice dock.

Search:
July 4, 2008
Featured, FreeBSD, Linux, Mac OS X, Technical

HOWTO: Random Number in Shell Script

Link: http://dannyman.toldme.com/2008/07/04/shell-sh-bash-random-splay/

The other day I was working on a shell script to be run on several hundred machines at the same time. Since the script was going to download a file from a central server, and I did not want to overwhelm the central server with hundreds of simultaneous requests, I decided that I wanted to add a random wait time. But how do you conjure a random number within a specific range in a shell script?

Updated: Due to much feedback, I now know of three ways to do this . . .

1) On BSD systems, you can use jot(1):
sleep `jot -r 1 1 900`

2) If you are scripting with bash, you can use $RANDOM:
sleep `echo $RANDOM%900 | bc`

3) For portability, you can resort to my first solution:
# Sleep up to fifteen minutes
sleep `echo $$%900 | bc`

$$ is the process ID (PID), or “random seed” which on most systems is a value between 1 and 65,535. Fifteen minutes is 900 seconds. % is modulo, which is like division but it gives you the remainder. Thus, $$ % 900 will give you a result between 0 and 899. With bash, $RANDOM provides the same utility, except it is a different value whenever you reference it.

Updated yet again . . . says a friend:
nah it’s using `echo .. | bc` that bugs me, 2 fork+execs, let your shell do the math, it knows how
so $(( $$ % 900 )) should work in bsd sh

For efficiency, you could rewrite the latter two solutions:
2.1) sleep $(( $RANDOM % 900 ))
3.1) sleep $(( $$ % 900 ))

The revised solution will work in sh-derived shells: sh, bash, ksh. My original “portable” solution will also work if you’re scripting in csh or tcsh.

1 Comment

May 6, 2008
FreeBSD, Linux, Mac OS X, Technical

Mini-HOWTO: What Time is UTC?

Link: http://dannyman.toldme.com/2008/05/06/what-time-utc/

I wanted to know what time it was in UTC, but I forgot my local offset. (It changes twice a year!) I figured I could look in the date man page, but I came up with an “easier” solution. Simply fudge the time zone and then ask.

0-20:57 djh@noneedto ~$ env TZ=UTC date
Tue May  6 03:57:07 UTC 2008

The env bit is not needed in bash, but it makes tcsh happy.

Update: Mark points out an easier solution:
date -u

Knowing you can set TZ= is still useful in case you ever need to contemplate an alternate timezone.

(Thanks, Saul and Dave for improving my knowledge.)

3 Comments

January 26, 2008
Featured, Free Style, FreeBSD, Linux, Mac OS X, Sundry, Technical, Technology

Trendspotting: “The Amiga Line”

Link: http://dannyman.toldme.com/2008/01/26/deader-than-amiga/

I have been playing with Google Trends, which will be happy to generate a pretty graph of keyword frequency over time. A rough gauge to the relative popularity of various things. This evening, I was riffing off a post from the Royal Pingdom, regarding the relative popularity of Ubuntu and Vista, among other things.

I got started graphing various Linux distributions against each other, XP versus Vista, and trying to figure out the best keyword for OS X. Then, I wondered about FreeBSD. Against Ubuntu, it was a flatline. So, I asked myself: what is the threshold for a dead or dying Operating System?

Amiga vs FreeBSD:
Google Trends: Amiga versus FreeBSD

Ouch! Can we get deader?

Amiga vs FreeBSD vs BeOS:
Google Trends: Amiga versus FreeBSD versus BeOS

To be fair, the cult of Amiga is still strong . . . BeOS is well and truly dead. But how do the BSDs fare?

Amiga vs FreeBSD vs BeOS vs NetBSD vs OpenBSD:
Google Trends: *BSD versus Amiga, BeOS

NetBSD has been sleeping with the BeOS fishes for a while, and OpenBSD is on its way. And that’s a league below Amiga!

In Red Hat land, only Fedora beats “the Amiga Line”. For Unix in general, nothing stops the Ubuntu juggernaut. But there’s a long way to go to catch up with Uncle Bill.

(Yes, it is a rainy night and the girlfriend is out of town.)

Postscript: Ubuntu versus Obama

3 Comments

January 11, 2008
About Me, FreeBSD, Linux, Mac OS X, Technical

SysAdmin OpEd: Where to Keep the Crons

Link: http://dannyman.toldme.com/2008/01/11/etc-crontab-or-die/

This is just a note which I contributed to a thread on sage-members, to get something off my chest, as to where people should maintain their crontab entries. I sincerely doubt that reading what I have to say will bring you any great illumination.

I’d say, any reasonable SysAdmin should default to /etc/crontab because every other reasonable SysAdmin already knows where it is. If anything is used in addition to /etc/crontab, leave a note in /etc/crontab advising the new guy who just got paged at 3:45am where else to look for crons.

For production systems, I strongly object to the use of per-user crontabs. I’m glad to hear I’m not alone. One thing I have to do in a new environment tends to be to write a script that will sniff out all the cron entries.

And then there was the shop that used /etc/crontab, user crons, and fcron to keep crons from running over each other. This frustrated me enough that I did a poor job of explaining that job concurrency could easily be ensured by executing a command through (something like) the lockf utility, instead of adding a new layer of system complexity.

Yes, I am a cranky old SysAdmin.

2 Comments

January 8, 2008
About Me, Mac OS X, News and Reaction, Sundry, Technical, Technology

Goodbye, Bill Gates!

Link: http://dannyman.toldme.com/2008/01/08/bill-gates-last-day/

I was startled by this YouTube video, where we discover that Bill Gates can make fun of himself. Or, at least, his people can assemble a video where Bill Gates makes fun of himself. Good for Bill! I was then reassured at the consistency of the universe, when it was revealed that Bill really can’t make fun of himself without at least a dozen star cameos to reassure us that it is not so much that he is poking fun at himself, but that he is “acting”.

It is telling that Al Gore has the funniest line.

I hope Bill’s foundation does much good in the world. I almost feel sorry for Microsoft that after all the effort, Vista has proven to be a cold turkey. For what its worth, from a UI and performance perspective, I prefer Windows XP to Mac OS X. Though I’m not sure that this is praise for Microsoft as much as it is an aversion to the Smug Cult of Apple.

(Yes, I am a contrarian. People hate contrarians. Especially Mac people, who think they have the contrarian cred: the last thing a contrarian wants to encounter is a contradicting contrarian!)

Feedback Welcome

August 3, 2007
About Me, Mac OS X, Technology

Evidence I’m Not a Mac Person . . .

Link: http://dannyman.toldme.com/2007/08/03/the-genius-of-apple/

I just completed a feedback form regarding my AppleCare warranty experience. Question 12a gave me a chance to bitch. Question 12b made me smile at my ridiculous expectations:


12a Is there anything else you would like to tell Apple about your recent in-store repair experience at the Apple Retail Store? (NOTE: 2000 character limit)

Replacing the optical drive on a Mac Mini is a simple procedure that takes fifteen minutes, requiring a screwdriver and a putty knife. That I should have to drive to a God damned mall and explain to a “genius” that he doesn’t actually need my password to log in to OS X, wait for twenty minutes as the “genius” engages in manual data entry, then wait “seven to ten business days” for the part to be replaced is FUCKING SAD.

(Note: Hold down command+s during boot, run to the appropriate init level and type “passwd” to reset the password. Even someone who isn’t a “genius” can pull that off!)

12b The comment above is a

Compliment

Complaint

Suggestion

(more…)

5 Comments

June 21, 2007
Mac OS X, Technical, WordPress

Mac OS X and per-user Support for .htaccess

Link: http://dannyman.toldme.com/2007/06/21/mac-os-x-sites-htaccess-allowoverride/

Problem

I just spent a fair amount of time wrestling with Apache on my Macintosh. The problem is that it simply refused to read the .htaccess file in my user directory.

My First Approach

I took the “Unix Guy” approach and edited /etc/httpd/httpd.conf to ensure that Apache was configured to consult my user’s .htaccess file. I changed this bit:

<Directory /Users/*/Sites>
    AllowOverride FileInfo AuthConfig Limit
    Options MultiViews Indexes FollowSymLinks IncludesNoExec
    [ . . . ]

To read:

<Directory /Users/*/Sites>
    # AllowOverride FileInfo AuthConfig Limit
    AllowOverride All
    Options MultiViews Indexes FollowSymLinks IncludesNoExec
    [ . . . ]

But . . . nada. (more…)

Feedback Welcome

May 14, 2007
Mac OS X, Technical

Newbie Mac Hobo

Link: http://dannyman.toldme.com/2007/05/14/hopping-a-train/

So, I have a few gazillion things I would like to do, and some free time to play with. In terms of “professional development” I am looking for work, but more interesting to me is to have some time for education. I know an awful lot of things, especially about systems administration, and there are plenty of things that I don’t know, some things you would think I have done, but haven’t, and then there’s adding new stuff like learning Ruby on Rails, which could be danged handy if I choose to pursue contracting. To that end, I’ve had the “Agile Web Development with Rails” book collecting dust for a while . . . and a Macintosh desktop, to keep Unix confusing.

I’m into Chapter 3: Installing Rails, and the instructions for me don’t quite cut it, and a link they provide to Lucas Carlson’s blog no longer exists. But that needn’t stop us, because Google found me a very handy tutorial from Hivelogic: Building Ruby, Rails, Subversion, Mongrel, and MySQL on Mac OS X. It is from February and it does an excellent job of walking through the steps to fetch, build, and install the various pieces on a Macintosh, in /usr/local, like a real Unix system, and explaining all the important bits like fixing your $PATH and installing Xcode, and what the heck is sudo anyway. Stepping through the article is a breeze and you are left with a working Rails server, backed by MySQL, and the beginnings of a clue as to contemporary “best practices” like deployment-via-Capistrano. Huzzah!

In my case, I had to complete one other hurdle along the way. The “gem install rails” bit was erroring out for me when I followed the book and again with the Hivelogic article, with an error like: “Could not find rails (> 0) in any repository” . . . Google again found me an answer from Army of Evil Robots that basically boils down to:

gem update

Anyway, now I have a working Ruby setup on this computer, and I can hitch along to the next chapter, where I’ll learn to hop a freight.

I’m not sure what I might want to do with Rails, but “re-implement lnk.to” seems sort of obvious. If you are reading this and happen to have feelings about lnk.to, or link-shortening services, I welcome any thoughts, suggestions, wishes . . . thanks! -d

2 Comments


Site Archive

Danny Howard is 100% responsible for the content on this site, except some of it is stolen.

All rights are reserved, unless otherwise noted. Generally, I'm a BSD guy, so you can assume implicit permission to adapt, modify, and redistribute my intellectual property with appropriate attribution. Except some of this content is itself re-appropriated, so you'd best ask first, especially for commercial use. Thanks!

You can contact me via e-mail: dannyman@toldme.com

Most of http://dannyman.toldme.com/ is powered by WordPress.

If you're hip to RSS and whatnot, you can subscribe to this site.

These links are for dannyman: login AND backlinks