Skip to content

Instantly share code, notes, and snippets.

@barek2k2
Last active July 26, 2023 10:43
Show Gist options
  • Save barek2k2/6038382 to your computer and use it in GitHub Desktop.
Save barek2k2/6038382 to your computer and use it in GitHub Desktop.
Useful linux commands while developing Rails web applications
Copy directory to remote server from remote server
scp -r sourcedir/ user@dest.com:/dest/dir/
scp -i /home/barek/Dropbox/clients/Ongoing_Clients/instaprize/rafflehunter.pem ubuntu@18.191.19.52:/home/ubuntu/rafflehunter_production.dump.gz /home/barek/Dropbox/clients/Ongoing_Clients/instaprize/
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
#shortcut node to nodejs
sudo ln -s /home/ubuntu/.nvm/versions/node/v10.5.0/bin/node /usr/bin/nodejs
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
#Kill with namespecifying
pkill -f skype
Request to specific port on a server:
telnet SERVER_IP PORT
Zip a folder
zip -r new_zep.zip /folder/path
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(1GB~1024k) on ubuntu server
sudo dd if=/dev/zero of=/swapfile bs=1024 count=1024k # where swap memory will be the value of count
sudo mkswap /swapfile
sudo swapon /swapfile
# Removing swap memory
swapoff /swapfile
rm /swapfile
# Copy public ssh key from local to remote machine to avoid prompt password on each ssh
# It will create authorized_keys folder on ~/.ssh
# on Amazon EC2, authorized_keys folder exists already
cat ~/.ssh/id_rsa.pub | ssh -i /path/to/pemfile.pem USER@HOST "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
Mysql big file import
SET autocommit=0 ; source the_sql_file.sql ; COMMIT ;
#Undo last push and checkout to a specific commit
git reset --hard <revision_id_of_last_known_good_commit>
git push --force
#git forecfully pull from master and override all local changes
git fetch origin master
git reset --hard FETCH_HEAD
# Change git latest commit time to current time
GIT_COMMITTER_DATE="`date`" git commit --amend --date "`date`"
# Removes local branches those are already merged with master branch
git branch --merged master | grep -v '^[ *]*master$' | xargs git branch -d
#Simple Log rotation in ubuntu
sudo nano /etc/logrotate.conf
/home/barek/projects/therapymate/log/*.log {
daily
missingok
rotate 7
compress
delaycompress
notifempty
copytruncate
}
#Restore/Import from Heroku backup
heroku pg:backups capture
curl -o latest.dump `heroku pg:backups public-url`
pg_restore --verbose --clean --no-acl --no-owner -h localhost -U barek -d therapymate_heroku latest.dump
##Docker
# import host database into docker container
docker cp gaport.sql DOCKER_CONTAINER_ID:/
psql -h localhost -d gaport_development -U postgres -f gaport.sql
#ssh into docker container
docker exec -it DOCKER_CONTAINER_ID /bin/bash
#List all running container
docker ps
#stops all running containers
docker-compose stop
#Stops specific running container
docker stop c4a1e0ef3d8c
# Display all rescoures(host's CPU and RAM usages by docker containers)
docker stats
#Gets docker machine in details inlcuding IP address
docker inspect DOCKER_CONTAINER_ID
From host, test installed redis on a docker container
redis-cli -h 172.21.0.3 ping
#Install chromewebdriver
https://developers.supportbee.com/blog/setting-up-cucumber-to-run-with-Chrome-on-Linux/
#Installed bin file did not work? for example I installed `sqlcmd` for MSSQL server into linux and directly can not run the command
# I need to make a shortcut like this(basically puting executable file into /usr/bin solves it):
sudo ln -sfn /opt/mssql-tools/bin/sqlcmd /usr/bin/sqlcmd
#get number of processors of server
grep -c processor /proc/cpuinfo
#git removes local branches which has no remote tracking
git fetch -p && git branch -vv | awk '/: gone]/{print $1}' | xargs git branch -d
# create postgresql user with interactive mode
sudo su - postgres
createuser --interactive --pwprompt
############## DB backup and upload to an ftp user ############################
#!/bin/bash
BACKUP_NAME=database-production-$(date +%d-%m-%Y_%H-%M-%S).gz
BACKUP_PATH=/home/user/path
cd $BACKUP_PATH && pg_dump -Fc -U gaport2 -h localhost gaport_production | gzip > $BACKUP_NAME
HOST=something.hostgator.com
USER=<your_user>
PASSWORD='<your_password>'
ftp -inv $HOST <<EOF
user $USER $PASSWORD
mput $BACKUP_NAME
bye
EOF
cd $BACKUP_PATH && rm $BACKUP_NAME
####################################################################################
# Sweetest warmer temerature for my eye is
gsettings set org.gnome.settings-daemon.plugins.color night-light-temperature 6100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment