Tag Archives: time

ntp: Including my favorite, ntpdate

I learned about ntp and its cohort, ntpdate, when I ran into a machine so slow and old that it couldn’t keep time properly between reboots.

Its internal battery was shot, and powering it down meant the machine would reset at its next startup. And a reset clock triggered a BIOS warning, and … and … and …

The solution was to immediately synchronize its time across the Internet, as soon as it powered up. To that end, ntpdate became quite useful.

2014-02-11-lv-r1fz6-ntpdate

A quick

ntpdate -u pool.ntp.org

brings everything back into line. And it’s not just old machines that need nudging now and again. As you can see in the screenshot, even a high(er)-end machine can need adjustment over time.

I don’t bother with regular synchronizing though, just because I don’t think a shift of a few dozen seconds over the course of a couple months is worth the effort.

I must admit I haven’t worked much with ntp beyond ntpdate; it’s one of those tools I know about but don’t seem to have much call to use.

I know can do quite a bit more, but until the need arises, I am content to leave it as a mystery. 😉

date: The possibilities are endless

I had a few technical glitches this morning, if you were wondering why today’s posts were late. Which is a bit suspicious, since these two are technically the last ones I have left to share from the D section. 😐

First up is date, which ranks among some of my favorite command line tools. I saved it to the last mostly because I wanted to show a little more detail than my usual quick glance.

2013-10-17-lv-r1fz6-date

The key to understanding and loving this wonderful gift from coreutils is in the help flags. Most of the fun happens when you add formatting, but to start with, just this

date

will give you the current date and time, according to your machine’s clock.

Change it only slightly, and you get UTC time.

date -u

You can shift that information slightly, into the future or the past, by adding a -d flag and specifying 10 minutes, 2 hours, 8 weeks or what have you.

Formatting is where the fun is though. Here’s just the date, in easy-read year-month-day format.

date +%F

The plus sign triggers the formatting sequence, the percent sign specifies the style. Unless I am mistaken, this is how the date is usually shown in America.

date +%D

In other words, month-slash-day-slash-year. Counterintuitive from my perspective, but hey, who am I to blow against the wind?

Here’s date and time on the same line, with a space separating them.

date +%F\ %H:%M:%S

And here’s the same thing on two different lines. You can jam a newline into date with the %n format.

date +%F%n%H:%M:%S

Now let’s get really crazy. Write it all out like a sentence.

date +"Today is "%A", "%B\ %e", "%Y"."

And the current time.

date +"The time is "%-l:%M\ %p"."

This is fun to me. Stick the two together and you get the date and time all at once. Remember this when you’re fiddling with things like ticker.

date isn’t just for your local machine though. The TZ variable can be fed through date to show time zones outside local.

TZ='Asia/Tokyo' date

Ta-da! Get fancy with that stuff:

TZ='Asia/Tokyo' date +"Japan time is now "%-l:%M\ %p".

Oh, you’re an Internet hero, are you?

TZ='Asia/Tokyo' date +"Japan time is now "%-l:%M\ %p" on "%A", "%B\ %e", "%Y"."

No, I don’t just sit and tinker with date in my free time. Okay, actually, yes, I do. … 😐

If you interject date into other commands, you can arrange files or commands to include the date in specific formats. Ergo,

mv testfile.txt `date +%F-%H:%M:%S`.txt

That’s most often the way I use date; I like to arrange family photos and so forth by date and time. That includes screenshots. 😉

fbgrab -s 5 `date +%F-%H:%M:%S`-${HOSTNAME}-date.png

Sadly, beyond formatting and measuring out arbitrary lengths of time, date can’t really handle much in the way of calculating time. For that I recommend dateutils.

I could go on for a long time, but by now you should get the picture. If you need more examples, check out the examples page straight from the coreutils site, or Wikipedia’s list of examples. The possibilities are endless. …

In the mean time I’m going to dig around and see if I can get that last post from the D section ready again. … 😉

dateutils: Making everything better

I want to look over the standard date utility out of coreutils soon, but that will be a rather complex adventure, a la dmesg or top.

For now, I have dateutils to show, which is going to make some people in the audience very very happy indeed.

2013-09-29-v5-122p-dateutils

dateutils isn’t one tool, it’s about a half-dozen, each set up to manage dates and conversions quickly and seamlessly.

dateutils comes with tools that can easily find the difference between two times, add or subtract set amounts of time to dates, skim through lists to pick out specific dates, navigate backwards or forwards to a set date given a starting time, convert between calendars, generate sequences of dates or times and skip particular ones, compare dates in the shell, and a lot of other things.

dateutils alone is something you’ll probably want if you work with files or timestamped data at all; I’ve already put it to good use helping organize personal photos that I know were taken with the wrong date set on the camera.

That’s just the first example I could think of. If you have the slightest interest, you owe it to yourself to check this one out. It could conceivably save you immense time and effort.

A gold smilie for dateutils: 😀

cron: The chronological daemon

I think I have yet to meet a Linux distro that didn’t include cron, in some fashion or another, by default.

2013-09-26-4dkln41-crontab

I suppose in that sense it doesn’t bear my explanation. I’ve used it only rarely by design; other times, it was just running by default.

crontab is an underling to the daemon. If you want to get started with cron, the comments in the basic crontab -e display will give you hints.

If you need more of a jump start, you could look over this quick how-to that some geek wrote, years ago.

More help than that, and I would advise you to take a peek at the finest collection of computer know-how in the history of Linuxdom … the Arch wiki. 😉