Knowledgebase

How to Add Swap Space and Monitor Your Server (CPU, RAM, Disk, Load)

To add swap space on an Ubuntu server, create a swap file with fallocate, secure it with chmod 600, format it with mkswap, enable it with swapon, and add it to /etc/fstab so it survives reboots. To monitor the server, use uptime for load, free -h and htop for RAM and CPU, df -h and du for disk, and iostat, vmstat and journalctl for deeper checks. Add an external uptime monitor and a cron script that emails you when a disk is nearly full. This guide applies to Cloud VPS and dedicated servers with root SSH access running Ubuntu 24.04 or 22.04.

Prerequisites

  • A Cloud VPS or dedicated server running Ubuntu 24.04 or 22.04.
  • SSH access as root or as a user with sudo.
  • At least a few GB of free disk space for the swap file.
  • An email address for alerts.
Shared cPanel hosting accounts cannot add swap or change kernel settings, because CloudLinux manages resources per account. If you regularly hit memory limits there, open a support ticket to discuss a plan upgrade or a move to a VPS.

1. When does a VPS need swap?

Swap is disk space that the kernel uses when RAM runs out. It is much slower than RAM, but it stops short memory spikes from triggering the OOM (out-of-memory) killer. Examples of such spikes are a composer install, a database backup or a heavy n8n workflow. Small VPS plans (1–4 GB RAM) almost always benefit from swap. Ubuntu cloud images often ship with none. Check what you have:

sudo swapon --show
free -h

If swapon --show prints nothing, the server has no swap. A practical size is 1–2 GB for servers with up to 2 GB RAM, and 2–4 GB for larger servers. If the server swaps constantly, it needs more RAM, not more swap.

2. How do I create a swap file on Ubuntu?

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

If swapon reports that the file has holes, or fallocate fails on your filesystem, delete the file. Then create it with dd instead and repeat the chmod, mkswap and swapon steps:

sudo rm /swapfile
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress

Tune swappiness

vm.swappiness controls how eagerly the kernel moves memory to swap. The Ubuntu default is 60. On servers, a value of 10 keeps active data in RAM and uses swap mainly as a safety net. Apply the new value and make it permanent:

sudo sysctl vm.swappiness=10
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf
sudo sysctl --system

3. How do I check CPU, RAM and load average?

uptime
nproc

uptime prints three load averages, for the last 1, 5 and 15 minutes. Load is the number of processes running or waiting for CPU or disk. Compare it with the core count from nproc. On a 2-core server, a load around 2 means the CPU is fully busy. A sustained load of 4 or more means processes are queuing.

sudo apt update && sudo apt install -y htop sysstat
htop
free -h

In htop, press F6 to sort by PERCENT_MEM or PERCENT_CPU, and press q to quit. In free -h, look at the available column, not free. Linux uses spare RAM for cache, and it releases that cache when applications need it.

Find what is eating memory

ps aux --sort=-%mem | head -n 10
docker stats --no-stream

Use docker stats when your apps run in containers, for example on n8n hosting.

Check disk I/O and memory pressure

vmstat 2 5
iostat -xz 2 3

These columns tell you where the bottleneck is:

  • In vmstat, steady non-zero si/so values mean the server is actively swapping and needs more RAM.
  • A high wa value in vmstat means the CPU is waiting on disk.
  • In iostat, %util close to 100 marks a saturated disk.

4. How do I see what is using disk space?

df -h
df -i
sudo du -xh --max-depth=1 / 2>/dev/null | sort -h | tail -n 15
sudo du -sh /var/log/* | sort -h | tail

Use the output like this:

  • df -h shows usage for each filesystem.
  • df -i shows inodes. You can run out of inodes even when free space remains, typically because of millions of small cache or session files.
  • Run du repeatedly on the largest directory until you find the culprit.

5. How do I check service logs with journalctl?

systemctl status nginx
sudo journalctl -u nginx --since "1 hour ago"
sudo journalctl -p err -b
sudo journalctl -f
sudo journalctl --disk-usage

These commands do the following:

  • -u filters by service. Replace nginx with your own service, such as mysql, php8.3-fpm or docker.
  • -p err -b shows errors since the last boot.
  • -f follows new log lines live.
  • --disk-usage shows how much space the journal takes.

6. How do I get alerted when my server or website goes down?

Your server cannot report its own outage, so use an external monitor. UptimeRobot, or a similar service, can check your site over HTTP(S) on its free plan, typically every 5 minutes, and email you when it fails.

  • Create one HTTP(S) monitor for https://example.com.
  • Optionally, add a keyword monitor that checks for text on the page, which catches error pages that still return 200.
  • Optionally, add a ping monitor for 203.0.113.10.

For a deeper review of performance, resource bottlenecks and configuration, Cloudesta offers a Server Monitoring Analysis service. Open a support ticket to request it.

7. How do I get email alerts when the disk is almost full?

First, install a mail tool. When asked, choose "Internet Site" and enter your server hostname:

sudo apt install -y mailutils
Mail sent directly from a VPS often lands in spam unless the IP has reverse DNS and your domain has SPF. Reverse DNS (rDNS) is set by Cloudesta, so request it by support ticket. A more reliable option is to relay through an authenticated SMTP mailbox, such as business email.

Next, create the script /usr/local/bin/disk-alert.sh:

#!/bin/bash
THRESHOLD=85
EMAIL="you@example.com"
HOST=$(hostname)

df -P -x tmpfs -x devtmpfs -x squashfs -x overlay | awk 'NR>1 {gsub("%","",$5); print $5, $6}' |
while read -r USAGE MOUNT; do
  if [ "$USAGE" -ge "$THRESHOLD" ]; then
    echo "Disk usage on $HOST: $MOUNT is at ${USAGE}%" | mail -s "Disk alert: $HOST $MOUNT ${USAGE}%" "$EMAIL"
  fi
done

Make the script executable and schedule it to run hourly:

sudo chmod 755 /usr/local/bin/disk-alert.sh
sudo crontab -e

In the editor, add this line:

0 * * * * /usr/local/bin/disk-alert.sh

Verify it works

  • sudo swapon --show lists /swapfile, and free -h shows swap.
  • cat /proc/sys/vm/swappiness returns 10.
  • After sudo reboot, swap is still active.
  • Temporarily set THRESHOLD=1 and run sudo /usr/local/bin/disk-alert.sh. An email should arrive. Check your spam folder and sudo tail /var/log/mail.log. Then set the threshold back to 85.
  • Your external monitor shows the site as "up".

Common problems

Disk full because of logs

Shrink the journal, empty an oversized log file in place, and find deleted files that a running process still holds open:

sudo journalctl --vacuum-size=200M
sudo truncate -s 0 /var/log/example.log
sudo lsof +L1

Restart the service that owns any file listed by lsof +L1 to release the space. To keep the journal small permanently, set SystemMaxUse=200M in /etc/systemd/journald.conf and restart systemd-journald.

Services die randomly (OOM killer)

Confirm that the kernel killed them:

sudo journalctl -k | grep -iE "out of memory|killed process"

If you see matches, add swap and reduce worker counts (PHP-FPM pm.max_children, MySQL buffers). If memory is still short, open a ticket to upgrade the plan.

High load from bots or crawlers

Find the IP addresses sending the most requests:

sudo awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head
sudo ufw deny from 198.51.100.25

Block abusive IPs with ufw as shown. Rate-limit or block aggressive user agents in your web server configuration.

High load from a cron job loop

Run ps -ef --forest to look for many copies of the same script, and check the schedules with crontab -l and sudo crontab -l. To stop runs from overlapping, wrap the job in flock:

*/5 * * * * /usr/bin/flock -n /tmp/job.lock /usr/local/bin/job.sh

Swap missing after reboot

Check /etc/fstab for typos and run sudo swapon -a. If an error appears, fix that line. Never leave a broken fstab entry in place.

Frequently asked questions

How much swap should I add to my VPS?

Use 1–2 GB for servers with up to 2 GB RAM and 2–4 GB for larger ones. Swap only absorbs spikes, so constant swapping means you need more RAM.

Can I add swap on shared cPanel hosting?

No. Memory on shared hosting is managed by CloudLinux per account. Open a support ticket to upgrade your plan if you keep hitting limits.

Is a load average of 3 bad?

It depends on the core count. On a 4-core server it is fine, but on a 1-core server processes are queuing and you should look for the cause in htop.

Need help? Open a ticket at https://cloudesta.com/submitticket.php and include the output of uptime, free -h and df -h.

Was this answer helpful?

0 Users Found This Useful

Powered by WHMCompleteSolution