Skip to content

Instantly share code, notes, and snippets.

@ChengLong
ChengLong / install_phusion_passenger.md
Created July 23, 2013 16:44
Phusion Passenger Installation Error

If you see the following error when you run sudo passenger-install-apache2-module

Your RVM wrapper scripts are too old, or some wrapper scripts are missing. Please update/regenerate them first by running:

rvmsudo rvm get stable && rvm reload && rvmsudo rvm repair all

If that doesn't seem to work, please run:

rvmsudo rvm wrapper [] --no-prefix --all*

@ChengLong
ChengLong / InstructionsForYCM-Mac.md
Last active May 22, 2024 06:50
Install YouCompleteMe on Mac OS X
@ChengLong
ChengLong / findIP.md
Created September 10, 2013 06:56
Find IP of EC2 instance

To find IP of an EC2 instance

ssh to the instance, run curl http://169.254.169.254/latest/meta-data/public-ipv4

To find hostname of an EC2 instance

ssh to the instance, run curl http://169.254.169.254/latest/meta-data/public-hostname

@ChengLong
ChengLong / convert_to_pem.md
Last active December 24, 2015 21:18
Convert cer/p12 to pem

openssl x509 -inform der -in certificate.cer -out certificate.pem

openssl x509 -inform der -in certificate.p12 -out certificate.pem

@ChengLong
ChengLong / will_paginate in Sinatra.md
Last active January 9, 2021 00:09
Use will_paginate with ActiveRecord in Sinatra
  1. Add gem 'will_paginate', '~> 3.0' to Gemfile
  2. Add %w(will_paginate will_paginate/active_record).each {|lib| require lib} to config.ru
  3. Assuming modular Sinatra, open Sinatra::Base and add include WillPaginate::Sinatra::Helpers so that all views can use will_paginate
  4. In controller, @users = User.paginate(:page => params[:page], :per_page => 30)
  5. In view, = will_paginate @users will generate the pagers

If you want to style your pagers

  • Include this css in your layout
  • Style it
@ChengLong
ChengLong / run_long_process_in_background.rb
Last active April 19, 2021 20:59
Run Background Process in Sinatra
require 'sinatra'
get '/long_process' do
child_pid = Process.fork do
# hard work is done here...
sleep 10
Process.exit
end
@ChengLong
ChengLong / find_large_files.md
Last active January 2, 2016 08:49
Find large files

The following command will list the top 10 biggest files, in descending order of file size, in the current directory.

sudo du -sm ./* | sort -nr | head -n 10

If the top result is a directory, you can cd into that directory and run the command again until you know what file is taking big space.

The above method is useful if you DO NOT know any characteristics of sought-after files. But if you DO know some characteristics like file name pattern or creation time, you are probably better off using find

find . -name "*.log" | xargs du -m | sort -nr | head -n 10

@ChengLong
ChengLong / delete_old_logs.md
Last active August 29, 2015 13:56
Delete old log files

Often, you find that log files are taking too much disk space. You want to delete them efficiently

find /path/to/log/directory -type f -mtime +180 | xargs rm

This command will delete all files older than 180 days in /path/to/log/directory. If you know your log files' name pattern, it's better to specify that pattern in the find command.

You probably don't want to do this manually everytime. So add it to cronjob

0 0 1 * * find /path/to/log/directory -type f -mtime +180 | xargs rm > /dev/null 2>&1

@ChengLong
ChengLong / emoji_mysql.md
Created March 24, 2014 09:54
Store Emoji in Mysql on Rails

I recently came accross this error while working on a rails app

ActiveRecord::StatementInvalid (Mysql2::Error: Incorrect string value: '\xF0\x9F\x98\x84' for column ...

A bit search shows that this error is famously known. You can see more details from here and here.

In short, this error happens because Mysql uses 3 bytes for utf8 character by default. So when a 4-byte character is sent, it crashes.

Solution: use encoding utf8mb4 and collation utf8mb4_unicode_ci

@ChengLong
ChengLong / ruby_cron.md
Created April 17, 2014 06:34
Run Ruby Script using cron job

If you see this error when cron job runs a ruby script:

'require': cannot load such file ...

And you are using bundler e.g. require 'bundler/setup'. It's probably because the directory where cron runs the script is not correct, resulting in bundler fails to load gems.

Simply changed the directory to fix it, i.e.

* * * * * /usr/local/bin/ruby -C /home/example/scripts example_script.rb >>/home/example/log/cron.log 2>&1