Skip to content

Instantly share code, notes, and snippets.

@huynhbaoan
Last active July 29, 2020 09:18
Show Gist options
  • Save huynhbaoan/e86a2ed9a4e10ae619def12c9c9e6b6e to your computer and use it in GitHub Desktop.
Save huynhbaoan/e86a2ed9a4e10ae619def12c9c9e6b6e to your computer and use it in GitHub Desktop.
Some common bash shell syntax for daily use
# Stack size check for HTTPD thread
ps aux | grep "[h]ttpd" | awk '{print $2}' > /tmp/httpd.list
while IFS= read -r line
do
echo "$line"
cat /proc/$line/status
ls /proc/$line/task | xargs -I % bash -c "cat /proc/$line/task/%/status" | grep VmStk
done < /tmp/httpd.list
rm -f /tmp/httpd.list
### Since on all thread of process share everything except stack, we only check VmStk here
### /proc/<pid>/task contains info of thread of process, just like the process itself.
### Check large-size diretory
du -shxc | sort -h
### IF short form
[[ -f $FILE ]] && { echo "$FILE esixt"; cat $FILE; } || { echo "$FILE does not esixt"; retun 1; }
[[ -d $DIR ]] && { echo "$DIR esixt"; ls $DIR; } || { echo "$DIR does not esixt"; retun 1; }
[[ $EUID -ne 0 ]] && { echo "Not root"; return 1 }
[[ gzip -t ]]
### IF long form
gzip -t $FILE
if [[ $? -eq 0 ]]; then
echo "$FILE is gzip."
else
echo "$FILE is NOT gzip"
fi
# Crontab check
for user in $(cut -f1 -d: /etc/passwd); do crontab -u $user -l; done
https://opensource.com/article/17/11/how-use-cron-linux
# Another version, use xargs -I (-t for printing full command, -0 take care of special name in command - but will not work in this case)
netstat -nlptu | awk '{print $7}' | cut -d / -f 1| xargs -t -I % bash -c 'ls -l /proc/%/cwd'
# Find and move all dir to new place
find . -type d -mtime -0 -print0 | xargs -0 mv -v -t /path/to/dest-dir
# Find and delete all empty files with name pattern
find . -name "pattern*" -type f -empty -delete
# Find and extract all .tar.gz files inside child directory
find . -name '*.tar.gz' -execdir tar -xzvf '{}' \;
# Find and upload S3 tar files modified within 14 days
find /path/ -name "*.tar*" -mtime -14 -print0 | xargs -0 -t -I % bash -c 'aws s3 cp % s3://bucketname/path/ --storage-class ONEZONE_IA'
# Alias to find cwd, exe, command of a listening process
function find-proc {
sudo netstat -nlpt | grep $1 | awk '{print $7}' | cut -d / -f 1 | sort | xargs -I % bash -c 'sudo cat /proc/%/cmdline && echo && sudo ls -l /proc/$
}
# Alias to find process using the most swap space
(for Ubuntu 16)
function top-swap {
for file in /proc/*/status ; do awk '/VmSwap|Name|NSpid/{printf $2 " " $3}END{ print ""}' $file; done | sort -k 3 -n -r | more
}
(for CentOS 6)
function top-swap {
for file in /proc/*/status ; do sudo awk '/VmSwap|Name|Tgid/{printf $2 " " $3}END{ print " "}' $file; done | sort -k 3 -n -r | more}
# Alias to find process using the most Memory space (RSS)
(for CentOS 6)
function top-rss {
for file in /proc/*/status ; do sudo awk '/VmRSS|Name|Tgid/{printf $2 " " $3}END{ print " "}' $file; done | sort -k 3 -n -r | more}
# Alias function for fast ssh
function myssh {
ssh an.hb@192.168.$1.$2
}
# Alias to execute a command on remote host via ssh
function remote_command {
echo $3 > ~/.command.sh
exe_command=$(base64 -w0 ~/.command.sh)
ssh -T -n <user>@192.168.$1.$2 "echo $exe_command | base64 -d | sudo bash"
}
# Alias to setup alias on remote machine
function scp-alias {
scp /home/<user>/.bash_aliases <user>@$1:/home/<user>/
}
# Alias to execute alias on remote machine (need scp-alias set up to run)
function remote_alias {
echo "shopt -s expand_aliases ; source /home/<user>/.bash_aliases ; eval $3" > ~$
exe_command=$(base64 -w0 ~/.command.sh)
ssh -T -n <user>@192.168.$1.$2 "echo $exe_command | base64 -d | sudo bash"
}
# Find largest directory
du -hs * / | sort -rh | head -10
du -Sh / | sort -rh | head -10
find /home/ -type f -exec du -Sh {} + | sort -rh | head -n 5
# Find where java source code located from running Java process
sudo netstat -nlpt | grep java | awk '{print $7}' | cut -d / -f 1 | sort | xargs -I % bash -c 'sudo ls -l /proc/%/cwd'
# Find which Java JDK is running
sudo netstat -nlpt | grep java | awk '{print $7}' | cut -d / -f 1 | sort | xargs -I % bash -c 'sudo ls -l /proc/%/exe'
# Get detail Java command-line from running Java process
sudo netstat -nlpt | grep java | awk '{print $7}' | cut -d / -f 1 | sort | xargs -I % bash -c 'sudo cat /proc/%/cmdline'
# Get limits detail applied for a Java running process
sudo netstat -nlpt | grep java | awk '{print $7}' | cut -d / -f 1 | sort | xargs -I % bash -c 'sudo ls -l /proc/%/cwd && sudo cat /proc/%/limits'
# Connection established
netstat -ant | grep EST| awk '{print $5}' | sort | uniq -c | sort -n |more
netstat -an|awk '/tcp/ {print $6}'|sort|uniq -c
# Check ulimit for another user
su <user> --shell /bin/bash --command "ulimit -aS"
su <user> --shell /bin/bash --command "ulimit -aH"
# init.d script template
http://www.tldp.org/HOWTO/HighQuality-Apps-HOWTO/boot.html
https://askubuntu.com/questions/228304/how-do-i-run-a-script-at-start-up
# System stat tools:
https://www.digitalocean.com/community/tutorials/how-to-use-top-netstat-du-other-tools-to-monitor-server-resources
# Bash shell cheat sheet
https://devhints.io/bash
http://mywiki.wooledge.org/BashFAQ
https://github.com/koalaman/shellcheck#user-content-installing
https://www.shellcheck.net/
## Add multiple lines, with variable -> value:
cat >> /path/to/file << EOF
~line
$var
EOF
## Add multiple lines, with variable "as it":
cat >> /path/to/file << 'EOF'
~line
$var (now $var is "as it", not a variable anymore)
EOF
############### Bash shell array literate ########################
#!/bin/bash
## declare an array variable
declare -a array=("one" "two" "three")
# get length of an array
arraylength=${#array[@]}
# use for loop to read all values and indexes
for (( i=1; i<${arraylength}+1; i++ ));
do
echo $i " / " ${arraylength} " : " ${array[$i-1]}
done
#################################################################
# Search and replace with variable value
sed -i "s@search@$replace@g" /path/to/file
# "search" text is replace by $replace value
sed -i 's@search@$replace@g' /path/to/file
# "search" text is replace by "$replace" text
# Single quote prevent variable
# Check ulimit of process
return-limits(){
for process in $@; do
process_pids=`ps -C $process -o pid --no-headers | cut -d " " -f 2`
if [ -z $@ ]; then
echo "[no $process running]"
else
for pid in $process_pids; do
echo "[$process #$pid -- limits]"
cat /proc/$pid/limits
done
fi
done
}
=> return-limits [process]
# For with arguments
for var in "$@"
do
echo "$var"
done
# Send a running job to background and keep it running without terminal opened
Ctrl+Z to stop (pause) the program and get back to the shell.
bg to run it in the background.
disown -h [job-spec]
#where [job-spec] is the job number (like %1 for the first running job; find about your number with the jobs command) so that the job isn't killed when the terminal closes.
# or
kill -20 PID
kill -18 PID
#kill -20 (SIGTSTP) will suspend the process and kill -18 (SIGCONT) will resume the process, in background. So now, closing both your terminals won't stop your process.
# Or, start process disowned from begining
nohup <command> &
# Re-attach disowned process to new terminal
reptyr PID
# Yum list all packages version and install specific version
yum clean all
yum makecache fast
yum --showduplicates list mongodb-org
yum install mongodb-org-3.0.7
# trick to truncate logfile
#!/bin/bash
ls -t server.log.* | tail -n +72 | xargs rm -f
truncate /var/log/kafka/kafka.log -s 100M
### Add DNS server setting to Linux, the simple way ###############################
you can change the nameservers that dnsmasq uses by adding the following lines to /etc/dnsmasq.conf:
server=8.8.8.8
server=8.8.4.4
didn't have a /etc/dnsmasq.conf file though, since it's installed by the dnsmasq package, but Ubuntu only comes with dnsmasq-base. I ran sudo apt-get install dnsmasq, then edited /etc/dnsmasq.conf, then sudo service dnsmasq restart and sudo service network-manager restart.
ran sudo tail -n 200 /var/log/syslog to check my syslog and verify that dnsmasq was using the nameservers I specified:
Oct 21 23:00:54 mylaptop dnsmasq[8611]: using nameserver 8.8.8.8#53
Oct 21 23:00:54 mylaptop dnsmasq[8611]: using nameserver 8.8.4.4#53
#############################################################################################
https://superuser.com/questions/747884/how-to-write-a-script-that-accepts-input-from-a-file-or-from-stdin
https://eklitzke.org/how-to-nice-a-bash-function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment