Alternatives to Top

Linux Bash

Looking for alternatives to your usual top command? Here are some options.

htop

htop is based on ncurses and is compatible with most Linux (and Unix) systems. It’s also in most the official repos for most distros.

Features: - Mouse clicks (due to ncurses) - Defaults to multi-CPU view - Memory shown in GB

htop

Website: http://hisham.hm/htop/

vtop

vtop takes more of a graphical approach to top, while concentrating more on simplicity. It displays CPU and Memory usage live charts, as well as a running process list. It runs on Node.js and can be easily installed with npm install -g vtop.

Features: - Mouse clicks - Themes - Live chart (CPU, memory) - Process list

vtop

Website: https://github.com/MrRio/vtop

gtop

gtop takes the graphical interface of vtop to another level. Just like vtop, it also displays a live chart of both CPU and memory, however gtop adds a network live chart to the list, and pie charts for both memory and swap, as well as storage usage.

gtop also runs on Node.js, and can easily be installed with npm install gtop -g.

Features: - Live chart (CPU, memory/swap, network) - Pie chart (memory, swap, disk usage) - Process list

gtop

Website: https://github.com/aksakalli/gtop

s-tui

While s-tui is not a process monitoring utility, it displays great information about your process status. It’s python based and uses little resource. You can easily install it with sudo pip install s-tui.

Features: - Processor info - Live charts (CPU freq, utilization, temperature and power usage) - Built-in CPU stress testing

Website: https://amanusk.github.io/s-tui/

Bash - Command Substitution, Subshell and Codeblock

Bash Linux

Bash - Command Substitution, Subshell and Codeblock

Quick and simple differences between using $(..), (..) and {..;}.

  • $(command) or `command` - Command substitution (the output of a command replaces the command name) and is executed in a subshell. Please give preference to $(..) for better readability
  • ( command ) - Command list is executed in a subshell
  • { command ;} - Command list is executed in the current shell

Executing code

$ ( echo 123 )
123

$ $(echo 123) # this executes the output of the command
123: command not found

$ { echo 123 ;}
123

Fiding subshell level with built-in BASH_SUBSHELL variable

$ ( echo $BASH_SUBSHELL )
1
$ ( ( echo $BASH_SUBSHELL ) )
2

$ $($BASH_SUBSHELL)
1: command not found

$ { echo $BASH_SUBSHELL ;}
0

Passing variables to subshell

$ var1=1

$ ( echo $var1 )
1

$ $(echo $var1)
1: command not found

$ { echo $var1 ;}
1

Changing and getting variables values in subshell

$ var=1

$ ( var=2 ) ; echo $var
1

$ $(var=2) ; echo $var
1

# Because we did not create a subshell,
# variable is available to original shell
$ { var=2 ;} ; echo $var
2

Other usages

Example 1:

Multiple commands and multiple lines. This can be used with (..) and {..}.

$ { echo 1 ; echo 2 ; echo 3; }
1
2
3

$ {
> echo 1
> echo 2
> echo 3;
> }
1
2
3

Example 2:

Changing the environment for subshell with separate environment. Here, set -u is only set in the subshell, so only the first echo will error out.

unset var1 var2
(
  set -u
  echo $var2
)
echo $var1

Redirecting Bash Xtrace to a Separate Log

Linux Bash

So you have a Bash script that you want to troubleshoot, but you want to send stdout to a file, and stderr to another. Here’s a solution.

For example, I like to use Bash’s color to display failure or success on checks, and echo’s removal of new lines (echo -e "Wait for it...\c ") to wait for checks. For example, the screenshot below shows a script that check each step and report back.

terminal

Errors are displayed in red, and success in green, and this same output is sent to the stdout and to a log file as well.

Now I want to get error messages in a separate log file, so I can go back to my original log and match the time where it failed, with additional verbose from Bash’s xtrace.

The solution is very simple… I can just add the lines below to the top of my script:

# Debugging
debug_log="${HOME}/bin/log/script.debug"
exec 2>>${debug_log}
set -x

The first line setups my debug log, the second redirects stderr to my debug log, and the third enables xtrace (same as bash -x [script]).

Note that doing this will disable any errors from being show in stdout.

How to Record and Share a Terminal Session

Linux Shell

A while ago I had shared instructions on “How to Share a Terminal Window Online”. This is great for live support.

Today I’m sharing instructions on how to record your teminal session, and not only share it, but also allow users to copy and paste text from the playback video.

The installation could not be easier. And to show how great this utility is, I’m showing the instructions using a “video” recorderd with “showterm”.

Now go ahead and try copying text from the video. Isn’t that great!?


Reference:

http://showterm.io/

https://github.com/ConradIrwin/showterm

How to Create a Prompt With Timeout in Bash

Linux Bash

Here’s a quick function that will display a prompt with timeout in a bash script:

_myCountdownFunction () {
echo -e "Hit \"Ctrl+c\" to quit or \"Enter\" to continue...    \c"
cnt=5
while (( cnt >= 0 )) ; do
  if (( cnt < 9 )) ; then
    echo -e "\b\b${cnt}s\c"
  elif (( cnt == 9 )) ; then
    echo -e "\b\b\b ${cnt}s\c"
  elif (( cnt <= 99 )) ; then
    echo -e "\b\b\b\b ${cnt}s\c"
  elif (( cnt < 999 )) ; then
    echo -e "\b\b\b\b${cnt}s\c"
  fi
  read -t 1 my_reply
  (( $? == 0 )) && exit 1
  let cnt-=1
done
}

The user will see the following message on his terminal with the seconds counting down in place:

Hit "Ctrl+c" to quit or "Enter" to continue... 5s

At the end of the specified time in ‘cnt’, the script (where the function would be) will continue executing, and if the user hits “Ctrl+c” before that, it will exit (both script and function).

The function supports up to 999s (which should be enough).

code with