Posts

Showing posts from October, 2018

netstat tulpena find process id based on Inode in netstat

awsc1@ubuntu:~/dockerexamples/docker18.06/Dockerfile$ netstat -tulpena Proto Recv-Q Send-Q Local Address           Foreign Address         State       User       Inode      PID/Program name     tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      101        27118      -                           tcp6       0      0 :::4000                 :::*                    LISTEN      0          51306      -                    awsc1@ubuntu:~/dockerexamples/docker18.06/Dockerfile$ sudo find /proc -lname '* 51306 *' 2>/dev/null /proc/2469/task/2469/fd/4 /proc/2469/task/2470/fd/4 /proc/2469/task/2471/fd/4 /proc/2469/task/2472/fd/4 /proc/2469/task/2473/fd/4 /proc/2469/fd/4

bash script header best practices

#!/bin/bash ######################## # Program Nmae: blabla   Author: John Miller # Description: The purpose of the script #------ # Change log: # Date  Programmer Description # 21.05.2018 Somebody    Initial implementation ########################

Cloudcommit | aws codecommit list-repositories | aws codecommit create-repository | git clone

awsc1@ubuntu:~/awsauto$ aws codecommit list-repositories {     "repositories": [         {             "repositoryName": "HelloWorld",             "repositoryId": "8d7cffad-dc17-41dc-a605-87082e603fb6"         }     ] } =========== cat ~/.ssh/config Host git-codecommit.us-east-1.amazonaws.com User APKXXXXX7MVBXXXX5GALA IdentityFile ~/.ssh/id_rsa_awsccXXXt aws codecommit create-repository --repository-name HelloW --repository-description "Test" ssh-keygen aws iam upload-ssh-public-key --user-name cliuser  --ssh-public-key-body "public key" ssh git-codecommit.us-east-1.amazonaws.com ... You have successfully authenticated over SSH. You can use Git to interact with AWS CodeCommit.  git clone ssh://git-codecommit.us-east-1.amazonaws.com/v1/repos/HelloWorld

debug scripts tip | PS4='+ ${BASH_SOURCE} : ${LINENO} :${FUNCNAME[0]}() '

root@ubuntu:/home/awsc1/linuxlessons/linuxscripts# bash -x debugPS4tip.sh  ls + PS4='+ ${BASH_SOURCE} : ${LINENO} :${FUNCNAME[0]}() ' + debugPS4tip.sh : 3 :main() TEST_VAR=test + debugPS4tip.sh : 4 :main() echo test test + debugPS4tip.sh : 10 :main() debug ls + debugPS4tip.sh : 7 :debug() echo 'Executing: ls' Executing: ls + debugPS4tip.sh : 8 :debug() ls associative_array  check_multiple_proc.sh  EOF.sh     paralleping.sh   showecho.sh     test.file calc.sh    debugPS4tip.sh    funechoerror.sh  README.md      sleeppid.sh     trap.sh capacity.sh    dollar_sc.sh    fu.sh     shellscripts.sh  systemdtarget.service root@ubuntu:/home/awsc1/linuxlessons/linuxscripts#

trap.sh | trap (CTRL-C) is signal 2 (SIGINT)

 cat trap.sh #!/bin/usr/env bash trap 'increment' 2 increment() {   echo "Caught SIGINT ..."   X=`expr ${X} + 500`   if [ "${X}" -gt "2000" ]   then     echo "Okay, I'll quit ..."     exit 1   fi } ### main script X=0 while : do   echo "X=$X"   X=`expr ${X} + 1`   sleep 1 done Here is a table of some of the common interrupts: Number SIG Meaning 0 0 On exit from shell 1 SIGHUP Clean tidyup 2 SIGINT Interrupt 3 SIGQUIT Quit 6 SIGABRT Abort 9 SIGKILL Die Now (cannot be trap'ped) 14 SIGALRM Alarm Clock 15 SIGTERM Terminate

sleepid.sh exit 1 read -p $@ $0

cat sleeppid.sh #!/bin/bash echo Hello echo "Staring script $0" USER=$2 fruits=$@ HOST=$1 T=test.file if [ -e $T ] then         echo $T exists else         echo file not exists fi read -p "Enter the country" country for COLOR in "${fruits}" #start from 2nd element do         echo "Color: $COLOR" done ping -c 1 $HOST if [ "$?" -ne "0" ] then         echo $HOST unreachable         exit 1 else         echo $HOST is reachable fi echo "User $USER, country $country" echo PID is $$ sleep 3 exit 0

ways to check linux distribution | /etc/*rel* | "dmesg | head -1" | cat /etc/issue

1) root@ubuntu:/home/awsc1/linuxlessons/linuxscripts# dmesg | head -1 [    0.000000] Linux version 4.15.0-36-generic (buildd@lgw01-amd64-031) (gcc version 7.3.0 (Ubuntu 7.3.0-16ubuntu3)) #39-Ubuntu SMP Mon Sep 24 16:19:09 UTC 2018 (Ubuntu 4.15.0-36.39-generic 4.15.18) root@ubuntu:/home/awsc1/linuxlessons/linuxscripts# 2)  root@ubuntu:/home/awsc1/linuxlessons/linuxscripts# ll /etc/*rel* -rw-r--r-- 1 root root 105 Jul 23 12:40 /etc/lsb-release lrwxrwxrwx 1 root root  21 Aug 20 06:44 /etc/os-release -> ../usr/lib/os-release 3) cat /etc/issue Ubuntu 18.04.1 LTS \n \l

pid location /var/run /var/log

The location of the pid file should be configurable. /var/run is standard for pid files, the same as /var/log is standard for logs.

shell scripting

[ condition to test ] [ -e /etc/passwd ] File operators -d FILE - true if file is directory -e FILE - true if file exists -f FILE - true if file exists and is a regular file -r FILE - true if file is readable by you -s FILE - true if file exists and is not empty -w FILE - true if file is writable by you -x FILE - true if file is executable by you String operators -z STRING - true if string is empty -n STRING - true if string is not empty STR1=STR2 - true if strings are equal Arithmetic operators arg1 -eq arg2 - true if arg1 = arg2 arg1 -ne arg2 - true if arg1 <> arg2 -lt less than -le less than or equal -gt greater than -ge greater or equal ----------------------------- if a script doesn't contain a shebang the commands are executed using your shell ------------------------------- #!/path/to/interpreter VARIABLE_NAME="Value" $VARIABLE_NAME ${VARIABLE_NAME} VARIABLE_NAME=$(command) ----------------------------- if [condition-is-true]

display PID and Thread ID details | ps -Tfp 82199

root@ubuntu:/home/awsc1/linuxlessons/linuxscripts# bash sleeppid.sh & [1] 82199 root@ubuntu:/home/awsc1/linuxlessons/linuxscripts# Hello PID is 82199 (reverse-i-search)`ps': ^C -fp 82157 root@ubuntu:/home/awsc1/linuxlessons/linuxscripts# ps -Tfp 82199 UID         PID   SPID   PPID  C STIME TTY          TIME CMD root      82199  82199  81460  0 12:46 pts/0    00:00:00 bash sleeppid.sh