Friday, September 4, 2015

How to count the number of threads in a process on Linux

Question: I have an application running, which forks a number of threads at run-time. I want to know how many threads are actively running in the program. What is the easiest way to check the thread count of a process on Linux? If you want to see the number of threads per process in Linux environments, there are several ways to do it.

Method One: /proc

The proc pseudo filesystem, which resides in /proc directory, is the easiest way to see the thread count of any active process. The /proc directory exports in the form of readable text files a wealth of information related to existing processes and system hardware such as CPU, interrupts, memory, disk, etc.
$ cat /proc//status
The above command will show detailed information about the process with , which includes process state (e.g., sleeping, running), parent PID, UID, GID, the number of file descriptors used, and the number of context switches. The output also indicates the total number of threads created in a process as follows.
For example, to check the thread count of a process with PID 20571:
$ cat /proc/20571/status

The output indicates that the process has 28 threads in it.
Alternatively, you could simply count the number of directories found in /proc//task, as shown below.
$ ls /proc//task | wc
This is because, for every thread created within a process, there is a corresponding directory created in /proc//task, named with its thread ID. Thus the total number of directories in /proc//task represents the number of threads in the process.

Method Two: ps

If you are an avid user of the versatile ps command, this command can also show you individual threads of a process (with "H" option). The following command will print the thread count of a process. The "h" option is needed to hide the header in the top output.
$ ps hH p | wc -l
If you want to monitor the hardware resources (CPU & memory) consumed by different threads of a process, refer to this tutorial.

Top 4 open source command-line email clients

Like it or not, email isn't dead yet. And for Linux power users who live and die by the command line, leaving the shell to use a traditional desktop or web based email client just doesn't cut it. After all, if there's one thing that the command line excels at, it's letting you process files, and especially text, with uninterrupted efficiency.
Fortunately, there are a number of great command-line email clients, many with a devoted following of users who can help you get started and answer any questions you might have along the way. But fair warning: once you've mastered one of these clients, you may find it hard to go back to your old GUI-based solution!
To install any of these four clients is pretty easy; most are available in standard repositories for major Linux distributions, and can be installed with a normal package manager. You may also have luck finding and running them on other operating systems as well, although I haven't tried it and can't speak to the experience.


Many terminal enthusiasts may already have heard of or even be familiar with Mutt and Alpine, which have both been on the scene for many years. Let's first take a look at Mutt.
Mutt supports many of the features you've come to expect from any email system: message threading, color coding, availability in a number of languages, and lots of configuration options. It supports POP3 and IMAP, the two most common email transfer protocols, and multiple mailbox formats. Having first been released in 1995, Mutt still has an active development community, but in recent years, new releases have focused on bug fixes and security updates rather than new features. That's okay for many Mutt users, though, who are comfortable with the interface and adhere to the project's slogan: "All mail clients suck. This one just sucks less."


Alpine is the other well-known client for terminal email, developed at the University of Washington and designed to be an open source, Unicode-friendly alternative to Pine, also originally from UW.
Designed to be friendly to beginners, but also chocked full of features for advanced users, Alpine also supports a multitude of protocols—IMAP, LDAP, NNTP, POP, SMTP, etc.—as well as different mailbox formats. Alpine is packaged with Pico, a simple text editing utility that many use as a standalone tool, but it also should work with your text editor of choice: vi, Emacs, etc.
While Alpine is still infrequently updated, there is also a fork, re-alpine, which was created to allow a different set of maintainers to continue the project's development.
Alpine features contextual help on the screen, which some users may prefer to breaking out the manual with Mutt, but both are well documented. Between Mutt and Alpine, users may want to try both and let personal preference guide their decision, or they may wish to check out a couple of the newer options below.


Sup is the first of two of what can be called "high volume email clients" on our list. Described as a "console-based email client for people with a lot of email," Sup's goal is to provide an interface to email with a hierarchical design and to allow tagging of threads for easier organization.
Written in Ruby, Sup provides exceptionally fast searching, manages your contact list automatically, and allows for custom extensions. For people who are used to Gmail as a webmail interface, these features will seem familiar, and Sup might be seen as a more modern approach to email on the command line.


"Sup? Notmuch." Notmuch was written as a response to Sup, originally starting out as a speed-focused rewrite of some portions of Sup to enhance performance. Eventually, the project grew in scope and is now a stand-alone email client.
Notmuch is also a fairly trim program. It doesn't actually send or receive email messages on its own, and the code which enables Notmuch's super-fast searching is actually designed as a separate library which the program can call. But its modular nature enables you to pick your favorite tools for composing, sending, and receiving, and instead focuses on doing one task and doing it well—efficient browsing and management of your email.

This list isn’t by any means comprehensive; there are a lot more email clients out there which might be an even better fit for you. What’s your favorite? Did we leave one out that you want to share about? Let us know in the comments below!

Linux/UNIX: Bash Read a File Line By Line

 How do I read a file line by line under a Linux or UNIX-like system using KSH or BASH shell?

You can use bash loop to read file line by line on a Linux, OSX, *BSD, or Unix-like system.

Syntax to read file line by line on a Bash Unix & Linux shell:

Tutorial details
DifficultyEasy (rss)
Root privilegesNo
Estimated completion time10m
  1. The syntax is as follows for bash, ksh, zsh, and all other shells -
  2. while read -r line; do COMMAND; done < input.file
  3. The -r option passed to red command prevents backslash escapes from being interpreted.
  4. Add IFS= option before read command to prevent leading/trailing whitespace from being trimmed -
  5. while IFS= read -r line; do COMMAND_on $line; done < input.file
Here is more human readable syntax for you:
while IFS= read -r var
  echo "$var"
done < "$input"


Here are some examples:
while IFS= read line
        # display $line or do somthing with $line
	echo "$line"
done <"$file"
The same example using bash shell:
while IFS= read -r line
        # display $line or do somthing with $line
	printf '%s\n' "$line"
done <"$file"
You can also read field wise:
while IFS=: read -r f1 f2 f3 f4 f5 f6 f7
        # display fields using f1, f2,..,f7
        printf 'Username: %s, Shell: %s, Home Dir: %s\n' "$f1" "$f7" "$f6"
done <"$file"
Sample outputs:
Fig.01: Bash shell scripting-  read file line by line demo outputs
Fig.01: Bash shell scripting- read file line by line demo outputs

Bash Scripting: Read text file line-by-line to create pdf files

My input file is as follows (faq.txt):
4||Mysql User Creation: Setting Up a New MySQL User Account
4096||What is UNIX / Linux Korn Shell?
4101||What Is POSIX Shell?
17267||Linux: Check Battery Status Command
17245||Linux Restart NTPD Service Command
17183||Ubuntu Linux: Determine Your IP Address
17172||HowTo: Determine an IP Address My Linux Server
16510||Linux / Unix: Restart PHP Service Command
8292||FreeBSD: Mount Hard Drive / Disk Command
8190||Reboot a Solaris UNIX System
My bash script:
# Usage: Create pdf files from input (wrapper script)
# Author: Vivek Gite  under GPL v2.x+
#Input file
#Output location
# If file exists 
if [[ -f "$_db" ]]
    # read it
	while IFS='|' read -r pdfid pdfurl pdftitle
    	local pdf="$o/$pdfid.pdf"
        echo "Creating $pdf file ..."
	#Genrate pdf file
        $_writer --quiet --footer-spacing 2 \
        --footer-left "nixCraft is GIT UL++++ W+++ C++++ M+ e+++ d-" \
        --footer-right "Page [page] of [toPage]" --footer-line \
        --footer-font-size 7 --print-media-type "$pdfurl" "$pdf"
    done <"$_db"

Tip: Read from bash variable

Let us say you want a list of all installed php packages on a Debian or Ubuntu Linux, enter:
# My input source is the contents of a variable called $list #
list=$(dpkg --list php\* | awk '/ii/{print $2}')
printf '%s\n' "$list"
Sample outputs:
You can now read from $list and install the package:
# BASH can iterate over $list variable using a "here string" #
while IFS= read -r pkg
    printf 'Installing php package %s...\n' "$pkg"
    /usr/bin/apt-get -qq install $pkg
done <<< "$list"
printf '*** Do not forget to run php5enmod and restart the server (httpd or php5-fpm) ***\n'
Sample outputs:
Installing php package php-pear...
Installing php package php5-cli...
Installing php package php5-common...
Installing php package php5-fpm...
Installing php package php5-gd...
Installing php package php5-json...
Installing php package php5-memcache...
Installing php package php5-mysql...
Installing php package php5-readline...
Installing php package php5-suhosin-extension...
*** Do not forget to run php5enmod and restart the server (httpd or php5-fpm)