RSS
 

Archive for the ‘Linux’ Category

Customize your Linux Terminal (Bash)

05 Dec 2015

So the terminal is something that a program uses all the time but the default terminals on Unix distros looks very plain and boring in my opinion.

While I really like the Kali Linux terminal style I use my Xubuntu distro much more often, its my personal favourite. So I decided to customize it and make it look more interesting.

Before

Original Terminal Xubuntu

After

Customized Terminal

The customized terminal when opened prints:

How to get the above terminal style

To replicate the colours and style, simply copy the preferences from the image below. These settings should be pretty similar on all graphics based Linux distributions.
Xubuntu Terminal Style Preferences

You can download the Aperture Laboratories background image by Download Aperture Laboratories Terminal Wallpaper.

You will have to modify your bash preference files to get the terminal welcome message printed at the top of the terminal when you spawn a new shell (bash prompt).

Navigate to your home directory and add the following to the end of your .bash_profile  file:

Next, append the following to .bashrc file:

Now restart your terminal or spawn a new shell to see the results!

Note:  The function which determines the CPU usage is a little slow so you may notice a slight delay (~150ms) every time you spawn a new shell. If this is a massive problem for you, simply remove the CPU usage line from the terminal welcome message.

 
No Comments

Posted in Linux

 

How to get overall CPU utilization from the bash command line (Linux)

27 Nov 2015

For a little project I worked on I needed to get the CPU utilization as a percentage.
I Google’d the issue and searched for “cpu utilization bash“.

To my surprise there were no elegant solutions. Most just failed to work (on my machine anyway) while others were very inaccurate. For example, some would show the same number over and over again. Some would change the numbers but were not related to CPU utilization at all. Others would sample the CPU utilization over 1 second which is a good idea but wouldn’t work well in my script in my particular case (as my script needed an instant result).

So I came up with the following instant and accurate solution:

This outputs a floating point number between 0.0 and 100.0.
Essentially, we look at the current output of the top using  top -n 1  and then tally up all the numbers for all processes in the CPU usage column. Then we simply divide this number by nproc  (number of processor units available) to get the overall CPU utilization. In this case python handled the floating point division for us (as bash cannot do floating point arithmetic).

Pro tip

You can use the  stress  command (http://linux.die.net/man/1/stress) to stress test your system in order to check the above solution. You should notice that the script will output a number close to 100 if you stress all processor cores.

 
1 Comment

Posted in Linux