3

I've been scanning forums for days/weeks now on this issue, but I have a raspberry pi that I'm trying to setup a cleaning script on. The script works well manually, but not via cron (cron shows the job running, but nothing in errors).

The cron entry (and execution) is:

Jul  8 07:02:01 E1 CRON[16785]: (root) CMD (/usr/bin/php /extHDD/subdir/subdir/subdir/cleanbackups.php >>/extHDD/subdir/subdir/subdir/cleanbackups.log 2>&1)

The "cleanbackups.log" only shows blank lines when the script runs.

The script is designed like this (set to expire older than 2 days and 1hr):

cat cleanbackups.php
#!/bin/bash
export PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/extHDD/subdir/subdir/subdir

<?php
    $files = glob("backup-*_xyz.tar.gz");
    foreach($files as $file) {
        if(is_file($file)
        && time() - filemtime($file) >= 2*1*0*0) { // 7 days
            unlink($file);
        }
    }
?>

The script runs with:

- php cleanbackups.php
- php -f cleanbackups.php
- php -q cleanbackups.php
- /usr/bin/php cleanbackups.php
- /usr/bin/php -f cleanbackups.php
- /usr/bin/php -q cleanbackups.php

This cron job is being modified on the root account, but using a standard user doesn't change the result.

Also, I already used chmod +X /path/to/cleanbackups.php, with no change.

Can anyone assist me in this? It's causing me severe anxiety :)

2
  • Is your directory structure really .../subdir/subdir/subdir? Commented Jul 8, 2017 at 13:34
  • No, it is not. It shouldn't be relevant as it's an external drive, and all directories are non-standard for the OS I'm on. They're just names. Why? Commented Jul 9, 2017 at 18:04

2 Answers 2

1

A couple of things:

  1. put some echo lines in your script so you see the command you are trying to run, and the value of any variables when cron runs.

  2. How about this as an alternative:

    # edit root's crontab, not the pi user's
    sudo crontab -e
    # insert the following line, then save 
    0 18  * * *  find /extHDD/subdir/subdir/subdir/backup-*_xyz.tar.gz -mtime +2 -exec rm {} \; 
    # con then runs the command every day at 18:00 
    # the command deletes files named 
    # /extHDD/subdir/subdir/subdir/backup-*_xyz.tar.gz 
    # that are more than two days old 
    # if 2 days and 1 hour is important, then look at -mmin
    
2
  • I am able to use find command with the switches you gave to find the old files, but the cron job doesn't remove them. Commented Jul 11, 2017 at 20:16
  • My apologies, I forgot a space after "rm {}". Your solution worked best. Here is my final cron job (before looking into -mmin): 20 4 * * * find /mnt/ext/../../backup-*_xyz.tar.gz -mtime +2 -exec rm {} \; Commented Jul 11, 2017 at 20:30
-1

My solution is more of a way to troubleshoot. Try to run your scripts in a cron-like environment.

The answer in https://stackoverflow.com/questions/2135478/how-to-simulate-the-environment-cron-executes-a-script-with will explain how to get a clean environment to test your scripts with.

1
  • Are you suggesting I try to run the command in the CLI like I've already done to get a different result? I can already run the script with success manually. The article you suggested seems to want to run the command "env -i your_command", which would just be "env -i php /extHDD/../../../cleanbackups.php", which wouldn't be much different, I'd assume? Commented Jul 11, 2017 at 20:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.