Report this

What is the reason for this report?

Cleaning /var/lib/php/sessions - 100% inodes usage - Ubuntu 16.04

Posted on March 23, 2021

After having issues doing anything on one of my Ubuntu 16.04 droplets due to disc usage errors, I found that I was running out of inodes and there are about 6.2m of them in /var/lib/php/sessions.

I recently updated from PHP 5 to PHP 7, I think this has caused the issue.

My questions are:

What’s the best way to clean sessions from this folder to free up inodes?

How can I prevent this from happening again?

I have this

09,39 *     * * *     root   [ -x /usr/lib/php/sessionclean ] && if [ ! -d /run/systemd/system ]; then /usr/lib/php/sessionclean; fi

in etc/cron.d/php but it doesn’t appear to be working.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi there,

I believe that this is a good approach.

What happens if you try to run the script manually:

/usr/lib/php/sessionclean

Do you get any errors?

Regards, Bobby

Heya,

If the problem persist and it’s not related only to the php session files, you can expand the search and check the disk space usage in other directories as well.

You can check which folders are using the most space on the Droplet by using the disk utilization command, du:

  1. du -h --max-depth=1 /

Check our tutorial on How to fix disk space issues here:

https://docs.digitalocean.com/support/how-do-i-fix-disk-space-issues-on-my-droplet/

There is also an ncurses interface for du, appropriately called ncdu, that you can install:

  1. sudo apt install ncdu

This will graphically represent your disk usage:

  1. ncdu
Output--- /root ----------------------------------------------------------------------
    8.0KiB [##########] /.ssh
    4.0KiB [#####     ] /.cache
    4.0KiB [#####     ]  .bashrc
    4.0KiB [#####     ]  .profile
    4.0KiB [#####     ]  .bash_history

You can step through the filesystem by using the up and down arrows and pressing Enter on any directory entry.

Hope that this helps!

To clean the /var/lib/php/sessions folder and prevent future inode exhaustion:

1. Clean Up Sessions to Free Inodes

You can safely remove stale session files to free up inodes. Here’s how:

  1. Manually Delete Old Session Files Use find to delete session files older than a certain age, such as 24 hours:
sudo find /var/lib/php/sessions -type f -mtime +1 -delete
  • This command deletes files modified more than 1 day ago. Adjust the +1 parameter based on your needs.

  • Clear All Session Files If you want to clear all files regardless of age, you can run:

sudo rm -rf /var/lib/php/sessions/*
  1. e cautious with this, as it will log out users with active sessions.

2. Ensure the sessionclean Script is Working

Your cron job appears to call the sessionclean script. To troubleshoot why it isn’t working:

  1. Test the sessionclean Script Manually Run the script manually to ensure it works:
sudo /usr/lib/php/sessionclean
  • If there are errors, investigate and resolve them.

  • Check session.gc_maxlifetime The sessionclean script relies on session.gc_maxlifetime to determine which files are considered stale. Ensure this value matches your intended cleanup policy:

sudo grep session.gc_maxlifetime /etc/php/7.*/cli/php.ini

Update it as needed (e.g., session.gc_maxlifetime = 1440 for 24 minutes) in both cli and fpm configurations:

sudo nano /etc/php/7.x/fpm/php.ini
sudo nano /etc/php/7.x/cli/php.ini
  1. Debug the Cron Job

    • Ensure the cron service is running:
sudo systemctl status cron
-   Check the cron logs for errors:
sudo grep CRON /var/log/syslog
-   Verify the path to `sessionclean` is correct in the cron file.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.