Bash script to print/log uptime, load average, cpu usage, diskusage and ram usage

The output of the script will be similar to:

02:20:41 up 35 min, 2 users, load average: 0.22, 0.80, 1.05

Memory Usage: 4986/7994MB (62.37%)

Disk Usage: 23/68GB (35%)

CPU Load: 0.78

=========

vi script1

#!/bin/sh

uptime >> /var/log/monitorlog.txt

free -m | awk ‘NR==2{printf “Memory Usage: %s/%sMB (%.2f%)\n”, $3,$2,$3*100/$2 }’ >> /var/log/monitorlog.txt

df -h | awk ‘$NF==”/”{printf “Disk Usage: %d/%dGB (%s)\n”, $3,$2,$5}’ >> /var/log/monitorlog.txt

top -bn1 | grep load | awk ‘{printf “CPU Load: %.2f\n”, $(NF-2)}’ >> /var/log/monitorlog.txt

echo “=========” >> /var/log/monitorlog.txt

Change the permissions to execute:

chmod 777 script1

Put a shell script in one of these folders: /etc/cron.daily, /etc/cron.hourly, /etc/cron.monthly or /etc/cron.weekly

If these are not enough for you, you can add more specific tasks eg. twice a month or every 5 minutes or… go to the terminal and type:

crontab -e

this will open your personal crontab (cron configuration file), so use http://crontab-generator.org/ to generate the crontab line and paste it under the file.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s