Snippet Bash

With snippet Bash, you can free your mind

  1. Kill process

               .---------- PID
    tomcat   17782  0.6  8.0 3691108 1247720 ?     Sl   Jul08  11:38 /usr/lib/jvm/jre/bin/java
    kill [signal] PID
    kill -15 PID
    kill -9 PID
    kill -SIGTERM PID
    kill [options] -SIGTERM PID

    SIGHUP (1) - Hangup detected on controlling terminal or death of controlling process. Use SIGHUP to reload configuration files and open/close log files.

    SIGKILL (9) - Kill signal. Use SIGKILL as a last resort to kill process. This will not save data or cleaning kill the process.

    SIGTERM (15) - Termination signal. This is the default and safest way to kill process.

  2. Kill background jobs

    ./script &
    [1] 35341
    jobs -l
    [1]+ 35341    Running          ./script &
    kill $! # $! expands to the PID of the last process executed in the background
    kill %1
    kill %% # most recent background job
  3. Kill all process by name/pattern

    pkill -f process_name
  4. Show why linux kill process

    dmesg | egrep -i -B100 'killed process'
  5. Check reboot required

    # check file /var/run/reboot-required exists or not
    ls /var/run/reboot-required
    file /var/run/reboot-required
    stat /var/run/reboot-required
  6. Find last reboot time

    # check who is logged on
    who –b
    # last logged in user and system last reboot time (/var/log/wtmp)
    last reboot | head -1
    # systems last shutdown time
    last -x|grep shutdown | head -1
    # -x: display system shutdown info and run level change
  7. Finding the size of a directory

    du -sh
    du -sh /*
    du -sh /tmp /var
    du -sh s*
    # -s summary of the directory size
    # -h human readable format
    # -a displays the filenames along with the directory names in the output
    # -S excluding the size of the subdirectories that exist within that directory
    # --exculde=mp3 exclude all the files having the given pattern present in their filenames
    # /* simply expands to all directories (and files) in /
  8. Finding the disk free space

    df -h
    # -h human readable format
  9. Use sar -r to monitoring Linux memory

    # install
    sudo yum install sysstat
    # configure
    sudo vi /etc/cron.d/sysstat
    # monitor memory usage
    sar -r
    sar -r X Y # display every X second(s) for Y time(s)
    sar -r -f /var/log/sa/saXX # display daily summary report, XX is the day of the month
  10. Change modification time of linux files

    # touch [OPTION]... FILE...
    # -a, change the access time only
    # -c, if the file does not exist, do not create it
    # -d, update the access and modification times
    # -m, change the modification time only
    # -r, use the access and modification times of file
    # -t, creates a file using a specified time
    touch -d "2 hours ago" filename
    touch -d "5 days ago" filename
    touch -t 201412210923.45 filename
  11. Remove file first line

    tail -n +2 oldfile > newfile
    sed '1d' oldfile > newfile
    sed -i '1d' file
  12. Delete all user mail

    # email message file location : /var/spool/mail/username
    > /var/spool/mail/ec2-user
    sudo cp /dev/null /var/spool/mail/root
  13. Best way to delete /var/spool/clientmqueue

    sudo service sendmail stop
    sudo mv /var/spool/clientmqueue /var/spool/clientmqueue-todelete
    sudo mkdir /var/spool/clientmqueue
    sudo chown --reference=/var/spool/clientmqueue-todelete /var/spool/clientmqueue
    sudo chmod --reference=/var/spool/clientmqueue-todelete /var/spool/clientmqueue
    sudo service sendmail start
    sudo rm -rf /var/spool/clientmqueue-todelete
  14. Copy without permissions

    sudo cp --no-preserve=mode /var/log/messages messages
  15. Copy website by wget

    wget --wait=20 --limit-rate=20K -r -p -U Mozilla --no-parent
    # -w, --wait=seconds: pause seconds between retrievals, this makes sure you are not manually added to a blacklist
    # --limit-rate=amount: limit the download speed to amount bytes per second
    # -r, --recursive: download sites recursive
    # -p, --page-requisites: download all the files that are necessary to properly display a given HTML page
    # -U, --user-agent: identify as agent-string to the HTTP server
    # -np, --no-parent : do not ever ascend to the parent directory when retrieving recursively
  16. Keep a background process alive after closing the terminal

    nohup sh &
    nohup sh > nohup.out 2> nohup.err < /dev/null &

    Without nohup.out created

    nohup sh >/dev/null 2>&1 # doesn't create nohup.out
    nohup sh >/dev/null 2>&1 & # runs in background, doesn't create nohup.out
    nohup sh /dev/null 2>&1 & # completely detached from terminal
    screen -A -d -m -S name_screen sh &
    -A Adapt the sizes of all windows to the size of the current terminal.
    By default, screen tries to restore its old window sizes when attaching to resizable terminals
    -d -m Start screen in "detached" mode. This creates a new session but doesn't attach to it
    -S <sessionname> Specify a meaningful name for the session. This name identifies the session for "screen -list" and "screen -r" actions
    See more : screen command
  17. Send the already running process into the background process and keep alive after closing the terminal

    Using the Job Control

    COMMAND_1 # run 1st command
    # stop (pause) the program and get back to the shell
    Ctrl + Z
    COMMAND_2 # run 2nd command
    Ctrl + Z
    jobs # show jobs
    # bg [jobspec]
    # place jobspec into the background, as if it had been started with `&`
    # if jobspec is not supplied, the current job is used
    bg %1
    # disown -h [jobspec]
    # put that command into its own process so that you can exit the terminal while the command still run
    # so that the process isn't killed when the terminal close
    # if jobspec is not supplied, the current job is used
    # -h: mark each jobspec so that SIGHUP is not sent to the job if the shell receives a SIGHUP
    # SIGHUP (Hangup) signal is used by your system on controlling terminal or death of controlling process
    # use SIGHUP to reload configuration files and open/close log files
    # if you logout from your terminal all running jobs will be terminated
    disown -h %1
    bg %2
    disown -h %2
    exit # exit the terminal

    This process equivalent with nohup COMMAND

  18. Setup oh-my-zsh with autosuggestions and fzf on Ubuntu

    # install zsh
    sudo apt install zsh
    # config zsh as default bash
    chsh -s $(which zsh)
    # re-login
    # install oh-my-zsh
    sh -c "$(wget -O-"
    # install fonts-powerline which used for "agnoster" theme
    sudo apt install fonts-powerline
    # change theme by update ZSH_THEME
    vi ~/.zshrc # ZSH_THEME="agnoster"
    # download plugin zsh-autosuggestions
    git clone ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
    # download and install fzf
    git clone --depth 1 ~/.fzf
    # add more plugins
    vi ~/.zshrc # plugins=(git fzf zsh-autosuggestions)
    # re-open terminal to see what's changed


Popular posts from this blog