Showing posts with label codeigniter. Show all posts
Showing posts with label codeigniter. Show all posts

Monday 22 January 2024

CodeIgniter generating too many sessions files | Resolved and remove session in cron job

 

Normal Approach


First setup you have to know where your php.ini file is.


-For this you have to know using phpinfo(); function to know where the php.ini path is.

Here is a screenshot for where the php.ini file path you get.






Find below variable and change it

 

session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 7200










In the codeigniter config.php  file  change this.

In this create session folder with writable


$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'mysession';
$config['sess_expiration'] = (60 * 60);
$config['sess_save_path'] = FCPATH . "sessions";
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = TRUE;



First create folder using mkdir command  : mkdir sessions 

After change this folder user permission  : sudo chown -R www-data:www-data sessions/


In this if you have set $config['sess_save_path'] = null then it store in
/var/lib/php/sessions folder.




This solution sometimes works or sometimes not.



Emergency approach to remove session file



1)sudo find /var/lib/php/sessions/. -type f|wc -l


  -this command give you count of your current session


2)cd /var/lib/php/sessions and fire du -sh then it's
Give size of you session file
But you have to go for cd /var/lib/php/sessions you to become
Root user for this you have to use sudo su


1)sudo su
2)cd /var/lib/php/sessions
3)du -sh



After fire below command for remove all session in projects

But please be aware if you remove the session folder so that time web site session is not working.


Follow step by step Command


1)sudo rm -rf /var/lib/php/sessions
  -remove session folder


2)
cd /var/lib/php
mkdir sessions


3)sudo chmod -R 1733 sessions/


This is useful when you have so many session files and because the

server is full at that time you can clear the session's folder size.



Crontab Approach  



In this we can remove session file using name or all file 


So  in this we have create both



1)Remove all session file with cronjob


  

1.1 this command use current folder session count
 
    sudo ionice -c 2 nice -n 19  find /var/lib/php/sessions  -type f  | wc -l


  

1.2 this command use for count session file current time to 72 hours

sudo ionice -c 2 nice -n 19  find /var/lib/php/sessions  -type f -cmin +4320 | wc -l


In this we have set -cmin +4320 this means current time 4320 before file count

You can change as per your requirements.

ionice -c 2 nice -n 19 This is useful for checking server memory and priority 

We have set server best effort and nice -n 19 last priority


This command description
  The command you provided is a combination of several commands in a Unix-like operating system. Let's break it down:

The command you provided is a combination of several commands in a Unix-like operating system. Let's break it down:

  • sudo: This command is used to execute subsequent commands with superuser

  • privileges.

  • ionice -c 2: This sets the I/O scheduling class for the command. In this case, it sets

  • the I/O class to 2, which corresponds to the "idle" class. This means that the command

  • will have low I/O priority.

  • nice -n 19: This sets the process priority for the command. A lower priority value

  • (more negative) means higher priority, but in this case, it sets the process priority to the

  • lowest possible value, 19. This means the command will have very low CPU priority.


  • find /var/lib/php/sessions -type f -cmin +4320: This is the find command that

  • searches for files (-type f) in the specified directory (/var/lib/php/sessions) that

  • were changed more than 4320 minutes ago (-cmin +4320).

  • |: This is a pipe symbol, which is used to pass the output of one command as the input

  • to another.

  • wc -l: This command is used to count the number of lines in the input it receives. In

  • this case, it counts the number of files found by the find command.

So, the entire command is searching for files in the specified directory that were

changed more than 4320 minutes ago, and then it counts the number of such files.

Keep in mind that running commands with elevated privileges (sudo) and modifying

system files or settings can have significant consequences. Make sure you understand

the commands and their implications before executing them.





2)Remove all session with timeout and without time out


  1. sudo su

  2. crontab -e

      3)  set cron using below


  05 05 * * * ionice -c 2 nice -n 19 find /var/lib/php/sessions -type f -cmin +4320 -exec sudo rm {} \;



This command run on 5:05 am and remove session file before 72hour (4320/60 = 72)


Some time so many session file so at that time not remove session file 24 hour so 

At time some time override our cron for that i have suggest you can set timeout 

In cron so after that time cron is automatically closed.

So i have run cron for 20h duration please check below example


05 05 * * * /usr/bin/timeout 20h sudo ionice -c 2 nice -n 19 find /var/lib/php/sessions -type f -cmin +4320 -exec sudo rm {} \;


/usr/bin/timeout 20h - > you can set timeout in minutes , hours and seconds



For example you have to set for 5 minutes then you can set link this

/usr/bin/timeout 5m


3)Remove all session with timeout and without time out by name


  1. sudo su

  2. crontab -e

      3)  set cron using below

 

1)without timeout
05 05 * * * ionice -c 2 nice -n 19 find /var/lib/php/sessions -type f -name 'session_name*' -cmin +4320 -exec sudo rm {} \;
-type f -name 'session_name*' -> this is find file from session folder and remove particular this file



2) timeout

05 05 * * * /usr/bin/timeout 20h sudo ionice -c 2 nice -n 19 find /var/lib/php/sessions -type f -name 'session_name*' -cmin +4320 -exec sudo rm {} \;


/usr/bin/timeout 20h - > you can set timeout in minutes , hours and seconds