Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Sunday, August 26, 2018

vi comment and uncomment multiple lines

Comment multiple lines
  1. Press ESC
  2. Go to the starting line, press ctrl+v to enable visual block mode
  3. Use down arrow or k key to select multiple lines
  4. Press shift+I to enable insert mode
  5. Press # to add comment to first line, then press ESC, then wait for 1 second to get # added to all lines
Another option is:
  1. Press ESC
  2. Go to the starting line, press ctrl+v to enable visual block mode
  3. Use down arrow or k key to select multiple lines
  4. Press shift+ : to do search and replace ('<,'> will be automatically added)
  5. Type  s/^/#/ to add # to comment out selected lines
Uncomment multiple lines
  1. Press ctrl+v to enable visual block mode
  2. Move down to select lines
  3. Press x to uncomment selected lines

Monday, May 8, 2017

Copy folder from local to remote or vice versa

Option 1: scp -r
To copy all from Local Location to Remote Location (Upload)
scp -r /path/to/file username@hostname:/path/to/destination

To copy all from Remote Location to Local Location (Download)
scp -r username@hostname:/path/to/file /path/to/destination

Custom Port where xxxx is custom port number
scp -r -p xxxx username@hostname:/path/to/file /path/to/destination

Copy on current directory from Remote to Local
scp -r username@hostname:/path/to/file .


Option 2: zip to file then scp
Better to first compress catalog on remote server:
tar czfP backup.tar.gz /path/to/catalog

Secondly, download from remote:
scp user@your.server.example.com:/path/to/backup.tar.gz .

At the end, extract the files:
tar -xzvf backup.tar.gz

Option 3: sftp
$ sftp user_remote@remote_ip
sftp> cd /path/to/remoteDir
sftp> get -r remoteDir

Wednesday, March 18, 2015

Shell script error: bad interpreter: No such file or directory

It is highly likely that you created this file with a windows editor, which will place a <cr><lf> at the end of each line. This is the standard under dos / windows. However, under Unix / linux, the standard is to just put a <lf> at the end of the line.

Linux is now looking for a file called /bin/bash<cr> to interpret the file, where <cr> is a carriage return character, which is a valid file character under linux. Such a file doesn't exist. Hence the error.

http://stackoverflow.com/questions/2841593/bash-script-bad-interpreter

Sublime Text 2 -> View -> Line Endings -> Unix

Thursday, September 23, 2010

cannot open '/etc/mail/local-host-names': World writable directory

What is World-writable?
A world-writable file or directory is one that anyone can alter. For directories this means that files can be created or deleted within it by anyone. Being world-writable is almost always a security risk.

Problem:
Get below error when try to start sendmail (/etc/init.d/sendmail start)
/etc/mail/sendmail.cf: line 94: fileclass: cannot open '/etc/mail/local-host-names': World writable directory

Solution:
  1. Grant w to /etc, /etc/mail, /etc/mail/* => not work
  2. Manually edit file sendmail.cf
replace line Fw/etc/mail/local-host-names with line
Fw-o /etc/mail/local-host-names

identically (if you got the same error for trusted-users)
replace line Ft/etc/mail/trusted-users with line
Ft-o /etc/mail/trusted-users

Shell script to monitor disk space

Motivation:
Space full caused db transaction hang issue last week, and this week it caused javax.jms.JMSException: No space left on device, herein ActiveMQ cannot write message to its queue. I decide to write a shell script to watch the disk space.

Steps:
  1. Google for similar script and info
  2. Write the shell script 
  3. Start sendmail for email alert
  4. Add script to cronjob for auto monitoring
Commands:
  1. /etc/init.d/sendmail => Usage: /etc/init.d/sendmail {start|stop|restart|condrestart|status}
  2. /etc/init.d/crond => Usage: /etc/init.d/crond {start|stop|status|reload|restart|condrestart}
Two options to add script to cronjob:
  1. crontab -e
  2. Put shell script to /etc/cron.* folder (cron.daily/   cron.hourly/  cron.monthly/ cron.weekly/)
Script:
# http://www.cyberciti.biz/tips/shell-script-to-watch-the-disk-space.html
# set admin email for alert

ADMIN_EMAIL="admin@sample.com"
# set alert level
ALERT_LEVEL=90
# set log folder to get deleted
LOG_FOLDER=/opt/logs/*

df -HP | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;
do
  echo $output
  used=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 )
  partition=$(echo $output | awk '{ print $2 }' )
  if [ $used -ge $ALERT_LEVEL ]; then
    echo "Running out of space \"$partition ($used%)\" on $(hostname) as on $(date)" |
    mail -s "Alert: Running out of disk space - $used% used" $ADMIN_EMAIL
              echo "Purging log files ..."
              find $LOG_FOLDER -mtime +2 -exec rm -f {} \;
              echo "DONE"   

  fi
done

Why use df - HP?

linux01:root > df -H
Filesystem             Size   Used  Avail Use% Mounted on
/dev/sda2              8.0G   2.7G   5.0G  35% /
192.168.253.131:/u001/ReleaseArchive/
                       289G   203G    78G  73% /jdrive

linux01:root > df -H -t ext3
Filesystem             Size   Used  Avail Use% Mounted on
/dev/sda2              8.0G   2.7G   5.0G  35% /

linux01:root > df -HP
Filesystem             Size   Used  Avail Use% Mounted on
/dev/sda2              8.0G   2.7G   5.0G  35% /
192.168.253.131:/u001/ReleaseArchive/   289G   203G    78G  73% /jdrive

Reference:
http://www.cyberciti.biz/tips/shell-script-to-watch-the-disk-space.html