Linux basics – Bash Login Logout scripts

When I do a bash login and logout of  my server there a few tasks I always run. 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

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

Bash reads and executes commands from the file:

  • ~/.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 bash
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 *