php 设置 Cron 作业以在 24 小时后删除文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16325350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:55:39  来源:igfitidea点击:

Setting Cron job to delete file after 24 hours

phpcroncrontabcron-task

提问by user2290749

I read all the related questions and was unable to understand them. I am using Plesk CPanel to set cron job as it was advised by everyone.

我阅读了所有相关问题,但无法理解它们。我正在使用 Plesk CPanel 来设置 cron 作业,因为每个人都建议这样做。

I want to delete all files from a folder after 24 hours. Assume that I have to delete it after every 2 mins (So I can check its working or not).

我想在 24 小时后删除文件夹中的所有文件。假设我必须在每 2 分钟后删除它(所以我可以检查它是否工作)。

I have two options:

我有两个选择:

  1. Either run a PHP file that deletes all files after 24 hours using a cron job
  2. Use the cron job command `rm` to delete all the files
  1. 运行一个 PHP 文件,使用 cron 作业在 24 小时后删除所有文件
  2. 使用 cron 作业命令 `rm` 删除所有文件

I tried both ways and was unable to get my task completed.

我尝试了两种方法,但无法完成我的任务。

Here is the pic of cpanel scheduled task:

这是cpanel计划任务的图片:

http://i41.tinypic.com/2n0tsfs.png

http://i41.tinypic.com/2n0tsfs.png

I want to delete files from folder var/www/example.com/public/js/complied. All files inside this compliedfolder should be deleted. I don't know which to write in Command textfield.

我想从文件夹中删除文件var/www/example.com/public/js/compliedcomplied应删除此文件夹中的所有文件。我不知道在命令文本字段中写哪个。

Should I use the following command?

我应该使用以下命令吗?

rm /var/www/example.com/public/js/compiled/*.*

Or should I execute a php file?

或者我应该执行一个php文件?

env php -q/var/www/example.com/public/js/cron.php

The source code of this Cron.php is:

这个 Cron.php 的源代码是:

<?php
$dir = "compiled"; // directory name



foreach (scandir($dir) as $item) {
    if ($item == '.' || $item == '..')
        continue;

        unlink($dir.DIRECTORY_SEPARATOR.$item);
        echo "All files deleted";
    }   
//rmdir($dir);

?>

I have tested this code and it works fine.

我已经测试了这段代码,它工作正常。

Thanks in advance.

提前致谢。

回答by MrCleanX

I use this in a shell script...

我在shell脚本中使用它...

find /some/path -mtime +7 -exec rm {} \; # delete > 7 days old

回答by JiNexus

If you have access to your Server or SSH, you can simply add it to your crontab.

如果您有权访问您的服务器或 SSH,您只需将其添加到您的 crontab。

In your SSH just type

在你的 SSH 中输入

crontab -e

you will see a list of cron jobs on it, just append this line of code to your cronjob:

您将在其上看到一个 cron 作业列表,只需将这行代码附​​加到您的 cronjob 中:

0 10 * * * rm -rf /var/www/example.com/public/js/compiled/*

The code above means that every 10am in the morning you are removing all the files in the path you provide. Please refer to this link for more info about Cron: http://en.wikipedia.org/wiki/Cron

上面的代码意味着每早上 10 点您都会删除您提供的路径中的所有文件。有关 Cron 的更多信息,请参阅此链接:http: //en.wikipedia.org/wiki/Cron

回答by Shuhad zaman

it worked for me to delete in once a day

每天删除一次对我有用

0 0 * * * rm -rf /home/user/public_html/folder

if you want to remove everything in this folder, but leave the folder itself:

如果要删除此文件夹中的所有内容,但保留文件夹本身:

0 0 * * * rm -f /home/user/public_html/folder/*

回答by mogul

To optimize MrCleanX' solution a bit, use xargs:

要稍微优化 MrCleanX 的解决方案,请使用xargs

find /some/path -type f -mtime +7 -print0 | xargs -0 --no-run-if-empty rm

Instead of calling rm for each file to delete, xargspacks many files together to a single call to rm

不是为每个要删除的文件调用 rm,而是xargs将许多文件打包到一个调用中rm

The -print0and -0stuff is to make both findand xargsusing NULL terminated strings, which is necessary to handle file names with space and other interesting chars in their names.

-print0-0的东西是使两者findxargs用NULL结尾的字符串,这是必要的,在他们的名字空间和其他有趣的字符来处理文件名。