Wednesday, August 23, 2017

How to kill an abandoned process in Linux/Unix

I remembered it, then I forgot, then I remembered it, and then I forgot again.

In case of a Linux/Unit process hang, I have to figure out the PID of the stalled process and then use the kill command to close it.
  • To figure out the PID of the stalled process, in shell terminal type:
ps -u [username]

  • Then, use your own justification to figure out the PID. To kill a process with specific PID:
kill -9 [PID]

References:

How to see process created by specific user in Unix/linux
http://unix.stackexchange.com/questions/85466/how-to-see-process-created-by-specific-user-in-unix-linux

The kill Command
http://www.linfo.org/kill.html

Tuesday, August 15, 2017

Switching from SQLite to PostgreSQL in Development Environment

I don't understand why Ruby on Rails Tutorial uses SQLite database for development and uses PostgreSQL database in Heroku for production.
Why don't they use PostgreSQL for all the environments? To use PostgreSQL in development environment, it is as simple as just adding the "-d postgresql" parameter when creating a new Rails project.
$ rails new myapp -d postgresql

I am writing this because when I was going further after the Rails Tutorial, I just wanted to add Comments and Articles models for a message board, some of the database migration codes that worked well in Sqlite but they do not work in Heroku's PostgreSQL.
For example, in db/migrate/20170812232746_add_user_ref_to_articles.rb
# This line works in Sqlite development enviroment
# This line does not work in Heroku's PostgreSQL production environment
add_index :articles, [:user_id, :created_at]   

So, I decided to switch from Sqlite database to PostgreSQL database in my Linux development environment. Approximately, this was what I did for the switching of the database.

In project: myapp_postgres


* I created a new Rails project that uses PostgreSQL:
$ cd ror
$ rails new myapp_postgres -d postgresql

* I made sure that I could create the PostgreSQL database and started the Rails server.
$ cd myapp_postgres
$ rake db:create
$ rails s -b 0.0.0.0 -p 3000

* I went "Turbo Speed" for the Chapter 2 of Rails Tutorial. I did NOT change the Gemfile of myapp_postgres project. And I did NOT change database.yml.

* I initialized a local Git repository, added all files, committed the changes, added remote Github URL, and pushed it onto Github.
$ git init
$ git add -A
$ git commit -m "Initialize repository"
$ git remote add origin https://github.com/jimmy2046/myapp_postgres.git
$ git push -u origin --all
* I created the "hello" action, committed the changes, created an Heroku project and pushed it onto Heroku.
$ git commit -am "Add hello"
$ heroku create
$ git push heroku master

* I generated the scaffold for User and made sure the database migration command worked.
$ rails generate scaffold User name:string email:string
$ rails db:migrate
* I generated the scaffold for Micropost.
$ rails generate scaffold Micropost content:text user_id:integer
$ rails db:migrate
* Using Firefox, I tried to add a new user and added a new micropost in order to make sure the local PostgreSQL database was working properly in development environment.

* At the end of Chapter 2, I added all untracked files, committed the changes, and pushed project myapp_postgres onto Github.
$ git add -A
$ git commit -m "Finish toy app"
$ git push

* At the same time, I pushed myapp_postgres to Heroku and ran database migration for Heroku production.
$ git push heroku
$ heroku run rails db:migrate

* At that time, I had a new PostgreSQL database project called myapp_postgres with configurations and settings for Heroku PostgreSQL deployment.

In project: myapp2

* After that, I went back to myapp2 project. I changed the the directory myapp2 and checked out a new branch called: "attempt-switch-to-postgresql".
$ cd ~/myapp2
$ git checkout -b  attempt-switch-to-postgresql

* In myapp2, I made a backup copy of the database.yml file. And then, I copied and pasted the database.yml file, that I got from myapp_postgres using "$ rails new myapp_postgres -d postgresql" to myapp2.
myapp2/config/database.yml
# PostgreSQL. Versions 9.1 and up are supported.
#
# Install the pg driver:
#   gem install pg
# On OS X with Homebrew:
#   gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On OS X with MacPorts:
#   gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
#   gem install pg
#       Choose the win32 build.
#       Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem 'pg'
#
default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see Rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: myapp_postgres_development

  # The specified database role being used to connect to postgres.
  # To create additional roles in postgres see `$ createuser --help`.
  # When left blank, postgres will use the default role. This is
  # the same name as the operating system user that initialized the database.
  #username: myapp_postgres

  # The password associated with the postgres role (username).
  #password:

  # Connect on a TCP socket. Omitted by default since the client uses a
  # domain socket that doesn't need configuration. Windows does not have
  # domain sockets, so uncomment these lines.
  #host: localhost

  # The TCP port the server listens on. Defaults to 5432.
  # If your server runs on a different port number, change accordingly.
  #port: 5432

  # Schema search path. The server defaults to $user,public
  #schema_search_path: myapp,sharedapp,public

  # Minimum log levels, in increasing order:
  #   debug5, debug4, debug3, debug2, debug1,
  #   log, notice, warning, error, fatal, and panic
  # Defaults to warning.
  #min_messages: notice

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: myapp_postgres_test

# As with config/secrets.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
#   DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
#
# You can use this database configuration with:
#
#   production:
#     url: <%= ENV['DATABASE_URL'] %>
#
production:
  <<: *default
  database: myapp_postgres_production
  username: myapp_postgres
  password: <%= ENV['MYAPP_POSTGRES_DATABASE_PASSWORD'] %>

* In Gemfile, I put gem 'pg', '0.18.4' on the top section. That meant Rails would use PostgreSQL for all Development, Testing and Production environments. I put a remark comment for gem 'sqlite3', '1.3.13' in development group, and I commented out the gem 'pg', '0.18.4' in Production group.
myapp2/Gemfile
source 'https://rubygems.org'

gem 'rails',        '5.1.2'
gem 'bcrypt',         '3.1.11'
gem 'faker',          '1.7.3'
gem 'carrierwave',             '1.1.0'
gem 'mini_magick',             '4.7.0'
gem 'fog',                     '1.40.0'
gem 'will_paginate',           '3.1.5'
gem 'bootstrap-will_paginate', '1.0.0'
gem 'bootstrap-sass', '3.3.7'
gem 'puma',         '3.9.1'
gem 'sass-rails',   '5.0.6'
gem 'uglifier',     '3.2.0'
gem 'coffee-rails', '4.2.2'
gem 'jquery-rails', '4.3.1'
gem 'turbolinks',   '5.0.1'
gem 'jbuilder',     '2.7.0'

gem 'pg', '0.18.4'

group :development, :test do
#  gem 'sqlite3', '1.3.13'
  gem 'byebug',  '9.0.6', platform: :mri
end

group :development do
  gem 'web-console',           '3.5.1'
  gem 'listen',                '3.0.8'
  gem 'spring',                '2.0.2'
  gem 'spring-watcher-listen', '2.0.1'
end

group :test do
  gem 'rails-controller-testing', '1.0.2'
  gem 'minitest-reporters',       '1.1.14'
  gem 'guard',                    '2.13.0'
  gem 'guard-minitest',           '2.4.4'
end

group :production do
#  gem 'pg', '0.18.4'end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

* Then, I ran bundle update.
$ bundle update

* And the, I restarted the my local Rails server.
Ctrl + C
$ rails s -b 0.0.0.0 -p 3000

* In local Linux development environment, I attempted to drop the database, created a new one. But, I was stuck during database migration.
$ rake db:drop db:create db:migrate

* It took me some time for my debug skill. Afterwards, I figured out that the add_index for user_id code worked well in Sqlite but not in PostgreSQL. So, I commented out the add_index line for model Articles and model Comments.
myapp2/db/migrate/20170812232746_add_user_ref_to_articles.rb
class AddUserRefToArticles < ActiveRecord::Migration[5.1]
  def change
    add_reference :articles, :user, foreign_key: true
  end
   
#    add_index :articles, [:user_id, :created_at]   
end
Similarly,
myapp2/db/migrate/20170814052759_add_user_to_comments.rb
class AddUserToComments < ActiveRecord::Migration[5.1]
  def change
    add_reference :comments, :user, foreign_key: true
  end
#    add_index :comments, [:user_id, :created_at]   
end

* After I had commented out the add_index to the database, I ran database migration again. The migration was successful.
$ rails db:migrate

* Then, I seeded the database.
$ rails db:seed

* Then, I deleted the old Sqlite database file in myapp2/db directory.
myapp2/db
deleted: test.sqlite3
deleted: development.sqlite3

* Finally, I pushed the changes to Github and deployed to Heroku.
$ rails test
$ git add -A
$ git commit -m "Changed to PostgreSQL in Development Env"
$ git checkout master
$ git merge attempt-switch-to-postgresql
$ git push

$ git push heroku
$ heroku pg:reset DATABASE
$ heroku run rails db:migrate
$ heroku run rails db:seed

* My Gihub URL is: https://github.com/jimmy2046/myapp2/commit/8a00ad4b549feb43cc832727c4701f6291e48334

* My Heroku URL is: https://vast-mesa-46380.herokuapp.com/articles?class=nav-link

Reference websites:

How to change your Rails app database from SQLite to PostgreSQL before deploying to Heroku
https://medium.com/@helenflam/how-to-change-your-rails-app-database-from-sqlite-to-postgresql-before-deploying-to-heroku-ae2acc25c7ac

Change from SQLite to PostgreSQL in a fresh Rails project
https://stackoverflow.com/questions/6710654/change-from-sqlite-to-postgresql-in-a-fresh-rails-project

Installing PostgreSQL database for Ruby on Rails on Ubuntu Linux

I realized that I did not write a blog for the installation procedures of PostgreSQL database for Ruby on Rails on Ubuntu Linux. This is an excerpt from GoRails.com :

Setting Up PostgreSQL

For PostgreSQL, we're going to add a new repository to easily install a recent version of Postgres.
sudo sh -c "echo 'deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main' > /etc/apt/sources.list.d/pgdg.list"
wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get install postgresql-common
sudo apt-get install postgresql-9.5 libpq-dev

The postgres installation doesn't setup a user for you, so you'll need to follow these steps to create a user with permission to create databases. Feel free to replace chris with your username.
sudo -u postgres createuser chris -s

# If you would like to set a password for the user, you can do the following
sudo -u postgres psql
postgres=# \password chris

Creating Ruby on Rails application

#### If you want to use Postgres
# Note that this will expect a postgres user with the same username
# as your app, you may need to edit config/database.yml to match the
# user you created earlier

rails new myapp -d postgresql

Author's note: I did not modify the 'database.yml' file. I just left it as it the original from the 'rails new' command.

# Move into the application directory
cd myapp

# If you setup MySQL or Postgres with a username/password, modify the
# config/database.yml file to contain the username/password that you specified

# Create the database
rake db:create

rails server

You can now visit http://localhost:3000 to view your new website!

Reference:

Setup Ruby On Rails on Ubuntu 16.04 Xenial Xerus
https://gorails.com/setup/ubuntu/16.04#postgresql

Thursday, August 10, 2017

Linux: Increasing Increasing the amount of inotify watchers

Today, I wanted to add Article and Comment for my Ruby on Rails myapp2 project. It is based on Getting Started with Rails online tutorial (http://guides.rubyonrails.org/getting_started.html).

When I typed Rails generate controller command, something weird was shown on the screen:
jimmyc@Jimmy-C-2017:~/myapp2$ rails generate controller Articles
FATAL: Listen error: unable to monitor directories for changes.
Visit https://github.com/guard/listen/wiki/Increasing-the-amount-of-inotify-watchers for info on how to fix this.

Then, I searched on Google: "FATAL: Listen error: unable to monitor directories for changes". The search result showed a Stack Overflow article. I also surfed the Github article that was shown on Linux terminal.

I tried to type this command in shell terminal and hopefully it could get through:
$ echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p

my shell terminal:
jimmyc@Jimmy-C-2017:~/myapp2$ echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p
[sudo] password for jimmyc:
fs.inotify.max_user_watches=524288
fs.inotify.max_user_watches = 524288

Good, this time the Rails Generate Controller command was successful.
jimmyc@Jimmy-C-2017:~/myapp2$ rails generate controller Articles
Running via Spring preloader in process 3769
      create  app/controllers/articles_controller.rb
      invoke  erb
      create    app/views/articles
      invoke  test_unit
      create    test/controllers/articles_controller_test.rb
      invoke  helper
      create    app/helpers/articles_helper.rb
      invoke    test_unit
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/articles.coffee
      invoke    scss
      create      app/assets/stylesheets/articles.scss

Reference:
Increasing the amount of inotify watchers
https://github.com/guard/listen/wiki/Increasing-the-amount-of-inotify-watchers

Listen error: unable to monitor directories for changes
https://stackoverflow.com/questions/42225677/listen-error-unable-to-monitor-directories-for-changes

Tuesday, August 8, 2017

Linking a CSS file on a sub-directory of a parent directory in HTML

Sometimes, for a better organized file system of a web design, I need to create some sub-directories and put those files that belonged to the same category into the same directory.

For example, this is the file structure of my HTML website:
+--- html_publish/
|   +--- images/
|        pic01-IMG_0584.JPG
|        pic02-IMG_0585.JPG
|
|   +--- photo01/
|        detail01.html
|        detail02.html
|
|   +--- saturn/
|       +--- css/
|            custom.css
|
|   +--- templated-radius/
|       +--- css/
|            main.css
|
|   index.html
|   photo01.html
|
  
The file details01.html is located under the subdirectory photo01

In Linux shell terminal, the path is:
~/html_publish/photo01/details01.html

If I want to link the CSS style that located in
~/html_publish/templated-radius/css/main.css. 

To link the CSS file (main.css) from detail01.html, I need to go up to one parent directory (html_publish), and then go down to templated-radius sub-directory and then go down to css sub-directory. In the details01.html file:
~/html_publish/photo01/details01.html
<link rel="stylesheet" href="../templated-radius/css/main.css" />

The "two dots" (../) means go up one directory. In some circumstances, if I want to go up 2 levels of directory I can do (../../).

Similarly, I want to link an image file that is located under the sub-directory images of the parent directory.
Images file:
~/html_publish/images/pic01-IMG_0584.JPG

And the HTML file detail01.html is located under the photo01 sub-directory.
HTML file:
~/html_publish/photo01/details01.html

If I want to link the image file, for the <img> tag in details01.html:
~/html_publish/photo01/details01.html
<img src="../images/pic01-IMG_0584.JPG" alt="" />

Reference:
How do I actually link a CSS stylesheet to a HTML sheet?
https://www.codecademy.com/en/forum_questions/553bcfc6d3292f7e1b000760

Saturday, August 5, 2017

Suitable Image Pixel Size for Blogging

It is a common sense that any picture taken from iPhone and Android phone needs to be re-sized before it is good to post on a blog.

But, there is no standardized way to shrink an image. In Photoshop, I used to use the "Resize image" command to shrink an image. I maintained the aspect ratio and I shrank the image to certain percentage by "trial-and-error" or "guessing".

I just found a magic number on a website: 690 pixels. I do not understand why 690 pixels, but it works very good at least for my blog page: (https://jimmy2046.github.io/photo01.html)

For example, in Linux Pinta image editor, if the original size of one side of an image is 1800 pixels.
  • I click the Image -> Resize Image menu
  • In Resize Image window, click the by Absolute Size button
  • Make sure the "Maintain aspect ratio" option is checked on
  • (For me), on the longer side, I type 690 pixels. Notice that another size will also be shrunk automatically
  • Click OK button. Now the image is good for online HTML blog posting


Reference:
10 Tips on Best Image Size for Your Blog
http://www.1dogwoof.com/10-tips-best-image-size-blog/

Thursday, August 3, 2017

Using Github Pages to host HTML CSS and Javascript website

I did not know that GitHub offers GitHub Pages that allow members to host a website directly from their GitHub repository. Each member can get one site per GitHub account.

The procedures to host a website using GitHub Pages on Ubuntu Linux:
  • Create a repository. Head over to GitHub and create a new repository named username.github.io, where username is your username (or organization name) on GitHub. If the first part of the repository doesn’t exactly match your username, it won’t work, so make sure to get it right.
  • On my Linux computer, create a directory. For example, html_publish and copy all the HTML, CSS and Javascript of the website file into that directory. My directory structure is:
+--- html_publish/
|    /about.html
|    /blog.html
|    /contact.html
|    /index.html
|    /README.md
|    /services.html
|   +--- css/
|        /bootstrap.min.css
|        /magnific-popup.css
|        /templatemo-style.css
|   +--- font-awesome-4.5.0/
|        /HELP-US-OUT.txt
|       +--- css/
|            /font-awesome.min.css
|       +--- fonts/
|            /FontAwesome.otf
|            /fontawesome-webfont.eot
|            /fontawesome-webfont.svg
|            /fontawesome-webfont.ttf
|            /fontawesome-webfont.woff
|            /fontawesome-webfont.woff2
|   +--- img/
|        /img-11-01.jpg
|        /img-11-02.jpg
|        /img-11-03.jpg
|        /img-11-04.jpg
|        /img-11-05.jpg
|        /img-11-06.jpg
|        /img-11-07.jpg
|        /img-11-08.jpg
|        /img-21-01.jpg
|        /img-21-01.jpg
|        /img-21-02.jpg
|        /img-21-03.jpg
|        /img-21-04.jpg
|        /img-21-05.jpg
|        /img-21-06.jpg
|        /logo-bg.jpg
|   +--- js/
|        /bootstrap.min.js
|        /jquery-1.11.3.min.js
|        /jquery.magnific-popup.min.js

  • Then, I used Linux shell terminal to create a README.md file, initialized a Git repository, added all untracked files, committed the changes, set remote GitHub push fetch URL locations, and pushed the files to GitHub. This is what I did:
cd html_publish

echo "# jimmy2046.github.io" >> README.md
git init
git add README.md
git commit -m "first commit"

git add -A
git commit -m "added all html css images javascript files"

git remote add origin https://github.com/jimmy2046/jimmy2046.github.io.git
git push -u origin master

  • This is a more reader friendly version of Git shell commands. Please replace username with your GitHub username.
cd html_publish

echo "# username.github.io" >> README.md
git init
git add README.md
git commit -m "first commit"

git add -A
git commit -m "added all html css images javascript files"

git remote add origin https://github.com/username/username.github.io.git
git push -u origin master


 
  • GitHub Pages support all functionalities of CSS and Javascript. It allows me to have total freedom and flexibility of website design.  The speed of the GitHub server is extremely fast. Fantastic.

References:
GitHub Pages | Websites for you and your projects, hosted directly from your GitHub repository. Just edit, push, and your changes are live.
https://pages.github.com/

Finally, a Guide to Hosting Your Website
https://www.jonhmchan.com/blog/2014/4/28/finally-a-guide-to-hosting-your-website

Author's GitHub Pages (under construction)
https://jimmy2046.github.io/index.html

How to kill an abandoned process in Linux/Unix

I remembered it, then I forgot, then I remembered it, and then I forgot again. In case of a Linux/Unit process hang, I have to figure out ...