start delayed job on reboot [sh script]

Short way:
Making script run at boot time at Debian

Manual Way:

cd /etc/init.d && sudo nano delayed_job_script

Paste following code to delayed_job_script:
Note: change RAILS_ROOT and ENV variables according to your setup.


case "$1" in
    echo -n "Starting Delayed Jobs: "
    su - deploy -c "cd $RAILS_ROOT && RAILS_ENV=$ENV ruby script/delayed_job start -n 3"
    echo "done."
    echo -n "Stopping Delayed Jobs: "
    su - deploy -c "cd $RAILS_ROOT && RAILS_ENV=$ENV ruby script/delayed_job stop -n 3"
    echo "done."
    echo "Usage: $N {start|stop}" >&2
    exit 1

exit 0

Make file executable:

sudo chmod +x /etc/init.d/delayed_job_script

Create links in Linux Runlevels for delayed_job_script:

sudo ln -s /etc/init.d/delayed_job_script /etc/rc0.d/K80delayed_job_script
sudo ln -s /etc/init.d/delayed_job_script /etc/rc1.d/K80delayed_job_script
sudo ln -s /etc/init.d/delayed_job_script /etc/rc1.d/etc/rc2.d/S80delayed_job_script
sudo ln -s /etc/init.d/delayed_job_script /etc/rc1.d/etc/rc3.d/S80delayed_job_script
sudo ln -s /etc/init.d/delayed_job_script /etc/rc1.d/etc/rc4.d/S80delayed_job_script
sudo ln -s /etc/init.d/delayed_job_script /etc/rc1.d/etc/rc5.d/S80delayed_job_script
sudo ln -s /etc/init.d/delayed_job_script /etc/rc6.d/K80delayed_job_script

Naming convention for link file names
In rc0.d, rc1.d and rc6.d link name starts from K which means kill command will execute. In other run levels link file name starts with S which means start process. After S/K there is number 80 which is just process priority number. After that you can write any name of your choice.

Intro to Run Levels

Tagged with: , ,
Posted in Ubuntu

Check if same key pressed twice in 1second

var twice = 0;
var previous_key=null;
$(document).on('keyup', function( e ){
      console.log(previous_key+'(you pressed twice!)'+e.which);
    	twice = 0; 
    twice = 1;
    setTimeout(function(){ // ...reset to 0 after 1s
      twice = 0; 
    }, 1000);
Posted in Javascript

Check and export Java path and version Ubuntu

Check java version:

java -version

Check path of installed java:

readlink -f $(which java

Note: remove /bin/java from end of path
In ~/.bashrc file add following command at the end:

export JAVA_HOME='/usr/lib/jvm/java-7-openjdk-amd64/jre/'

Now run:

source ~/.bashrc
Tagged with:
Posted in java

Elasticsearch service not starting Ubuntu

Cause: While trying to run elasticsearch in background my elasticsearch user and group was recreated and had a new user and group id


 sudo chown -R elasticsearch:elasticsearch /var/lib/elasticsearch/


sudo service elasticsearch start
sudo service elasticsearch status

referenced from github

Posted in Elasticsearch

Elasticsearch 1.2.1 ::Transport::Transport::Errors::BadRequest – [400]

This error occurred because index was corrupted or missing

Solution either re-index your documents or copy index from your backup.

In Ubuntu elastic-search data directory path is mentioned below:


inside this directory project wise data directories exists. Copy your data directory from here to take backup once in a day or week.

And to fix your index either copy your backup from one system to another.


Use this link StackOverFlow: how to move elasticsearch data from one server to another

Posted in Elasticsearch

Heroku permission denied for database “postgres”

FATAL: permission denied for database “postgres” DETAIL: User does not have CONNECT privilege.

Run this command to solve this error:

heroku addons | grep POSTGRES
Tagged with: , ,
Posted in Heroku, Uncategorized

LaTeX | error algorithm.sty not found [Ubuntu 14.04]

If you use the algorithm package in your LaTeX document, as mentioned below:


you might get this error on compiling it

LaTeX Error: File `algorithm.sty’ not found.
For Ubuntu, this LaTeX package is bundled in the texlive-science package. To install this package.

Run this command in terminal:

sudo apt-get install texlive-science
Tagged with: ,
Posted in Latex

Connect with MongoDB on separate EC2 Instance

  • You need to open mongoDB port to world so that you can listen inbound traffic. To do so:
    In instance security group -> Inboud Open 27017 port or whatever port on which your mongoDB is currently running.

    Type             Protocol  Port          Range Source
    Custom TCP Rule     TCP       27017
    Custom TCP Rule     TCP       mongoDB_port  your_ip/CIDR

    Amazon FAQ about CIDR

  • In /etc/mongod.conf which is now default configuration file for mongoDB.
    Change binIP: to
    By default it listen to local host.

    # network interfaces
      port: 27017
  • Run this command to reflect changes:

    sudo service mongod restart
Tagged with: , , ,
Posted in amazon, EC2, mongodb, mongoid

Ubuntu Server Basic Libraries [ with SSL, nokogiri, postgress and nodjs ]

To install basic tools and nokogiri libraries

sudo apt-get install git build-essential patch ruby-dev zlib1g-dev liblzma-dev
sudo apt-get install -y libssl-dev libreadline-dev

Uglifier required library

sudo apt-get install nodejs

Postgres required library

sudo apt-get install libpq-dev

Install and Setup Nginx with Puma
Setup Nginx with puma on Ubuntu, with Godaddy

Tagged with:
Posted in Nokogiri, postgres, Ubuntu

Create and integrate SSL certificate in Rails app Using [ Godaddy + Nginx(1.8) + Puma + Ubuntu Server(14.04 LTS) ]

Login to your server by ssh and then:

  1. Create file YOUR_RAILS_APP_DIRECTORY/config/puma.rb
    Below is puma.rb file content:

    #!/usr/bin/env puma
    directory '/home/ubuntu/YOUR_RAILS_APP_DIRECTORY/public/'
    rackup '/home/ubuntu/YOUR_RAILS_APP_DIRECTORY/'
    environment 'production'
    daemonize true
    pidfile '/home/ubuntu/YOUR_RAILS_APP_DIRECTORY/tmp/pids/'
    state_path '/home/ubuntu/YOUR_RAILS_APP_DIRECTORY/tmp/pids/puma.state'
    stdout_redirect '/home/ubuntu/YOUR_RAILS_APP_DIRECTORY/log/puma.log'
    threads 2, 5
    bind 'unix:///home/ubuntu/YOUR_RAILS_APP_DIRECTORY/tmp/sockets/puma.sock'
    workers 2
  2. Generate CSR certificate

     openssl req -new -newkey rsa:2048 -nodes -keyout SITE_DOMAIN_NAME.key -out SITE_DOMAIN_NAME.csr

    Note: For instance site domain is then you will place only facebook.key and facebook.csr

  3. .key file will be used in nginx configurations.
    Copy .csr file’s content and then paste its content to godaddy CSR field.

    After generating ssl certificate using godaddy, download it. Then upload that downloaded zip to Ubuntu Server using scp command.

  4. Unzip the folder there will be two .crt files in it. Chain those files using this command.

    cat file_name.crt  file_containing_bundle_in_name.crt > some_name.chained.crt

    Note: Place them .chained.crt file in that order that file containing bundle in its name comes after like above command.

  5. The resulting chainned file should be used in the ssl_certificate directive:

    server {
        listen              443 ssl;
        ssl_certificate     some_name.chained.crt;
        ssl_certificate_key file_generated_by_openssl_command.key;
  6. Create file /etc/nginx/conf.d/your_app_name.conf

    ###########################################SETUP SSL ON NGINX USING PUMA############################
    #upstream name should be same name passed in "proxy_pass" option (like in my case 'cuhivetech')
    upstream cuhivetech {
        #puma.sock file will be generated by puma when it will start, you have to bind below mentioned file path in YOUR_APP_PATH/config/puma.rb
            server unix:///home/ubuntu/YOUR_APP_DIRECTORY/tmp/sockets/puma.sock;
    #Redirect http requests to https
    server {
        listen 80;
        return 301 https://$host$request_uri;
    # HTTPS server
    server {
        listen 443;
        root /home/ubuntu/YOUR_APP_DIRECTORY/public;
            try_files $uri/index.html $uri.html $uri @app;
        ssl_certificate /home/ubuntu/GODADDY_CERT_DIRECTORY/cuhivetech.chained.crt;
        ssl_certificate_key /home/ubuntu/cuhivetech.key;
        ssl on;
        ssl_session_cache  builtin:1000  shared:SSL:10m;
        ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
        ssl_prefer_server_ciphers on;
        proxy_read_timeout  90;
        location @app {
            proxy_set_header        Host $host;
            proxy_set_header        X-Real-IP $remote_addr;
                  proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
                  proxy_set_header        X-Forwarded-Proto $scheme;
                proxy_pass    http://cuhivetech;
                proxy_redirect      http://cuhivetech https://cuhivetech;
  7. Now restart Nginx server using command:

    sudo service nginx restart
  8. Go back to

    • goto domains
    • Click on “Manage DNS” of your specific domain
    • Click on “DNS Zone File”
    • Edit entry in “A (Host)” and add your IP address in “POINTS TO” field.
  9. Now start Web server using command:

    bundle exec puma -C config/puma.rb 
  10. It will take some time to reflect changes. Now You can access site using your domain.

godaddy ssl certificate guide
godaddy request ssl certificate

Tagged with: , , ,
Posted in amazon, DNS, Godaddy, Nginx, production, Puma, Ruby on Rails, Ubuntu