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.
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.
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.
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.
So, assuming you are a SysAdmin, you really want to get a basic understanding of public key cryptography and the rest. But then, there’s a lot of stuff you need to learn and sometimes you just need to apply a patch, and would like some decent assurance that the patch hasn’t been compromised.
Today, I am patching–a few weeks too late–a FreeBSD system to reflect recent legislative changes to Daylight Saving Time. The procedure is very simple, and covered in FreeBSD Security Advisory FreeBSD-EN-07:04.zoneinfo. It starts:
a) Download the relevant patch from the location below, and verify the detached PGP signature using your PGP utility.
For many yers I have used FreeBSD nearly exclusively. In the BSD tradition, root is pretty well protected — root can not log in from remote unless you put some effort into hooking that up, and local users can only run su if they are members of the wheel group. Because of the nifty sudo tool and my own disinterest in memorizing any more passwords than necessary, I have tended to remain unconcerned with the root password, setting it and storing the thing somewhere, which is a pain, or setting it to something dumb, or just not setting it, depending on the security needs of a given system.
I recently learned a painful lesson from Fedora: not all unices are as protective of the root user. Sure, I knew that in Linux any local user can run su, but OpenSSH isn’t going to allow people to log in as root, right? Wrong!(more…)
If you’re like me, you run Firefox on FreeBSD, or maybe Linux. And you use a classy nice window environment like fvwm2. And every time you start Firefox it asks can it be the default browser, and you say yes … like you use anything else? (MSIE4-Solaris, anyone?) And every time you start, it asks again . . . stupid!
I just saw this solution posted to FreeBSD-questions: (more…)
I recently had a need for two quick temperature conversion algorithms in a Perl script. I asked Google, but did not immediately get a great answer, so here’s my answer:
# Two quick helper functions: CtoF and FtoC
sub CtoF { my $c = shift; $c =~ s/[^\d\.]//g; return (9/5)*($c+32); }
sub FtoC { my $f = shift; $f =~ s/[^\d\.]//g; return (5/9)*($f-32); }
The regex is to untaint the input datum, and could be eliminated if you know that your variable is clean. This code has been incorporated into a systems health and data trend monitoring script for FreeBSD. For the vaguely interested, here’s today’s perldoc: (more…)
Q: How do you measure swap utilization in FreeBSD? (Assuming you are writing a script to gather performance metrics.)
A: If you are writing a C program, check kvm_getswapinfo(3) and maybe take a gander at the bottom of /usr/src/usr.bin/top/machine.c.
A: If you are writing a Perl script:
Measure swap activity: sysctl vm.stats.vm.v_swapin vm.stats.vm.v_swapout vm.stats.vm.v_swappgsin vm.stats.vm.v_swappgsout
(I believe these results are COUNTER type values, like you get from netstat -inb. You could establish “swap activity” by plotting changes in this value.)
I am an NPR junkie, and my new place gets awful reception. And for a long time I have admired TiVo and wondered if my life might not be better if I had a DVR for NPR programs. Add to that KQED’s obnoxious plugin/popup window and the fact that I use an older version of RealAudio to avoid Spyware, which stutters and rebuffers all the time, and I have enough motivation to rig up something different.
I have rigged up a simple system to schedule rips of programs broadcast on the KQED audio stream in to easily manageable .mp3 files. If I had one of them iPod thingies I could even listen to the radio programs on the bus. I might even get around to warezing this to interested friends via BitTorrent and CSS, which would leave us another technical explanation. (more…)
I have also figured out how to convert the Windows Media Player stream in to mp3 files, and may set up a system to “record” programs on a regular schedule, at which point I can listen to public radio as I would watch TV on a DVR. (Radio TiVo!)
If anyone might be interested in getting in on a non-RealAudio “NPR audio archive” via a bittorrent setup, I’d love to hear from you.
Skype is an Instant Messenger client with a twist — if you hook up a microphone you can CALL each other. Like a telephone, only you can see if your buddy is online before you interrupt them. The audio quality is very good, and clients are available for Windows, OS X, and Linux. The Linux client works on FreeBSD.
Even nicer, you can dial out on Skype, for exceedingly low rates. It costs us about 3c a minute to call Japan, though we’re going to get Noriko-san on Skype soon enough, and then the calls will be free.
If anyone wants to try it out, you can ring me at dannymanTM.
To answer a question you may have on your mind, Skype is not a telephone, so it is different from a VOIP service, where they send you a telephone that you hook up to your broadband. Instead, Skype is a way of making telephone calls from your computer. Unfortunately, people can not yet dial in to someone using Skype.
For me, though, it is as if the Internet has come full-circle: we used to have to find a cheap local number to dial in to the Internet on our existing telephone. Now, we are finding cheap services to make telephone calls on our existing Internet connection. Yow!
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!