Tag Archives: bash


Shell script wrapper function for sending messages through Pushover

Pushover makes it easy to get real-time notifications on your Android, iPhone, iPad, and Desktop (Android Wear and Apple Watch, too!)

You can use this shell function anywhere in your script.



pushmail() {
    curl 'https://api.pushover.net/1/messages.json' -X POST -d "token=$APP_TOKEN&user=$USER_TOKEN&message=\"$MESSAGE\"&title=\"$TITLE\""

# call
pushmail "server is down"

Note: you need to update API tokens and title above

mutex lock

Locking your bash script against parallel execution

Sometimes there’s a need to ensure only one copy of a script runs, i.e prevent two or more copies running simultaneously. Imagine an important cronjob doing something very important, which will fail or corrupt data if two copies of the called program were to run at the same time. To prevent this, a form of MUTEX (mutual exclusion) lock is needed.

The basic procedure is simple: The script checks if a specific condition (locking) is present at startup, if yes, it’s locked – the script doesn’t start.

This article describes locking with common UNIX¬ģ tools.

Method 1

setting the noclobber shell option (set -C). This will cause redirection to fail, if the file the redirection points to already exists (using diverse open() methods). Need to write a code example here.

if ( set -o noclobber; echo "locked" > "$lockfile") 2> /dev/null; then
  trap 'rm -f "$lockfile"; exit $?' INT TERM EXIT
  echo "Locking succeeded" >&2
  rm -f "$lockfile"
  echo "Lock failed - exit" >&2
  exit 1


Method 2

A simple way to get that is to create a lock directory – with the mkdir command. It will:

create a given directory only if it does not exist, and set a successful exit code
it will set an unsuccessful exit code if an error occurs – for example, if the directory specified already exists
With mkdir it seems, we have our two steps in one simple operation. A (very!) simple locking code might look like this:

if mkdir /var/lock/mylock; then
  echo "Locking succeeded" >&2
  echo "Lock failed - exit" >&2
  exit 1

In case mkdir reports an error, the script will exit at this point – the MUTEX did its job!

How to find width and height of the terminal in linux

You may wonder how do I find the number of characters will fit in a line in the terminal or size of the window. There are several ways to figure out the size of the window. Let’s go through the different ways to find size of terminal

First lets see different ways that you can use to figure out width and height of terminal in terms of characters

  1. Using command tput
  2. Using command stty
  3. Using environment variables

    Using command tput

The command tput is very helpful to query the terminal information and to do some other simple operations like placing cursor at required random position.

Here is how you can find the width and height of terminal. You may call it as number of rows and columns

$ tput lines
$ tput cols
$ echo "columns: `tput cols` Rows: `tput lines` "
columns: 141 Rows: 36 

Using command stty

This is also one of the command which useful to query,change and print terminal line settings.

The simple output of stty command regarding information of terminal

speed 38400 baud; line = 0;
eol = M-^?; eol2 = M-^?; swtch = M-^?;
ixany iutf8

Command to get lines and columns count in characters

$ stty size
39 143


Using environment variables

On you terminal always you can query the size of the terminal using the environment variables $COLUMNS

$ echo "$LINES $COLUMNS"
39 143
$ echo "$LINES $COLUMNS"   # Resized the window
22 79

Values of those environment variables will change if size of the terminal window changes.

Fork Bomb

How does bash fork bomb :(){ :|:& };: work

The fork bomb is kind of DOS (denial-of-service) attack on system. It attacks the system by consuming all resources. It makes use of the fork operation, that is why it is called as fork bomb.

Here is the bash version of fork bomb

:(){ :|:& };:

It is nothing more than bash function definition and calling it recursively to consume all system resources. We can also write this fork bomb in different programming languages like python. It creates endless number of processes to consumes all system resources.

WARNING! ¬† These examples may crash your system. Please don’t try it on production machines or in your libraries or on your machine. If you want to test. Try it executing in Virtual Machine¬†


Once the fork bomb is successfully executed on system it may not be possible to bring system back into normal state with out restarting it.

Break down of bash fork bomb :(){ :|:& };:

Bash fork bomb

:(){ :|:& };:

Here is elaborated  version of bash fork bomb,



 :()  Р is formal bash function definition. That is, we created a function named as : (colon).  We are calling the same function recursively twice in side function definition.

:|:   Р This function is getting called itself and piping output to another function call using |.  This is programming technique recursion nothing but a process of calling function itself.

& Р  This thing will put the process execution to background

;   Р  Termination of function definition

: ¬† – ¬† ¬†Calling the defined function (:). It initiates the fork bomb. With out this thing fork bomb won’t get started.

Here is human readable version of bash fork bomb. Probably it might make sense,

fbomb(){ fbomb | fbomb &}; fbomb

That is,

fbomb() {
 fbomb | fbomb &
}; fbomb

This fork bomb is some times used by system administrators to test process limits of user. You can save the system from crashing by setting process limits. These process limits can be changed using command ulimit  or these limits can configured  at /etc/security/limits.conf and PAM.  Process limits can be configured per user also.
If you set process limits to any process, all child processes will inherit those limits.

Well configured system would not go down when fork  bomb is initiated.

To query all existing limit try the following command.

ulimit -a

$ ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 31463
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 31463
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

You can try executing fork bomb by reducing these limits. So, once these limits are hit, kernel will impose a restriction over creating new processes, this is how you can protect the system from crashing  because these kind DOS attacks.

You can run the fork bomb by reducing max user process limits

ulimit -u 100

$ ulimit -u 100
$ :(){ :|:& };:


$ ulimit -u 100
$ :(){ :|:& };:
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: Resource temporarily unavailable

As there is restriction over creating endless processes your system won’t go down or no need to restart to resume normal operation.