Wednesday, January 8, 2020

Time adjustment for DigitalOcean droplets

Hello!

I have some droplets on DigitalOcean, but I noted that the system time (in all of them) is not correctly synced with NTP servers.

The solution that worked for me is to include the following line at the end of /etc/crontab:

* * * * * root /sbin/hwclock -s

That command instructs the system to copy time from hardware (which is more precise) every minute. That solved my problem.

Friday, December 27, 2019

Troubleshooting Ruby 2.7 compiler errors

So Ruby 2.7 was released at Xmas'19 and I got some errors when compiling it in Linux Mint 19.1. Some libraries were not configured. The errors ocurred at the linking phase of make. Five libraries got errors, and I'll walk across their solutions:

  • openssl:
    sudo apt install libssl-dev
  • gdbm:
    sudo apt install libgdbm-dev
  • dbm:
    sudo apt install libqdbm-dev
  • readline:
    sudo apt install libedit-dev
  • zlib:
    sudo apt install libz-dev
    cd /lib/x86_64-linux-gnu/
    sudo ln -s libz.so.1 liblibz.so

Enjoy!

Monday, May 13, 2019

Detecting key press in Ruby

To check whether a key is pressed in linux (non-blocking, non-waiting).
def keypressed
  system('stty raw -echo') # => Raw mode, no echo
  char = (STDIN.read_nonblock(1).chr rescue nil)
  system('stty -raw echo') # => Reset terminal mode
  char
end
Inspired on https://stackoverflow.com/a/22659929.

Saturday, October 27, 2018

Net::HTTP error sending GET request with parameters

If you're getting trouble with Net::HTTP#send_request when sending a GET request with parameters, one possible cause is that parameters must be part of path as a query string, and not as data (3rd parameter of #send_request).

For example (assuming that parameters is not empty), instead of

http = Net::HTTP.new('server.com')
http.send_request('GET', path, parameters, headers)

try:

http = Net::HTTP.new('server.com')
http.send_request('GET', path + '?' + parameters, '', headers)

Net::HTTP error 400 (bad request) with HTTPS

Hello, friends!

Yesterday I got stuck when trying to connect to an HTTPS site using Net::HTTP:

      http = Net::HTTP.new('server.com', 443)
      response = http.send_request('GET', path, parameters, headers)

I was getting error 400 in response.code.

What is not well documented is that when accessing a server via HTTPS protocol it's not enough to set port to 443. You must also set use_ssl to true:

      http = Net::HTTP.new('server.com', 443)
      http.use_ssl = true
      response = http.send_request('GET', path, parameters, headers)


Tuesday, March 21, 2017

Pipe inside a bash script

Hello!

Today I got stuck trying to use pipe inside a command variable in a bash script:

$CMD="echo foo | cat"
EXEC=`$CMD`                 # expecting foo
echo $EXEC                  # got foo | cat


I could solve that if I run the command directly, without variables:

EXEC=`echo foo | cat`       # expecting foo
echo $EXEC                  # got foo


But that was not an option, because $CMD was complex and carefully mounted before that snippet.

Googling around I found that the solution is to use eval (although I used it slightly different from the source):

$CMD="echo foo | cat"
EXEC=`eval $CMD`            # expecting foo
echo $EXEC                  # got foo


The reason is that eval can expand the pipe command inside a variable, while backticks can't.

Cheers!

Wednesday, August 31, 2016

How to install mysql2 gem on Ubuntu

If you are having trouble to install the mysql2 gem, try the following:

# apt-get install build-essential ruby-dev libmysqlclient-dev