Thursday, September 4, 2014

Necessary Linux/RubyOnRails/Server Commands


Set time and restrict automatic update it with host machine at virtual box : 
         sudo service vboxadd-service stop                   

Set custom Date at ubuntu machine :
        sudo date -s "3 SEPT 2014 09:18:00"

Copy directory to remote server from remote server
       scp -P 22 -r sourcedir/ user@dest.com:/dest/dir/

Run a scheduled rake task(lets say update sphinx indexing on every 30 min periodically) on Lunux crontab:
       # find where is your rake(which rake)
       #m h dom mon dow user  command
      */30 * * * *  root /bin/bash -l -c 'searchd --stop -c /var/rails/project/current/config sudo dd if=/dev/zero of=/swapfile bs=1024 count=256k sudo dd if=/dev/zero of=/swapfile bs=1024 count=256k sudo dd if=/dev/zero of=/swapfile bs=1024 count=256k/production.sphinx.conf && cd /var/rails/project/current && RAILS_ENV=production /usr/local/bin/rake ts:rebuild && chmod -R 777 tmp/'

Create Symbolic link(Short cut)
       ln -s /source/dir destination/dire/short_cut_name
       ln -s -f /source/dir destination/dire/short_cut_name

truncate a file(0 means how many character you wanna keep)
       truncate -s0 /path/to/file

see a file's last portion on real time
       tail -f path/to/file

See a folder's size
       du -hs /path/to/folder

See a file size
       du /path/to/file -h

See full disc size
       df -h

Search file with pattern and delete them (from a specific directory)
       find . -name 'file_name_pattern*' -delete
       find . -name '*file_name_pattern' -delete

find running process and kill them by process id forcefully
       ps aux | grep rails
       kill -9 RAILS_PROCESS_ID

Kill all processes named for example "firefox"
       killall firefox

Request to specific port on a server:
       telnet SERVER_IP PORT

Zip a folder
       zip -r new_zep.zip /folder/path

tar a folder
       tar  -zcvf compressFileName folderToCompress

untar a gz file
       tar -xvzf file.tar.gz

Remove recursively a folder's content(keeping the folder itself)
       rm -rf /path/to/folder/*

Download
       wget download_url -O new_downloaded_file_name

Copy a folder
       cp -r  source/dir destination/dir

Make folder recursively
       mkdir -p /source/f1/f2/...../fn

To know 32 or 64 bit Linux machine
       uname -a

To know Ubuntu version
       lsb_release -a


How to change timezone on ubuntu:
       ln -sf /usr/share/zoneinfo/Asia/Bangkok  /etc/localtime
       ntpdate th.pool.ntp.org
       sudo dpkg-reconfigure tzdata


Better way to run long running Rails Rake Task:
       nohup bundle exec rake extract:infogenesis RAILS_ENV=production > rake.out 2&>&1 &

#How to login and then download a file using CURL preserving user loggedin cookie
       curl -c cookie.txt -d "username=USERNAME&password=PASSWORD" LOGIN_URL
       curl -X POST -b cookie.txt -d 'p1=d1&p2=d2' CSV_EXPORT_URL

# See all running cron tasks(be sure you switch to right user like root) inside ubuntu Machine
       crontab -l

# Activating swap memory on ubuntu server
       sudo dd if=/dev/zero of=/swapfile bs=1024 count=256k  # where swap memory will be double of count
       sudo mkswap /swapfile
       sudo swapon /swapfile

#Shutdown ubuntu machine automatically after 2 hours 
    sudo shutdown -h 120

#Linux search a content from a folders containing files
    grep -R -i -n 'the-text-you-are-searching-for' *

#For Large Sql Import
    SET autocommit=0 ; source the_sql_file.sql ; COMMIT ;