Bash trick: Repeat last command until success

More and more often I come across a scenario where I need to repeat the shell command until it succeeds. Here are a couple of examples:

  • Reboot a server. Try to remotely login to it via ssh. This fails until the server actually boots up. Keep trying until connected.
  • Start an application that writes to the log file. Run “tail -f some.log” to watch the log messages. This fails if the log file does not exist yet. Keep trying until the application creates the log file and writes something into it.

Sure, I can always press the up arrow key and Enter, to repeat the last command from the history. But it is a tiny bit annoying.

Today I came across this little trick, that solves the problem. Add the following function to your .bashrc:

rpt() {
  CMD=$(fc -ln | tail -n 2 | head -n 1)
  echo "repeating until success: $CMD"
  until $CMD
  do
    sleep 1
  done
}

Now you can run “rpt” to repeat the latest command until it succeeds.

Handy!

Calculating distance using MySQL

Calculating distance using MySQL” is a very useful blog post for everyone who works with geographical location data and MySQL. It shows a simple example of how to calculate the distance between two coordinates on a sphere (Earth in particular) within the MySQL itself.

SELECT ST_Distance_Sphere(
    point(-87.6770458, 41.9631174),
    point(-73.9898293, 40.7628267)
);

The above will return 1148978.6738241839, which is the distance between the two points in meters.

This functionality is available since MySQL 5.7. Have a look at the documentation of the spacial convenience functions.

MariaDB has similar functionality, but with a slightly different function names. Use ST_DISTANCE() instead of ST_Distance_Sphere(). Have a look at this blog post for more details.

GDPR cookie scanner

I came across the GDPR Expert service via this HackerNews thread. It is a service that helps website owners with the GDPR compliance. Behind the scenes, there is this open source tool, which scans for cookies and provides the details about the vendor and purpose of each identified cookie. The database includes more than 10,000 known cookies.

Very handy.

Telegram’s description of DDoS attack is the best

The servers of Telegram, a popular instant messenger, were under a DDoS attack recently. While they were working on the problem, they’ve tweeted a couple of explanations of what’s going on. CNET brings those tweets to our attention, as they explain rather complex things in a very short and simple way.

Internet Trends 2019 (Bond Report)

Internet Trends 2019 report is the most comprehensive, detailed, and research document that I have ever seen on what’s going on with the Internet, web, mobile, social media, marketing, and security.

This year’s report spans 333 pages and is full charts, graphs, statistics, insights, and references. And if you are feeling nostalgic, there is an archive of the annual reports going all the way back to 1995.

It’s difficult to pick a single fact from such a huge document, but if I had to, I’d go with this:

51% of the global population, or 3.8 billion people, were Internet users last year.

Wow. That’s quite a crowd.

Via Slashdot.