aicas.blogg.se

Shell script to monitor cpu and memory usage in linux
Shell script to monitor cpu and memory usage in linux







shell script to monitor cpu and memory usage in linux

Tasks: 1 total, 0 running, 1 sleeping, 0 stopped, 0 zombie Once we find the process id, we can feed the number into the top command in this manner: $ top -p 2225 We can find the process id of a named process using the ps command. When using the -p parameter, we often want to include the process id. If we want to have an idea of a single process, we can use the -p parameter. This command displays a real-time view of a running system in the command prompt. Output: You will receive an email alert similar to the one below.Usually, we can use the Linux built-in top command. Say for example If your system reaches the given limit after 8.25 minutes then you will be getting an email alert on the second cycle i.e after 10 minutes ( 2nd 5 minute cycle) Note: Since the script is scheduled to run once every 5 minutes, you will receive an email alert every 5 minutes. */5 * * * * /bin/bash /opt/scripts/memory-alert-1.sh

shell script to monitor cpu and memory usage in linux

SUBJECT="ATTENTION: Memory Utilization is High on $(hostname) at $(date)"Įcho "Memory Current Usage is: $ramusage%" > $MESSAGEĮcho "-" > $MESSAGEĮcho "Top Memory Consuming Processes Using top command" > $MESSAGEĮcho "$(top -b -o +%MEM | head -n 20)" > $MESSAGEĮcho "Top Memory Consuming Processes Using ps command" > $MESSAGEĮcho "$(ps -eo pid,ppid,%mem,%cpu,cmd -sort=-%mem | head)" > $MESSAGEįinally add a cronjob to automate this. This will trigger an email when more than 60% of your system memory is used. I preferred to go with this method for most of the time. This is very simple and straightforward one line script. If you want to get only the current Memory utilization percentage through mail when the system reaches the given threshold, use the following script. Method-1 : Linux shell script to monitor memory usage with email alert When the system reaches the given threshold then it will trigger a mail to given email id. In this tutorial we have added two shell script to monitor Memory utilization on Linux system. If you only have few systems and want to monitor them then writing a small shell script can make your task very easy. It monitors everything such as CPU utilization, Memory utilization, swap utilization, disk space utilization and much more. There are many open source monitoring tools are currently available in market to monitor Linux systems performance which will send an email alert when the system reaches the specified threshold limit.









Shell script to monitor cpu and memory usage in linux