Uly.me

cloud engineer

  • Home
  • About
  • Archives
Home/Archives for Linux

March 3, 2021

Display Number of String Characters

Here’s a simple little script that displays the number of characters in a string.

#!/bin/bash
if [[ $# -eq 0 ]] ; then echo 'Missing arguments'; exit 0; fi
var=$1
size=${#var}
echo $size

#!/bin/bash if [[ $# -eq 0 ]] ; then echo 'Missing arguments'; exit 0; fi var=$1 size=${#var} echo $size

Command:

./length.sh abcdefghijklmnopqrstuvwxyz

./length.sh abcdefghijklmnopqrstuvwxyz

Output:

26

26

February 22, 2021

Logging Commands

I decided to log my commands for good record keeping. Here’s my script.

#!/bin/bash
if [[ $# -eq 0 ]] ; then echo 'No arguments. Format: ./go.sh script.sh'; exit 0; fi
log=log.txt
echo '-------------------------------' >> $log
date >> $log
echo '---' >> $log
cat $1 >> $log
echo '---' >> $log
/bin/bash $1 2>&1 | tee -a $log

#!/bin/bash if [[ $# -eq 0 ]] ; then echo 'No arguments. Format: ./go.sh script.sh'; exit 0; fi log=log.txt echo '-------------------------------' >> $log date >> $log echo '---' >> $log cat $1 >> $log echo '---' >> $log /bin/bash $1 2>&1 | tee -a $log

To run.

./go.sh script.sh

./go.sh script.sh

Line by line explanation.

  1. Checks for script to run. Exits if there’s no argument.
  2. Sets the log file.
  3. Prints dashes.
  4. Prints the date.
  5. Print 3 dashes.
  6. Prints the commands to the log file.
  7. Print 3 dashes.
  8. Run the command. Send error and standard out to log file.

February 16, 2021

Create A Swap File

How to create a swap file.

A 2GB swap file.

dd if=/dev/zero of=/swapfile bs=1k count=2048k

dd if=/dev/zero of=/swapfile bs=1k count=2048k

Activate.

mkswap /swapfile
chmod 0600 /swapfile
systemctl daemon-reload
swapon /swapfile

mkswap /swapfile chmod 0600 /swapfile systemctl daemon-reload swapon /swapfile

To make swap permanent, add to /etc/fstab.

/swapfile  swap   swap    defaults   0 0

/swapfile swap swap defaults 0 0

Check if swap is working.

cat /proc/swaps
free -h

cat /proc/swaps free -h

February 9, 2021

Reinstall Kernel

How to reinstall the latest kernel and rebuild initramfs.

Find the latest kernel.

rpm -qa kernel
kernel-3.10.0-1160.6.1.el7.x86_64
kernel-3.10.0-1160.11.1.el7.x86_64

rpm -qa kernel kernel-3.10.0-1160.6.1.el7.x86_64 kernel-3.10.0-1160.11.1.el7.x86_64

Remove the kernel first, then reinstall.

yum remove kernel-3.10.0-1160.6.1.el7.x86_64
yum install kernel-3.10.0-1160.6.1.el7.x86_64

yum remove kernel-3.10.0-1160.6.1.el7.x86_64 yum install kernel-3.10.0-1160.6.1.el7.x86_64

February 4, 2021

Monitor Disk Performance

Here are a few utilities that monitor disk I/O performance.

iostat -d 5
pidstat -dl 20
iotop --only

iostat -d 5 pidstat -dl 20 iotop --only

The alternative is atop, which you can set to run every 10 seconds for 3 hours. The output can be viewed later.

atop -a -w logfile.atop 10 10800 &

atop -a -w logfile.atop 10 10800 &

Use atop to read the log file.

atop -r /path/to/logfile.atop

atop -r /path/to/logfile.atop

  • 1
  • 2
  • 3
  • …
  • 68
  • Next Page »
  • Cloud
  • Linux
  • Git

Copyright © 2012–2021