deleteDirectory not working as Cron job

192 Views Asked by At

I have a job which I want cron to run at given interval , this removes some old entries from my db and then removes the files that were uploaded with the db entry.

If I run the task from the terminal it works fine, (removing both db entries and the files uploaded). But when I leave the task to cron, it does remove the entries in the db , but it doesn’t remove the uploaded folders in my public directory.

the code that removes the files looks like this

$machtiging = File::files('icec/'.$icecconsult->accesstoken.'            /machtiging');

if(count($machtiging) > 0){
    File::deleteDirectory(public_path() . '/icec/'.$icecconsult->accesstoken);
}

so it looks if there there, if so , delete them , but this just doesn’t work , ive tried putting the cron job run as root , www-data , and my user , all with same result . files and folder permission I have set them to 777 to be sure, but this doesn’t seem to be the problem.

Ive also tried adding shell=/bin/bash but that didnt do the trick either

Any help on solving this issue would be much appreciated

Update the crobline looks like this

* * * * * /bin/bash /home/ice/verlopenicec.sh >> /tmp/output 2&>1

also tried

* * * * * /home/ice/verlopenicec.sh >> /tmp/output 2&>1

and

* * * * * /usr/bin/php /var/www/wemedic/artisan verwijder-verlopen-icec-consult 1>> /dev/null 2>&1

All seem to run . its just it doesnt delete or move the directory it needs to trying to get some debug data , but nothing is showing up

the verlopenicec.sh script itself looks is just a say reference to the original script that laravel should run . thouth might be handy to make a script to test why laravel aint deleting the directory.

script looks like this

#!/bin/bash
SHELL=/bin/bash
USER=ice
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

/usr/bin/php /var/www/wemedic/artisan verwijder-verlopen-icec-consult

wich runs a laravel command that looks like this

    $icecconsult = Icecconsult::where('id', '=', $consult_id)->firstOrFail();
    $icecconsult->expire = Carbon::now();
    $icecconsult->status = 'Gesloten';
    $icecconsult->save();

    $icec = Icec::where('id', '=', $icecconsult->icec_id)->firstOrFail();
    $icec->delete();

    $machtiging = File::files('icec/'.$icecconsult->accesstoken.'/machtiging');

    if(count($machtiging) > 0){
//            File::deleteDirectory(public_path() . '/icec/'.$icecconsult->accesstoken);
        $move = 'mv /var/www/wemedic/public/icec/'.$icecconsult->accesstoken.'  /tmp' ;
        shell_exec('SHELL=/bin/bash');
        shell_exec('PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games');

        shell_exec($move);
//             File::deleteDirectory('/var/www/wemedic/public/icec/'.$icecconsult->accesstoken);
    }


    return;

(ive commented out the delete function and tried to move it to the temp directory , but having the same result with moving or deleting. both work If I directly run it , but not when cron runs it . Cron does run the task , cause I can see it beeing fired in the /var/log/syslog and the database entru does get changed ) Ive tryied deleting , then moving it to the temp folder, both work if I run them directly , but none work when I leave it to cron/ laravel cron scheduler

If also tried to get a response (tru/false ) from the delete function, but when I try to save that to the db to see it , the function seems to not execute.

dd($machtiging) returns an array like below, showing the files in the folder , after knowing there are files in the folder, it should go ahead and delete the complete folder allong with any files/sub directories located in it

array:1 [▼
0 => "icec/a89ce4c9010e0a745308b29782b5eeae/machtiging/machtiging.pdf"
]

Thanks for you help

1

There are 1 best solutions below

2
On

Try with this crontab :

*/1 * * * * ice /bin/bash /home/ice/verlopenicec.sh >> /tmp/output.log

Change your bash script to :

#!/bin/bash
moment="`/bin/date +%y_%m_%d`"
echo "--- The script has been executed on $moment ---"
/usr/bin/php /var/www/wemedic/artisan verwijder-verlopen-icec-consult

It should work better, but if not, could you paste here the content of your generated /tmp/output.log ?