SlideShare a Scribd company logo
CIS 216
Dan Morrill
 Top
    Gets you a list of processes that are consuming the CPU
 htop
    Near real time list of running processes by CPU, includes
     scrolling, and mouse support
 vmstat
    Provides information about processes, memory, paging, I/O,
     traps and CPU
 w/who/finger
    Provides information about users that are consuming
     resources on the computer
 ps (ps –ef )
    Lists all the currently running processes on a Linux computer
 pgrep/pkill
   pgrep <process name> lists the PID of the process based on
    name
   pkill <process name> sends a specific kill signal (default
    sigterm or shutdown) to a matching process
 free
   Shows the current memory usage of the system. Shows
    physical and swap memory
 mpstat
   mpstat 2 5 - shows five set of data of global statistics among
    all processors at two second intervals.
   mpstat –P ALL 2 5 - shows 5 sets of statistics for all processors
    at two second intervals.
 iostat
   reports CPU statists for devices and partitions
    (including NFS Samba partitions)
 pmap
   This command reports memory map of a process. This
    can be used to find memory usage of the process.
 Set the debug mode for this, you will want it,
 remember what each debug mode switch does
  1. # set -n : Uncomment to check script syntax, without
     execution.
  2. #     Note: Do not forget to put the comment back in
     or
  3. #      the shell script will not execute!
  4. # set -x : Uncomment to debug this shell script
 PROC_MON=`basename $0`                  # Defines the script_name variable as the
  file name of this script
  LOGFILE="/home/ganesh/procmon.log"         # Shows log file and where
  located
  [[ ! -s $LOGFILE ]] && touch $LOGFILE   # This checks to see if the file exists
                 # if not it creates one.
  TTY=$(tty)                 # Current tty or pty
  PROCESS="ssh"                # This will define which process to monitor
  SLEEP_TIME="1"                # This is the sleep time in second between
  monitoring

  txtred=$(tput setaf 1) # Red: will indicate a failed process and the information
  txtgrn=$(tput setaf 2) # Green: this is successful process information
  txtylw=$(tput setaf 3) # Yellow: this is used to show cautionary information
  txtrst=$(tput sgr0) # resets text
 function exit_trap     # this is the behavior of the trap
  signal
  {
  # Log an ending time for process monitoring
    DATE=$(date +%D)
    TIME=$(date +%T) # Get a new timestamp...
    echo "$DATE @ $TIME: Monitoring for $PROCESS
  terminated" >> $LOGFILE & # this will create an entry in
  the logfile
    echo "$DATE @ $TIME: ${txtred}Monitoring for
  $PROCESS terminated${txtrst}"
  #kill all functions
  kill -9 $(jobs -p) 2>/dev/null
 Set the trap to see if the process exits
 trap 'exit_trap; exit 0' 1 2 3 15

  # this will see if process is running if not will start it

  ps aux | grep "$PROCESS" | grep -v "grep $PROCESS" 
  | grep -v $PROC_MON >/dev/null
   if (( $? != 0 ))
    then
       DATE=$(date +%D)
       TIME=$(date +%T)
       echo
       echo "$DATE @ $TIME: $PROCESS is NOT active...starting $PROCESS.." >> $LOGFILE & #
    creates
                                # an entry in the logfile
       echo "$DATE @ $TIME: ${txtylw}$PROCESS is NOT active...starting $PROCESS..${txtrst}"
       echo
    sleep 1
       service $PROCESS start &
       echo "$DATE @ $TIME: $PROCESS has been started..." >> $LOGFILE & #puts an enrty in logfile
         else # this will say what to do if process is already running
       echo -e "n" # a blank line
       DATE=$(date +%D)
       TIME=$(date +%T)
       echo "$DATE @ $TIME: $PROCESS is currnetly RUNNING..." >> $LOGFILE & # puts entry in logfile
       echo "$DATE @ $TIME: ${txtgrn}$PROCESS is currently RUNNING...${txtrst}"
    fi
 while (( RC == 0 )) # this will loop until the return code is not zero
  do
     ps aux | grep $PROCESS | grep -v "grep $PROCESS" 
     | grep -v $PROC_MON >/dev/null 2>&1
     if (( $? != 0 )) # check the return code
     then
    echo
     DATE=$(date +%D)
    TIME=$(date +%T)
    echo "$DATE @ $TIME: $PROCESS has STOPPED..." >> $LOGFILE & # entry
  in logfile
       echo "$DATE @ $TIME: ${txtred}$PROCESS has STOPPED...${txtrst}"
    echo
    service $PROCESS start &
    echo "$DATE @ $TIME: $PROCESS has RESTARTED..." >> $LOGFILE & #
  ENTRY IN LOGFILE
    echo "$DATE @ $TIME: ${txtgrn}$PROCESS has RESTARTED...${txtrst}"
       sleep 1
      ps aux | grep $PROCESS | grep -v "grep $PROCESS" 
       | grep -v $PROC_MON >/dev/null 2>&1
       if (( $? != 0 ))  # This will check the return code
       then
       echo
       DATE=$(date +%D)         # New time stamp
       TIME=$(date +%T)
       echo "$DATE @ $TIME: $PROCESS failed to restart..." >> $LOGFILE
    & #entry in logfile
       echo "$DATE @ $TIME: ${txtred}$PROCESS failed to
    restart...${txtrst}"
       exit 0
    fi
    fi
      sleep $SLEEP_TIME          # This is needed to reduce CPU Load!!!
    done
 Process is hard coded in the script
   # Process to be monitored
    target="ssh"
 wait_time="10“
 This is in seconds
 log_file="procmon.log"
 script_failure="0"
   # Monitor process and restart if necessary
    for attempt in 1 2 3
    do
       ps aux | grep "$target" | grep -v "grep $target" 
       | grep -v $script_name >/dev/null
       if [ $? != 0 ]
       then
          log_time=$(date)
          echo
          echo "$(tput setaf 3)$target is not running. Attempt will be made to restart. This is attempt
    $attempt of 3.$(tput sgr0)"
          echo >>$log_file
          echo "$log_time: $target is not running. Restarting. Attempt $attempt of 3.">>$log_file
          echo
          service $target start &
          sleep 2 # Pause to prevent false positives from restart attempt.
       else
          attempt="3"
       fi
    done
    sleep 2 # Pause to prevent false positives from restart attempt.
    }
   detect_failure()
    {
    ps aux | grep "$target" | grep -v "grep $target" 
    | grep -v $script_name >/dev/null
    if [ $? != 0 ]
    then
       log_time=$(date)
       echo
       echo "$(tput setaf 1)$target is not running after 3 attempts. Process has failed and
    cannot be restarted. $(tput sgr0)" # Report failure to user
       echo "This script will now close."
       echo "">>$log_file
       echo "$log_time: $target cannot be restarted.">>$log_file # Log failure
       script_failure="1" # Set failure flag
    else
       log_time=$(date)
       echo
       echo "$log_time : $target is running."
       echo "$log_time : $target is running." >> $log_file
    fi
    }
 program_closing()
  {
  # Report and log script shutdown
  log_time=$(date)
  echo
  echo "Closing ProcMon script. No further monitoring of $target will be
  performed." #Reports closing of ProcMon to user
  echo
  echo "$(tput setaf 1)$log_time: Monitoring for $target terminated. $(tput sgr0)"
  echo
  echo "$log_time: Monitoring for $target terminated.">>$log_file # Logs closing
  of ProcMon to log_file
  echo >> $log_file
  echo "***************" >> $log_file
  echo >> $log_file
  # Ensure this script is properly killed
  kill -9 > /dev/null
  }
   # Trap shutdown attempts to enable logging of shutdown
    trap 'program_closing; exit 0' 1 2 3 15
    # Inform user of purpose of script
    clear
    echo
    echo "This script will monitor $target to ensure that it is running,"
    echo "and attempt to restart it if it is not. If it is unable to"
    echo "restart after 3 attempts, it will report failure and close."
    sleep 2
    #Perform monitoring
    while [ $script_failure != "1" ]
    do
      process_monitoring # Monitors process and attempts 3 restarts if it fails.
      detect_failure # Reports failure in the event that the process does not restart.
      if [ $script_failure != "1" ]
      then
         sleep $wait_time
      fi
    done
    sleep 2
    program_closing # Logs script closure
    exit 0
Process monitoring in UNIX shell scripting

More Related Content

What's hot

How to send files to remote server via ssh in php
How to send files to remote server via ssh in phpHow to send files to remote server via ssh in php
How to send files to remote server via ssh in php
Andolasoft Inc
 
Стажировка 2016-07-27 02 Денис Нелюбин. PostgreSQL и jsonb
Стажировка 2016-07-27 02 Денис Нелюбин. PostgreSQL и jsonbСтажировка 2016-07-27 02 Денис Нелюбин. PostgreSQL и jsonb
Стажировка 2016-07-27 02 Денис Нелюбин. PostgreSQL и jsonb
SmartTools
 
Ansible, Simplicity, and the Zen of Python
Ansible, Simplicity, and the Zen of PythonAnsible, Simplicity, and the Zen of Python
Ansible, Simplicity, and the Zen of Python
toddmowen
 
Shell Script
Shell ScriptShell Script
Shell Script
Adam Victor Brandizzi
 
ES6 generators
ES6 generatorsES6 generators
ES6 generators
Steven Foote
 
Asynchronous PHP and Real-time Messaging
Asynchronous PHP and Real-time MessagingAsynchronous PHP and Real-time Messaging
Asynchronous PHP and Real-time Messaging
Steve Rhoades
 
Unix Programming with Perl 2
Unix Programming with Perl 2Unix Programming with Perl 2
Unix Programming with Perl 2
Kazuho Oku
 
Raspberry pi Part 4
Raspberry pi Part 4Raspberry pi Part 4
Raspberry pi Part 4
Techvilla
 
Puppet Camp 2012
Puppet Camp 2012Puppet Camp 2012
Puppet Camp 2012
Server Density
 
Any event intro
Any event introAny event intro
Any event intro
qiang
 
Redis & ZeroMQ: How to scale your application
Redis & ZeroMQ: How to scale your applicationRedis & ZeroMQ: How to scale your application
Redis & ZeroMQ: How to scale your application
rjsmelo
 
Perl: Coro asynchronous
Perl: Coro asynchronous Perl: Coro asynchronous
Perl: Coro asynchronous
Shmuel Fomberg
 
Parsing JSON with a single regex
Parsing JSON with a single regexParsing JSON with a single regex
Parsing JSON with a single regex
brian d foy
 
ZeroMQ Is The Answer
ZeroMQ Is The AnswerZeroMQ Is The Answer
ZeroMQ Is The Answer
Ian Barber
 
Shell实现的windows回收站功能的脚本
Shell实现的windows回收站功能的脚本Shell实现的windows回收站功能的脚本
Shell实现的windows回收站功能的脚本
Lingfei Kong
 
Shell Script to Extract IP Address, MAC Address Information
Shell Script to Extract IP Address, MAC Address InformationShell Script to Extract IP Address, MAC Address Information
Shell Script to Extract IP Address, MAC Address Information
VCP Muthukrishna
 
Bag of tricks
Bag of tricksBag of tricks
Bag of tricks
brian d foy
 
Javascript ES6 generators
Javascript ES6 generatorsJavascript ES6 generators
Javascript ES6 generators
Ramesh Nair
 
Gitosis on Mac OS X Server
Gitosis on Mac OS X ServerGitosis on Mac OS X Server
Gitosis on Mac OS X ServerYasuhiro Asaka
 

What's hot (20)

How to send files to remote server via ssh in php
How to send files to remote server via ssh in phpHow to send files to remote server via ssh in php
How to send files to remote server via ssh in php
 
Стажировка 2016-07-27 02 Денис Нелюбин. PostgreSQL и jsonb
Стажировка 2016-07-27 02 Денис Нелюбин. PostgreSQL и jsonbСтажировка 2016-07-27 02 Денис Нелюбин. PostgreSQL и jsonb
Стажировка 2016-07-27 02 Денис Нелюбин. PostgreSQL и jsonb
 
Ansible, Simplicity, and the Zen of Python
Ansible, Simplicity, and the Zen of PythonAnsible, Simplicity, and the Zen of Python
Ansible, Simplicity, and the Zen of Python
 
Shell Script
Shell ScriptShell Script
Shell Script
 
ES6 generators
ES6 generatorsES6 generators
ES6 generators
 
Asynchronous PHP and Real-time Messaging
Asynchronous PHP and Real-time MessagingAsynchronous PHP and Real-time Messaging
Asynchronous PHP and Real-time Messaging
 
Unix Programming with Perl 2
Unix Programming with Perl 2Unix Programming with Perl 2
Unix Programming with Perl 2
 
Raspberry pi Part 4
Raspberry pi Part 4Raspberry pi Part 4
Raspberry pi Part 4
 
Puppet Camp 2012
Puppet Camp 2012Puppet Camp 2012
Puppet Camp 2012
 
Any event intro
Any event introAny event intro
Any event intro
 
Redis & ZeroMQ: How to scale your application
Redis & ZeroMQ: How to scale your applicationRedis & ZeroMQ: How to scale your application
Redis & ZeroMQ: How to scale your application
 
Perl: Coro asynchronous
Perl: Coro asynchronous Perl: Coro asynchronous
Perl: Coro asynchronous
 
Unix 5 en
Unix 5 enUnix 5 en
Unix 5 en
 
Parsing JSON with a single regex
Parsing JSON with a single regexParsing JSON with a single regex
Parsing JSON with a single regex
 
ZeroMQ Is The Answer
ZeroMQ Is The AnswerZeroMQ Is The Answer
ZeroMQ Is The Answer
 
Shell实现的windows回收站功能的脚本
Shell实现的windows回收站功能的脚本Shell实现的windows回收站功能的脚本
Shell实现的windows回收站功能的脚本
 
Shell Script to Extract IP Address, MAC Address Information
Shell Script to Extract IP Address, MAC Address InformationShell Script to Extract IP Address, MAC Address Information
Shell Script to Extract IP Address, MAC Address Information
 
Bag of tricks
Bag of tricksBag of tricks
Bag of tricks
 
Javascript ES6 generators
Javascript ES6 generatorsJavascript ES6 generators
Javascript ES6 generators
 
Gitosis on Mac OS X Server
Gitosis on Mac OS X ServerGitosis on Mac OS X Server
Gitosis on Mac OS X Server
 

Viewers also liked

KeySens: Passive User Authentication Through Micro Behavior Modeling of Soft ...
KeySens: Passive User Authentication Through Micro Behavior Modeling of Soft ...KeySens: Passive User Authentication Through Micro Behavior Modeling of Soft ...
KeySens: Passive User Authentication Through Micro Behavior Modeling of Soft ...
Jiang Zhu
 
Linux MMAP & Ioremap introduction
Linux MMAP & Ioremap introductionLinux MMAP & Ioremap introduction
Linux MMAP & Ioremap introduction
Gene Chang
 
Chapters 3 4
Chapters 3 4Chapters 3 4
Chapters 3 4
sakshi_20
 
We Know Your Type
We Know Your TypeWe Know Your Type
We Know Your TypeCTIN
 
Keystroke dynamics
Keystroke dynamicsKeystroke dynamics
Keystroke dynamics
Tushar Kayande
 
Trouble shoot with linux syslog
Trouble shoot with linux syslogTrouble shoot with linux syslog
Trouble shoot with linux syslogashok191
 
Linux Shell Scripting Craftsmanship
Linux Shell Scripting CraftsmanshipLinux Shell Scripting Craftsmanship
Linux Shell Scripting Craftsmanship
bokonen
 
UNIX - Class4 - Advance Shell Scripting-P1
UNIX - Class4 - Advance Shell Scripting-P1UNIX - Class4 - Advance Shell Scripting-P1
UNIX - Class4 - Advance Shell Scripting-P1
Nihar Ranjan Paital
 
UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2
Nihar Ranjan Paital
 
UNIX - Class1 - Basic Shell
UNIX - Class1 - Basic ShellUNIX - Class1 - Basic Shell
UNIX - Class1 - Basic Shell
Nihar Ranjan Paital
 
Karkha unix shell scritping
Karkha unix shell scritpingKarkha unix shell scritping
Karkha unix shell scritping
chockit88
 
Module 13 - Troubleshooting
Module 13 - TroubleshootingModule 13 - Troubleshooting
Module 13 - TroubleshootingT. J. Saotome
 
APACHE
APACHEAPACHE
Advanced Oracle Troubleshooting
Advanced Oracle TroubleshootingAdvanced Oracle Troubleshooting
Advanced Oracle Troubleshooting
Hector Martinez
 
Linux troubleshooting tips
Linux troubleshooting tipsLinux troubleshooting tips
Linux troubleshooting tips
Bert Van Vreckem
 
unix training | unix training videos | unix course unix online training
unix training |  unix training videos |  unix course  unix online training unix training |  unix training videos |  unix course  unix online training
unix training | unix training videos | unix course unix online training
Nancy Thomas
 
Fusion Middleware 11g How To Part 2
Fusion Middleware 11g How To Part 2Fusion Middleware 11g How To Part 2
Fusion Middleware 11g How To Part 2
Dirk Nachbar
 
25 Apache Performance Tips
25 Apache Performance Tips25 Apache Performance Tips
25 Apache Performance Tips
Monitis_Inc
 
Sql server troubleshooting
Sql server troubleshootingSql server troubleshooting
Sql server troubleshooting
Nathan Winters
 

Viewers also liked (20)

KeySens: Passive User Authentication Through Micro Behavior Modeling of Soft ...
KeySens: Passive User Authentication Through Micro Behavior Modeling of Soft ...KeySens: Passive User Authentication Through Micro Behavior Modeling of Soft ...
KeySens: Passive User Authentication Through Micro Behavior Modeling of Soft ...
 
Linux MMAP & Ioremap introduction
Linux MMAP & Ioremap introductionLinux MMAP & Ioremap introduction
Linux MMAP & Ioremap introduction
 
Chapters 3 4
Chapters 3 4Chapters 3 4
Chapters 3 4
 
We Know Your Type
We Know Your TypeWe Know Your Type
We Know Your Type
 
Keystroke dynamics
Keystroke dynamicsKeystroke dynamics
Keystroke dynamics
 
Trouble shoot with linux syslog
Trouble shoot with linux syslogTrouble shoot with linux syslog
Trouble shoot with linux syslog
 
Unixshellscript 100406085942-phpapp02
Unixshellscript 100406085942-phpapp02Unixshellscript 100406085942-phpapp02
Unixshellscript 100406085942-phpapp02
 
Linux Shell Scripting Craftsmanship
Linux Shell Scripting CraftsmanshipLinux Shell Scripting Craftsmanship
Linux Shell Scripting Craftsmanship
 
UNIX - Class4 - Advance Shell Scripting-P1
UNIX - Class4 - Advance Shell Scripting-P1UNIX - Class4 - Advance Shell Scripting-P1
UNIX - Class4 - Advance Shell Scripting-P1
 
UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2
 
UNIX - Class1 - Basic Shell
UNIX - Class1 - Basic ShellUNIX - Class1 - Basic Shell
UNIX - Class1 - Basic Shell
 
Karkha unix shell scritping
Karkha unix shell scritpingKarkha unix shell scritping
Karkha unix shell scritping
 
Module 13 - Troubleshooting
Module 13 - TroubleshootingModule 13 - Troubleshooting
Module 13 - Troubleshooting
 
APACHE
APACHEAPACHE
APACHE
 
Advanced Oracle Troubleshooting
Advanced Oracle TroubleshootingAdvanced Oracle Troubleshooting
Advanced Oracle Troubleshooting
 
Linux troubleshooting tips
Linux troubleshooting tipsLinux troubleshooting tips
Linux troubleshooting tips
 
unix training | unix training videos | unix course unix online training
unix training |  unix training videos |  unix course  unix online training unix training |  unix training videos |  unix course  unix online training
unix training | unix training videos | unix course unix online training
 
Fusion Middleware 11g How To Part 2
Fusion Middleware 11g How To Part 2Fusion Middleware 11g How To Part 2
Fusion Middleware 11g How To Part 2
 
25 Apache Performance Tips
25 Apache Performance Tips25 Apache Performance Tips
25 Apache Performance Tips
 
Sql server troubleshooting
Sql server troubleshootingSql server troubleshooting
Sql server troubleshooting
 

Similar to Process monitoring in UNIX shell scripting

Debugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 VersionDebugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 Version
Ian Barber
 
Debugging: Rules & Tools
Debugging: Rules & ToolsDebugging: Rules & Tools
Debugging: Rules & Tools
Ian Barber
 
Counting on God
Counting on GodCounting on God
Counting on God
James Gray
 
Containers: What are they, Really?
Containers: What are they, Really?Containers: What are they, Really?
Containers: What are they, Really?
Sneha Inguva
 
exercises-log-management-rsyslog.pdf
exercises-log-management-rsyslog.pdfexercises-log-management-rsyslog.pdf
exercises-log-management-rsyslog.pdf
SngB2
 
Really useful linux commands
Really useful linux commandsReally useful linux commands
Really useful linux commands
Michael J Geiser
 
Node.js Event Loop & EventEmitter
Node.js Event Loop & EventEmitterNode.js Event Loop & EventEmitter
Node.js Event Loop & EventEmitter
Simen Li
 
Linux cheat sheet
Linux cheat sheetLinux cheat sheet
Linux cheat sheet
Pinaki Mahata Mukherjee
 
Shell Script Disk Usage Report and E-Mail Current Threshold Status
Shell Script  Disk Usage Report and E-Mail Current Threshold StatusShell Script  Disk Usage Report and E-Mail Current Threshold Status
Shell Script Disk Usage Report and E-Mail Current Threshold Status
VCP Muthukrishna
 
10 tips for making Bash a sane programming language
10 tips for making Bash a sane programming language10 tips for making Bash a sane programming language
10 tips for making Bash a sane programming language
Yaroslav Tkachenko
 
Create an auto-extractible shell script linux
Create an auto-extractible shell script linuxCreate an auto-extractible shell script linux
Create an auto-extractible shell script linux
Thierry Gayet
 
linux_Commads
linux_Commadslinux_Commads
linux_Commads
tastedone
 
03 tk2123 - pemrograman shell-2
03   tk2123 - pemrograman shell-203   tk2123 - pemrograman shell-2
03 tk2123 - pemrograman shell-2
Setia Juli Irzal Ismail
 
Puppi. Puppet strings to the shell
Puppi. Puppet strings to the shellPuppi. Puppet strings to the shell
Puppi. Puppet strings to the shell
Alessandro Franceschi
 
PHP Backdoor: The rise of the vuln
PHP Backdoor: The rise of the vulnPHP Backdoor: The rise of the vuln
PHP Backdoor: The rise of the vuln
Sandro Zaccarini
 
Phishing for Root (How I Got Access to Root on Your Computer With 8 Seconds o...
Phishing for Root (How I Got Access to Root on Your Computer With 8 Seconds o...Phishing for Root (How I Got Access to Root on Your Computer With 8 Seconds o...
Phishing for Root (How I Got Access to Root on Your Computer With 8 Seconds o...
Vi Grey
 
Tips on how to improve the performance of your custom modules for high volume...
Tips on how to improve the performance of your custom modules for high volume...Tips on how to improve the performance of your custom modules for high volume...
Tips on how to improve the performance of your custom modules for high volume...Odoo
 
PHP PPT FILE
PHP PPT FILEPHP PPT FILE
PHP PPT FILE
AbhishekSharma2958
 

Similar to Process monitoring in UNIX shell scripting (20)

Debugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 VersionDebugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 Version
 
Debugging: Rules & Tools
Debugging: Rules & ToolsDebugging: Rules & Tools
Debugging: Rules & Tools
 
Counting on God
Counting on GodCounting on God
Counting on God
 
Containers: What are they, Really?
Containers: What are they, Really?Containers: What are they, Really?
Containers: What are they, Really?
 
exercises-log-management-rsyslog.pdf
exercises-log-management-rsyslog.pdfexercises-log-management-rsyslog.pdf
exercises-log-management-rsyslog.pdf
 
Really useful linux commands
Really useful linux commandsReally useful linux commands
Really useful linux commands
 
Five
FiveFive
Five
 
Node.js Event Loop & EventEmitter
Node.js Event Loop & EventEmitterNode.js Event Loop & EventEmitter
Node.js Event Loop & EventEmitter
 
Linux cheat sheet
Linux cheat sheetLinux cheat sheet
Linux cheat sheet
 
Shell Script Disk Usage Report and E-Mail Current Threshold Status
Shell Script  Disk Usage Report and E-Mail Current Threshold StatusShell Script  Disk Usage Report and E-Mail Current Threshold Status
Shell Script Disk Usage Report and E-Mail Current Threshold Status
 
10 tips for making Bash a sane programming language
10 tips for making Bash a sane programming language10 tips for making Bash a sane programming language
10 tips for making Bash a sane programming language
 
Create an auto-extractible shell script linux
Create an auto-extractible shell script linuxCreate an auto-extractible shell script linux
Create an auto-extractible shell script linux
 
linux_Commads
linux_Commadslinux_Commads
linux_Commads
 
03 tk2123 - pemrograman shell-2
03   tk2123 - pemrograman shell-203   tk2123 - pemrograman shell-2
03 tk2123 - pemrograman shell-2
 
Puppi. Puppet strings to the shell
Puppi. Puppet strings to the shellPuppi. Puppet strings to the shell
Puppi. Puppet strings to the shell
 
PHP Backdoor: The rise of the vuln
PHP Backdoor: The rise of the vulnPHP Backdoor: The rise of the vuln
PHP Backdoor: The rise of the vuln
 
Phishing for Root (How I Got Access to Root on Your Computer With 8 Seconds o...
Phishing for Root (How I Got Access to Root on Your Computer With 8 Seconds o...Phishing for Root (How I Got Access to Root on Your Computer With 8 Seconds o...
Phishing for Root (How I Got Access to Root on Your Computer With 8 Seconds o...
 
Tips on how to improve the performance of your custom modules for high volume...
Tips on how to improve the performance of your custom modules for high volume...Tips on how to improve the performance of your custom modules for high volume...
Tips on how to improve the performance of your custom modules for high volume...
 
lec4.docx
lec4.docxlec4.docx
lec4.docx
 
PHP PPT FILE
PHP PPT FILEPHP PPT FILE
PHP PPT FILE
 

More from Dan Morrill

Windows power shell and active directory
Windows power shell and active directoryWindows power shell and active directory
Windows power shell and active directory
Dan Morrill
 
Understanding web site analytics
Understanding web site analyticsUnderstanding web site analytics
Understanding web site analytics
Dan Morrill
 
Understanding UNIX CASE and TPUT
Understanding UNIX CASE and TPUTUnderstanding UNIX CASE and TPUT
Understanding UNIX CASE and TPUT
Dan Morrill
 
Information security principles
Information security principlesInformation security principles
Information security principles
Dan Morrill
 
Using Regular Expressions in Grep
Using Regular Expressions in GrepUsing Regular Expressions in Grep
Using Regular Expressions in Grep
Dan Morrill
 
Understanding the security_organization
Understanding the security_organizationUnderstanding the security_organization
Understanding the security_organization
Dan Morrill
 
You should ask before copying that media
You should ask before copying that mediaYou should ask before copying that media
You should ask before copying that media
Dan Morrill
 
Understanding advanced persistent threats (APT)
Understanding advanced persistent threats (APT)Understanding advanced persistent threats (APT)
Understanding advanced persistent threats (APT)
Dan Morrill
 
AWS Hadoop and PIG and overview
AWS Hadoop and PIG and overviewAWS Hadoop and PIG and overview
AWS Hadoop and PIG and overview
Dan Morrill
 
What is cloud computing
What is cloud computingWhat is cloud computing
What is cloud computing
Dan Morrill
 
Social Media Plan for CityU of Seattle
Social Media Plan for CityU of SeattleSocial Media Plan for CityU of Seattle
Social Media Plan for CityU of Seattle
Dan Morrill
 
Case Studies In Social Media Chinese
Case Studies In Social Media ChineseCase Studies In Social Media Chinese
Case Studies In Social Media Chinese
Dan Morrill
 
Case Studies In Social Media
Case Studies In Social MediaCase Studies In Social Media
Case Studies In Social Media
Dan Morrill
 
Turn On Tune In Step Out
Turn On Tune In Step OutTurn On Tune In Step Out
Turn On Tune In Step Out
Dan Morrill
 
Technology And The Future Of Management
Technology And The Future Of ManagementTechnology And The Future Of Management
Technology And The Future Of Management
Dan Morrill
 

More from Dan Morrill (16)

Windows power shell and active directory
Windows power shell and active directoryWindows power shell and active directory
Windows power shell and active directory
 
Understanding web site analytics
Understanding web site analyticsUnderstanding web site analytics
Understanding web site analytics
 
Understanding UNIX CASE and TPUT
Understanding UNIX CASE and TPUTUnderstanding UNIX CASE and TPUT
Understanding UNIX CASE and TPUT
 
Information security principles
Information security principlesInformation security principles
Information security principles
 
Using Regular Expressions in Grep
Using Regular Expressions in GrepUsing Regular Expressions in Grep
Using Regular Expressions in Grep
 
Understanding the security_organization
Understanding the security_organizationUnderstanding the security_organization
Understanding the security_organization
 
You should ask before copying that media
You should ask before copying that mediaYou should ask before copying that media
You should ask before copying that media
 
Understanding advanced persistent threats (APT)
Understanding advanced persistent threats (APT)Understanding advanced persistent threats (APT)
Understanding advanced persistent threats (APT)
 
AWS Hadoop and PIG and overview
AWS Hadoop and PIG and overviewAWS Hadoop and PIG and overview
AWS Hadoop and PIG and overview
 
What is cloud computing
What is cloud computingWhat is cloud computing
What is cloud computing
 
Social Media Plan for CityU of Seattle
Social Media Plan for CityU of SeattleSocial Media Plan for CityU of Seattle
Social Media Plan for CityU of Seattle
 
BSIS Overview
BSIS OverviewBSIS Overview
BSIS Overview
 
Case Studies In Social Media Chinese
Case Studies In Social Media ChineseCase Studies In Social Media Chinese
Case Studies In Social Media Chinese
 
Case Studies In Social Media
Case Studies In Social MediaCase Studies In Social Media
Case Studies In Social Media
 
Turn On Tune In Step Out
Turn On Tune In Step OutTurn On Tune In Step Out
Turn On Tune In Step Out
 
Technology And The Future Of Management
Technology And The Future Of ManagementTechnology And The Future Of Management
Technology And The Future Of Management
 

Recently uploaded

678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 

Recently uploaded (20)

678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 

Process monitoring in UNIX shell scripting

  • 2.  Top  Gets you a list of processes that are consuming the CPU  htop  Near real time list of running processes by CPU, includes scrolling, and mouse support  vmstat  Provides information about processes, memory, paging, I/O, traps and CPU  w/who/finger  Provides information about users that are consuming resources on the computer  ps (ps –ef )  Lists all the currently running processes on a Linux computer
  • 3.  pgrep/pkill  pgrep <process name> lists the PID of the process based on name  pkill <process name> sends a specific kill signal (default sigterm or shutdown) to a matching process  free  Shows the current memory usage of the system. Shows physical and swap memory  mpstat  mpstat 2 5 - shows five set of data of global statistics among all processors at two second intervals.  mpstat –P ALL 2 5 - shows 5 sets of statistics for all processors at two second intervals.
  • 4.  iostat  reports CPU statists for devices and partitions (including NFS Samba partitions)  pmap  This command reports memory map of a process. This can be used to find memory usage of the process.
  • 5.  Set the debug mode for this, you will want it, remember what each debug mode switch does 1. # set -n : Uncomment to check script syntax, without execution. 2. # Note: Do not forget to put the comment back in or 3. # the shell script will not execute! 4. # set -x : Uncomment to debug this shell script
  • 6.  PROC_MON=`basename $0` # Defines the script_name variable as the file name of this script LOGFILE="/home/ganesh/procmon.log" # Shows log file and where located [[ ! -s $LOGFILE ]] && touch $LOGFILE # This checks to see if the file exists # if not it creates one. TTY=$(tty) # Current tty or pty PROCESS="ssh" # This will define which process to monitor SLEEP_TIME="1" # This is the sleep time in second between monitoring txtred=$(tput setaf 1) # Red: will indicate a failed process and the information txtgrn=$(tput setaf 2) # Green: this is successful process information txtylw=$(tput setaf 3) # Yellow: this is used to show cautionary information txtrst=$(tput sgr0) # resets text
  • 7.  function exit_trap # this is the behavior of the trap signal { # Log an ending time for process monitoring DATE=$(date +%D) TIME=$(date +%T) # Get a new timestamp... echo "$DATE @ $TIME: Monitoring for $PROCESS terminated" >> $LOGFILE & # this will create an entry in the logfile echo "$DATE @ $TIME: ${txtred}Monitoring for $PROCESS terminated${txtrst}" #kill all functions kill -9 $(jobs -p) 2>/dev/null
  • 8.  Set the trap to see if the process exits  trap 'exit_trap; exit 0' 1 2 3 15 # this will see if process is running if not will start it ps aux | grep "$PROCESS" | grep -v "grep $PROCESS" | grep -v $PROC_MON >/dev/null
  • 9. if (( $? != 0 )) then DATE=$(date +%D) TIME=$(date +%T) echo echo "$DATE @ $TIME: $PROCESS is NOT active...starting $PROCESS.." >> $LOGFILE & # creates # an entry in the logfile echo "$DATE @ $TIME: ${txtylw}$PROCESS is NOT active...starting $PROCESS..${txtrst}" echo sleep 1 service $PROCESS start & echo "$DATE @ $TIME: $PROCESS has been started..." >> $LOGFILE & #puts an enrty in logfile else # this will say what to do if process is already running echo -e "n" # a blank line DATE=$(date +%D) TIME=$(date +%T) echo "$DATE @ $TIME: $PROCESS is currnetly RUNNING..." >> $LOGFILE & # puts entry in logfile echo "$DATE @ $TIME: ${txtgrn}$PROCESS is currently RUNNING...${txtrst}" fi
  • 10.  while (( RC == 0 )) # this will loop until the return code is not zero do ps aux | grep $PROCESS | grep -v "grep $PROCESS" | grep -v $PROC_MON >/dev/null 2>&1 if (( $? != 0 )) # check the return code then echo DATE=$(date +%D) TIME=$(date +%T) echo "$DATE @ $TIME: $PROCESS has STOPPED..." >> $LOGFILE & # entry in logfile echo "$DATE @ $TIME: ${txtred}$PROCESS has STOPPED...${txtrst}" echo service $PROCESS start & echo "$DATE @ $TIME: $PROCESS has RESTARTED..." >> $LOGFILE & # ENTRY IN LOGFILE echo "$DATE @ $TIME: ${txtgrn}$PROCESS has RESTARTED...${txtrst}" sleep 1
  • 11. ps aux | grep $PROCESS | grep -v "grep $PROCESS" | grep -v $PROC_MON >/dev/null 2>&1 if (( $? != 0 )) # This will check the return code then echo DATE=$(date +%D) # New time stamp TIME=$(date +%T) echo "$DATE @ $TIME: $PROCESS failed to restart..." >> $LOGFILE & #entry in logfile echo "$DATE @ $TIME: ${txtred}$PROCESS failed to restart...${txtrst}" exit 0 fi fi sleep $SLEEP_TIME # This is needed to reduce CPU Load!!! done
  • 12.  Process is hard coded in the script  # Process to be monitored target="ssh"
  • 16. # Monitor process and restart if necessary for attempt in 1 2 3 do ps aux | grep "$target" | grep -v "grep $target" | grep -v $script_name >/dev/null if [ $? != 0 ] then log_time=$(date) echo echo "$(tput setaf 3)$target is not running. Attempt will be made to restart. This is attempt $attempt of 3.$(tput sgr0)" echo >>$log_file echo "$log_time: $target is not running. Restarting. Attempt $attempt of 3.">>$log_file echo service $target start & sleep 2 # Pause to prevent false positives from restart attempt. else attempt="3" fi done sleep 2 # Pause to prevent false positives from restart attempt. }
  • 17. detect_failure() { ps aux | grep "$target" | grep -v "grep $target" | grep -v $script_name >/dev/null if [ $? != 0 ] then log_time=$(date) echo echo "$(tput setaf 1)$target is not running after 3 attempts. Process has failed and cannot be restarted. $(tput sgr0)" # Report failure to user echo "This script will now close." echo "">>$log_file echo "$log_time: $target cannot be restarted.">>$log_file # Log failure script_failure="1" # Set failure flag else log_time=$(date) echo echo "$log_time : $target is running." echo "$log_time : $target is running." >> $log_file fi }
  • 18.  program_closing() { # Report and log script shutdown log_time=$(date) echo echo "Closing ProcMon script. No further monitoring of $target will be performed." #Reports closing of ProcMon to user echo echo "$(tput setaf 1)$log_time: Monitoring for $target terminated. $(tput sgr0)" echo echo "$log_time: Monitoring for $target terminated.">>$log_file # Logs closing of ProcMon to log_file echo >> $log_file echo "***************" >> $log_file echo >> $log_file # Ensure this script is properly killed kill -9 > /dev/null }
  • 19. # Trap shutdown attempts to enable logging of shutdown trap 'program_closing; exit 0' 1 2 3 15 # Inform user of purpose of script clear echo echo "This script will monitor $target to ensure that it is running," echo "and attempt to restart it if it is not. If it is unable to" echo "restart after 3 attempts, it will report failure and close." sleep 2 #Perform monitoring while [ $script_failure != "1" ] do process_monitoring # Monitors process and attempts 3 restarts if it fails. detect_failure # Reports failure in the event that the process does not restart. if [ $script_failure != "1" ] then sleep $wait_time fi done sleep 2 program_closing # Logs script closure exit 0