skip to Main Content

I’m managing a Plesk Linux webserver with multiple website-subscriptions that all run in FastCGI. The setup requires me to have a session folder for each subscription (PHP writes session data to that folder). The problem is that over time, these session folders get full and are not automatically cleared.

My hosting provider therefore created the following script, that would run weekly in a cron job.

#!/bin/bash

mapfile -t domains < /home/cmadmin/domains

for i in "${domains[@]}"
do
 #echo "Deleting $i Session folder content"
 find /var/www/vhosts/$i/session/* -mtime +7 -type f -delete
done

The objective of the script is to loop over each subscription on my server, to go into the specific session folder for that subscription and there to remove all files older than 7 days.

This script seems to work fine, but for some reason, when it reaches the subscription called groepsmanager-rspo.com everything inside that folder gets deleted. So even files outside the session folder, like /var/www/hosts/groepsmanager-rspo.com/httpdocs/ and /var/www/hosts/groepsmanager-rspo.com/logs are deleted.

The very strange thing is that all other subscriptions are completely fine. Upon investigating their contents, indeed only files younger than 7 days remain.

At first we thought the dash (-) in the folder name might cause problems, but this is not the case, since other folder names with a dash are fine. Also, there are no files called session in folders outside the global session folder.

Does anyone have a clue why this particular subscription keeps getting removed? Are there any do’s or dont’s to keep in mind?

Thanks in advance.

2

Answers


  1. You should always quote variables and parameters; otherwise any special characters can be misinterpreted by the shell. As well, the first argument to find is a path; there’s no need for a wildcard. If I were doing this, I’d try something like this:

    #!/bin/bash
    
    mapfile -t domains < /home/cmadmin/domains
    
    for i in "${domains[@]}"; do
        #echo "Deleting $i Session folder content"
        cd "/var/www/vhosts/$i/session/" || continue
        find . -mtime +7 -type f -delete
    done
    
    Login or Signup to reply.
  2. I bet you have mixed line-endings in the file /home/cmadmin/domains. I.e. all the other lines have Unix line-ending (LF) but the problematic line has Windows line-ending (CRLF). To verify this, you can run file /home/cmadmin/domains.

    To fix it, run dos2unix /home/cmadmin/domains. You should still pay attention to quoting as @miken32 suggested. I warmly recommend using shellcheck that makes it easy to spot quoting problems.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search