I love bash

Linux basics – Bash Login Logout scripts

Checking the status of my servers in morning can be a mundane task.  Luckily a tool we use every day has the ability to automate tasks every time you log in and log out.

Like most power users Bash is the most important tool in my suite. For myself logging in to my servers in the morning has a standard check list, also there are tasks that are performed every time login and logout of  my server. Mostly, when I login, I perform network diagnostics. In this tutorial I am going to show you how create a simple script that runs whenever you login to your computer.

Bash

Bash is a unix shell command line. In English, it is a software that runs on the Operating systems ( see this link for how to install Bash on windows) and opens up a new suite of tools.

What happens when I log in, GUI

When Bash (a Unix shell and command language ) starts, it executes commands. This happens in a variety of different scripts. Those scripts (executed in order) include:

  • /etc/profile
  • ~/.bash_profile
  • ~/.bash_login
  • ~/.profile

Bash reads and executes commands from the first one that exists and is readable (has permissions to read).

What happens when I log out, GUI

The following file is read and executed:

  • ~/.bash_logout

 

Others include:

  • sh: ~/.profile
  • tcsh and csh: ~/.login
  • zsh: ~/.zshrc

You can check what shell your in by running echo $SHELL

 

I am going to edit the ~/.profile so only this user will be affected.

Here is my profile with my edits

# if running
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
    fi
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi
echo ""
echo "clients up:"
nmap -sP -R 192.168.2.0/24 -oN host.dat| grep "peggy" |awk '{print $5}'

 

now when I log in:

Last login:  ########

clients up:
peggy1
peggy2
peggy3

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.