{"id":2356,"date":"2026-11-21T22:05:29","date_gmt":"2026-11-21T22:05:29","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/?p=2356"},"modified":"2026-11-21T22:05:29","modified_gmt":"2026-11-21T22:05:29","slug":"test-your-shell-scripting-skills-program-4","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/test-your-shell-scripting-skills-program-4\/","title":{"rendered":"Shell Bash Scripting: Assignment &#038; Excercise &#8211; 3"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The script above prints a quick health report for a Linux server: uptime, logged-in users, recent logins, disk and memory use, OOM killer events, the busiest processes, open ports, socket statistics and vmstat. Several lines in it are broken or give wrong numbers on current distributions. This page explains each command, fixes the bugs and gives an improved script you can run, schedule and extend.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Write a Shell Bash Script for report server related information<\/h2>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/1ec5a67970baaea97218494a249e1b88.js\"><\/script>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What each part of the script reports<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The exercise asks you to collect the information an engineer checks first when someone says a server is slow or misbehaving. Every line in the gist calls a standard Linux tool. Knowing what each one shows makes the report easier to read and easier to fix when a tool behaves differently on another distribution.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Command<\/th><th>What it shows<\/th><th>Notes<\/th><\/tr><\/thead><tbody><tr><td><code>date<\/code>, <code>uptime<\/code><\/td><td>Current time, time since boot, number of users, 1\/5\/15-minute load average<\/td><td>A load average that stays above the CPU count (<code>nproc<\/code>) points to CPU or I\/O pressure<\/td><\/tr><tr><td><code>w<\/code><\/td><td>Logged-in users and what each one is running<\/td><td>Reads utmp<\/td><\/tr><tr><td><code>last -a<\/code><\/td><td>Recent logins, with the source host in the last column<\/td><td>Reads <code>\/var\/log\/wtmp<\/code>. Some newer distributions use wtmpdb instead, so <code>last<\/code> may be missing or a different implementation<\/td><\/tr><tr><td><code>df -h<\/code><\/td><td>Filesystem size, used and available space<\/td><td>Use <code>-P<\/code> for output that is always one line per filesystem, which is safe to parse<\/td><\/tr><tr><td><code>free -m<\/code><\/td><td>RAM and swap in MiB<\/td><td>Use the <code>available<\/code> column, not <code>free<\/code>, to judge memory headroom<\/td><\/tr><tr><td><code>grep<\/code> on kernel log<\/td><td>Out-of-memory killer events<\/td><td>The log location depends on the distribution<\/td><\/tr><tr><td><code>top -b<\/code><\/td><td>CPU summary and processes in batch (non-interactive) mode<\/td><td>Always add <code>-n 1<\/code> in scripts<\/td><\/tr><tr><td><code>nmap<\/code> \/ <code>ss -tln<\/code><\/td><td>Listening TCP ports<\/td><td><code>ss<\/code> is installed almost everywhere. <code>nmap<\/code> usually is not<\/td><\/tr><tr><td><code>ss -s<\/code><\/td><td>Socket totals by state and protocol<\/td><td>Quick way to spot connection leaks<\/td><\/tr><tr><td><code>ps auxf<\/code><\/td><td>Full process tree<\/td><td>Can run to thousands of lines on busy hosts<\/td><\/tr><tr><td><code>vmstat 1 5<\/code><\/td><td>Five one-second samples of CPU, memory, swap and I\/O<\/td><td>High <code>wa<\/code> means I\/O wait. Non-zero <code>si<\/code>\/<code>so<\/code> means active swapping<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Problems in the original script<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The script runs, but some sections print errors or misleading numbers:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>nmap -p -T4 127.0.0.1<\/code> is invalid.<\/strong> <code>-p<\/code> needs a port list, so nmap reads <code>-T4<\/code> as the port list and exits with an illegal port specification error. The author probably meant <code>-p-<\/code> (all ports). nmap is also not installed by default on most servers, and scanning 127.0.0.1 misses services that listen only on a specific external IP.<\/li>\n\n\n<li><strong>The memory line reports the wrong value.<\/strong> <code>free -m | xargs<\/code> flattens the output into one line, and awk field <code>$17<\/code> is then <strong>free swap<\/strong> on current procps versions. The field numbers only matched the old <code>free<\/code> layout that had a <code>-\/+ buffers\/cache<\/code> line.<\/li>\n\n\n<li><strong>The disk line reports the wrong filesystem.<\/strong> After <code>xargs<\/code>, fields <code>$9<\/code> and <code>$11<\/code> are the size and available space of whichever filesystem <code>df<\/code> lists first. That is often <code>devtmpfs<\/code> or <code>tmpfs<\/code>, not <code>\/<\/code>.<\/li>\n\n\n<li><strong>The OOM count is inaccurate.<\/strong> <code>grep -ci kill<\/code> counts every line that contains &quot;kill&quot; (any process, any signal), not just out-of-memory kills.<\/li>\n\n\n<li><strong><code>\/var\/log\/messages<\/code> often does not exist.<\/strong> Debian and Ubuntu write to <code>\/var\/log\/syslog<\/code>, and systems that only use journald have neither file. Reading the file also usually requires root.<\/li>\n\n\n<li><strong><code>top -b<\/code> without <code>-n<\/code> never ends on its own.<\/strong> It only stops because <code>head<\/code> closes the pipe. <code>top -b -n 1<\/code> makes one pass and exits cleanly.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Corrected and improved script<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This version keeps the same report structure. It fixes the parsing bugs, uses <code>journalctl<\/code> for OOM events with a fallback to the classic log files, replaces nmap with <code>ss<\/code>, and checks that optional tools exist before calling them.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">#!\/usr\/bin\/env bash<\/span>\n<span class=\"hljs-comment\"># server-health.sh - quick health report for a Linux server<\/span>\n<span class=\"hljs-comment\"># Usage: sudo .\/server-health.sh   (works without sudo, with less detail)<\/span>\n\nset -uo pipefail\n\nsection() {\n  printf <span class=\"hljs-string\">'\\n==================== %s ====================\\n'<\/span> <span class=\"hljs-string\">\"$1\"<\/span>\n}\n\nhave() {\n  command -v <span class=\"hljs-string\">\"$1\"<\/span> &gt;\/dev\/<span class=\"hljs-keyword\">null<\/span> <span class=\"hljs-number\">2<\/span>&gt;&amp;<span class=\"hljs-number\">1<\/span>\n}\n\n<span class=\"hljs-keyword\">if<\/span> &#91; <span class=\"hljs-string\">\"$(id -u)\"<\/span> -ne <span class=\"hljs-number\">0<\/span> ]; then\n  <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Note: not running as root; kernel logs and port owners may be hidden.\"<\/span> &gt;&amp;<span class=\"hljs-number\">2<\/span>\nfi\n\nsection <span class=\"hljs-string\">\"Host\"<\/span>\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Hostname : $(uname -n)\"<\/span>\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Date     : $(date '+%Y-%m-%d %H:%M:%S %Z')\"<\/span>\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Kernel   : $(uname -r)\"<\/span>\n<span class=\"hljs-keyword\">if<\/span> &#91; -r \/etc\/os-release ]; then\n  <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"OS       : $(. \/etc\/os-release &amp;&amp; echo \"<\/span>${PRETTY_NAME:-unknown}<span class=\"hljs-string\">\")\"<\/span>\nfi\n\nsection <span class=\"hljs-string\">\"Uptime and load average\"<\/span>\nuptime\n\nsection <span class=\"hljs-string\">\"Currently connected users\"<\/span>\nw\n\nsection <span class=\"hljs-string\">\"Last 3 logins\"<\/span>\n<span class=\"hljs-keyword\">if<\/span> have last; then\n  last -a -n <span class=\"hljs-number\">3<\/span> <span class=\"hljs-number\">2<\/span>&gt;\/dev\/<span class=\"hljs-keyword\">null<\/span> || <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"No login records available\"<\/span>\n<span class=\"hljs-keyword\">else<\/span>\n  <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"'last' is not installed\"<\/span>\nfi\n\nsection <span class=\"hljs-string\">\"Disk usage\"<\/span>\ndf -hP \/ | awk <span class=\"hljs-string\">'NR==2 {printf \"Free\/total on \/: %s \/ %s (%s used)\\n\", $4, $2, $5}'<\/span>\n<span class=\"hljs-keyword\">echo<\/span>\ndf -h -x tmpfs -x devtmpfs -x squashfs\n\nsection <span class=\"hljs-string\">\"Memory\"<\/span>\nfree -m | awk <span class=\"hljs-string\">'\n  \/^Mem:\/  {printf \"Available\/total memory: %s \/ %s MB\\n\", $7, $2}\n  \/^Swap:\/ {printf \"Free\/total swap:        %s \/ %s MB\\n\", $4, $2}'<\/span>\n\nsection <span class=\"hljs-string\">\"OOM killer events\"<\/span>\n<span class=\"hljs-keyword\">if<\/span> have journalctl; then\n  oom=$(journalctl -k -b --no-pager <span class=\"hljs-number\">2<\/span>&gt;\/dev\/<span class=\"hljs-keyword\">null<\/span> | grep -ci <span class=\"hljs-string\">'out of memory'<\/span>)\n  <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"OOM events since last boot: $oom\"<\/span>\n<span class=\"hljs-keyword\">else<\/span>\n  logfile=<span class=\"hljs-string\">\"\"<\/span>\n  <span class=\"hljs-keyword\">for<\/span> f in \/<span class=\"hljs-keyword\">var<\/span>\/log\/messages \/<span class=\"hljs-keyword\">var<\/span>\/log\/syslog \/<span class=\"hljs-keyword\">var<\/span>\/log\/kern.log; <span class=\"hljs-keyword\">do<\/span>\n    <span class=\"hljs-keyword\">if<\/span> &#91; -r <span class=\"hljs-string\">\"$f\"<\/span> ]; then logfile=$f; <span class=\"hljs-keyword\">break<\/span>; fi\n  done\n  <span class=\"hljs-keyword\">if<\/span> &#91; -n <span class=\"hljs-string\">\"$logfile\"<\/span> ]; then\n    start_log=$(head <span class=\"hljs-number\">-1<\/span> <span class=\"hljs-string\">\"$logfile\"<\/span> | cut -c <span class=\"hljs-number\">1<\/span><span class=\"hljs-number\">-15<\/span>)\n    oom=$(grep -ci <span class=\"hljs-string\">'out of memory'<\/span> <span class=\"hljs-string\">\"$logfile\"<\/span>)\n    <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"OOM events in $logfile since $start_log: $oom\"<\/span>\n  <span class=\"hljs-keyword\">else<\/span>\n    <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"No readable kernel log found\"<\/span>\n  fi\nfi\n\nsection <span class=\"hljs-string\">\"CPU summary\"<\/span>\ntop -b -n <span class=\"hljs-number\">1<\/span> | head <span class=\"hljs-number\">-5<\/span>\n\nsection <span class=\"hljs-string\">\"Top 5 processes by CPU\"<\/span>\nps -eo pid,user,%cpu,%mem,etime,comm --sort=-%cpu | head <span class=\"hljs-number\">-6<\/span>\n\nsection <span class=\"hljs-string\">\"Top 5 processes by memory\"<\/span>\nps -eo pid,user,%cpu,%mem,rss,comm --sort=-%mem | head <span class=\"hljs-number\">-6<\/span>\n\nsection <span class=\"hljs-string\">\"Listening TCP ports\"<\/span>\n<span class=\"hljs-keyword\">if<\/span> have ss; then\n  ss -tlnp\nelif have netstat; then\n  netstat -tlnp\n<span class=\"hljs-keyword\">else<\/span>\n  <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Neither ss nor netstat is installed\"<\/span>\nfi\n\nsection <span class=\"hljs-string\">\"Socket summary\"<\/span>\nss -s <span class=\"hljs-number\">2<\/span>&gt;\/dev\/<span class=\"hljs-keyword\">null<\/span> || <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"ss not available\"<\/span>\n\nsection <span class=\"hljs-string\">\"Process tree (first 40 lines)\"<\/span>\nps auxf --width=<span class=\"hljs-number\">200<\/span> | head <span class=\"hljs-number\">-40<\/span>\n\nsection <span class=\"hljs-string\">\"vmstat (5 samples, 1 second apart)\"<\/span>\nvmstat <span class=\"hljs-number\">1<\/span> <span class=\"hljs-number\">5<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li><code>set -uo pipefail<\/code> stops the script on an unset variable and makes a pipeline fail when any command in it fails. <code>set -e<\/code> is left out on purpose: a missing tool should not stop the rest of the report.<\/li>\n\n\n<li><code>have()<\/code> wraps <code>command -v<\/code>, so optional tools such as <code>last<\/code>, <code>netstat<\/code> and <code>journalctl<\/code> are skipped cleanly when they are not installed.<\/li>\n\n\n<li><code>df -hP \/<\/code> gives exactly one data line for the root filesystem. awk then reads columns <code>$2<\/code> (size), <code>$4<\/code> (available) and <code>$5<\/code> (use%).<\/li>\n\n\n<li>awk matches the <code>Mem:<\/code> and <code>Swap:<\/code> lines by name. It does not count fields across flattened output, so the result is correct even if the column layout changes.<\/li>\n\n\n<li><code>journalctl -k -b<\/code> reads kernel messages from the current boot. Searching for <code>out of memory<\/code> matches both system-wide and cgroup OOM kills.<\/li>\n\n\n<li><code>ps --sort=-%cpu<\/code> lists the busiest processes reliably. Note that <code>ps<\/code> reports %CPU as an average over each process&#039;s lifetime, not the current instant.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Running and scheduling the report<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Save the script as <code>server-health.sh<\/code> and make it executable with <code>chmod +x server-health.sh<\/code>.<\/li>\n\n\n<li>Check the syntax without running it: <code>bash -n server-health.sh<\/code>. If it is installed, <code>shellcheck server-health.sh<\/code> also catches quoting mistakes.<\/li>\n\n\n<li>Run it with root privileges so kernel logs and port owners are visible: <code>sudo .\/server-health.sh<\/code>.<\/li>\n\n\n<li>Save a dated copy for comparison later, as shown below.<\/li>\n\n\n<li>Optionally, install it in <code>\/usr\/local\/bin<\/code> and schedule it with cron.<\/li>\n<\/ol>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># Save a dated report<\/span>\nsudo .\/server-health.sh | tee <span class=\"hljs-string\">\"health-$(uname -n)-$(date +%F).txt\"<\/span>\n\n<span class=\"hljs-comment\"># Run against a remote host without copying the file there<\/span>\nssh admin@web01 <span class=\"hljs-string\">'sudo bash -s'<\/span> &lt; server-health.sh\n\n<span class=\"hljs-comment\"># Daily report at 07:00 (add with: sudo crontab -e)<\/span>\n<span class=\"hljs-number\">0<\/span> <span class=\"hljs-number\">7<\/span> * * * \/usr\/local\/bin\/server-health.sh &gt; \/<span class=\"hljs-keyword\">var<\/span>\/log\/server-health.log <span class=\"hljs-number\">2<\/span>&gt;&amp;<span class=\"hljs-number\">1<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> With <code>ssh ... &#039;sudo bash -s&#039;<\/code>, sudo must not prompt for a password, because the script is read from stdin. Use a sudoers rule limited to this one command, or connect as a user that already has the needed access.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example output<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Values differ on every host. Here is an abbreviated run on a small VM:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">==================== Host ====================\nHostname : web01\nDate     : 2026-11-21 09:14:02 UTC\nKernel   : 6.8.0-87-generic\nOS       : Ubuntu 24.04 LTS\n\n==================== Uptime and load average ====================\n 09:14:02 up 12 days,  3:41,  1 user,  load average: 0.21, 0.30, 0.27\n\n==================== Disk usage ====================\nFree\/total on \/: 31G \/ 49G (35% used)\n\n==================== Memory ====================\nAvailable\/total memory: 2874 \/ 3913 MB\nFree\/total swap:        2047 \/ 2047 MB\n\n==================== OOM killer events ====================\nOOM events since last boot: 0<\/code><\/span><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Common errors and fixes<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Error<\/th><th>Cause<\/th><th>Fix<\/th><\/tr><\/thead><tbody><tr><td><code>Your port specifications are illegal<\/code> from nmap<\/td><td><code>-p<\/code> took <code>-T4<\/code> as its port list<\/td><td>Use <code>-p-<\/code>, or better, <code>ss -tln<\/code><\/td><\/tr><tr><td><code>grep: \/var\/log\/messages: No such file or directory<\/code><\/td><td>Debian\/Ubuntu or a journald-only system<\/td><td>Use <code>journalctl -k<\/code>, or the fallback loop in the improved script<\/td><\/tr><tr><td><code>No journal files were opened due to insufficient permissions<\/code><\/td><td>Non-root user outside the <code>systemd-journal<\/code> or <code>adm<\/code> group<\/td><td>Run with <code>sudo<\/code>, or add the user to one of those groups<\/td><\/tr><tr><td><code>last: cannot open \/var\/log\/wtmp<\/code> or <code>last: command not found<\/code><\/td><td>No wtmp file, or the distribution moved to wtmpdb<\/td><td>Install the package that provides <code>last<\/code> on your distribution, or skip that section<\/td><\/tr><tr><td><code>$&#039;\\r&#039;: command not found<\/code><\/td><td>Script saved with Windows (CRLF) line endings<\/td><td><code>sed -i &#039;s\/\\r$\/\/&#039; server-health.sh<\/code> or <code>dos2unix server-health.sh<\/code><\/td><\/tr><tr><td><code>Permission denied<\/code> when running <code>.\/server-health.sh<\/code><\/td><td>Execute bit not set, or filesystem mounted <code>noexec<\/code><\/td><td><code>chmod +x server-health.sh<\/code>, or run <code>bash server-health.sh<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Extend the exercise<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A report only helps if someone reads it. A useful next step is to add thresholds and an exit code, so cron or a monitoring agent can react when a limit is crossed:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">status=<span class=\"hljs-number\">0<\/span>\n\ndisk_pct=$(df -P \/ | awk <span class=\"hljs-string\">'NR==2 {gsub(\"%\",\"\",$5); print $5}'<\/span>)\n<span class=\"hljs-keyword\">if<\/span> &#91; <span class=\"hljs-string\">\"$disk_pct\"<\/span> -ge <span class=\"hljs-number\">90<\/span> ]; then\n  <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"WARNING: \/ is ${disk_pct}% full\"<\/span>\n  status=<span class=\"hljs-number\">1<\/span>\nfi\n\nmem_avail_pct=$(free | awk <span class=\"hljs-string\">'\/^Mem:\/ {printf \"%d\", $7*100\/$2}'<\/span>)\n<span class=\"hljs-keyword\">if<\/span> &#91; <span class=\"hljs-string\">\"$mem_avail_pct\"<\/span> -le <span class=\"hljs-number\">10<\/span> ]; then\n  <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"WARNING: only ${mem_avail_pct}% memory available\"<\/span>\n  status=<span class=\"hljs-number\">1<\/span>\nfi\n\n<span class=\"hljs-keyword\">exit<\/span> <span class=\"hljs-string\">\"$status\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li>Accept thresholds as arguments with <code>getopts<\/code>, for example <code>-d 85 -m 15<\/code>.<\/li>\n\n\n<li>Compare the load average with <code>nproc<\/code> and warn when the 5-minute load is higher than the core count.<\/li>\n\n\n<li>Add a <code>--short<\/code> mode that prints only the summary lines and leaves out the process tree and vmstat.<\/li>\n\n\n<li>Print selected values as JSON so another tool can collect them.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently asked questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Why should a server health script run with sudo?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Kernel logs, the journal for other users and the process owning each listening port are visible only to root or to members of groups such as adm or systemd-journal. Without sudo the script still runs, but those sections come back empty or incomplete.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How do I check for OOM killer events on Ubuntu?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Run <code>sudo journalctl -k | grep -i &#039;out of memory&#039;<\/code> for kernel messages, or search \/var\/log\/syslog or \/var\/log\/kern.log. Ubuntu does not use \/var\/log\/messages by default.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Which column of free shows how much memory is really left?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use the available column. It estimates how much memory new applications can use without swapping, because it counts page cache that can be reclaimed. The free column leaves that cache out, so it looks misleadingly low on a healthy server.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why use ss instead of nmap to list open ports?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>ss -tln<\/code> reads the kernel&#039;s socket table directly, is installed on nearly every distribution, and lists listeners on every address. nmap scans from the outside, often is not installed, and against 127.0.0.1 it only finds services bound to loopback or all interfaces.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How can I run the same health script on many servers?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Loop over hostnames and stream the script over SSH, for example <code>for h in web01 web02; do ssh &quot;$h&quot; &#039;bash -s&#039; &lt; server-health.sh &gt; &quot;$h.txt&quot;; done<\/code>. For larger fleets, a configuration management tool such as Ansible handles this better.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related tutorials<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/complete-linux-shell-scripting-guide-and-tutorial-for-linux-admin-and-devops-engineer\/\">Linux Commands and Shell Scripting Reference for Linux Admins and DevOps Engineers<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/what-is-bash-and-use-cases-of-bash\/\">What is Bash and use cases of Bash?<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/interview-questions-answer-sets-shell-programming\/\">Shell and Bash Interview Questions: Practical Answers, Commands, and Script Patterns<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/category\/programming\/shell-script\/\">More Shell Scripting tutorials<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The script above prints a quick health report for a Linux server: uptime, logged-in users, recent logins, disk and memory use, OOM killer events, the busiest processes,&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[454],"tags":[137],"class_list":["post-2356","post","type-post","status-publish","format-standard","hentry","category-shell-script","tag-bash"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2356","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=2356"}],"version-history":[{"count":4,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2356\/revisions"}],"predecessor-version":[{"id":80086,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2356\/revisions\/80086"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=2356"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=2356"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=2356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}