在cron作业执行的PHP脚本 [英] Execute PHP script in cron job

查看:382
本文介绍了在cron作业执行的PHP脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的centos6服务器。我想在cron作业作为Apache用户来执行PHP脚本,但遗憾的是它不工作。

In our centos6 server. I would like to execute a php script in cron job as apache user but unfortunately it does not work.

下面是crontab中的版本(的crontab -e -uapache)

here is the edition of crontab (crontab -uapache -e)

24 17 * * * php /opt/test.php

和这里是test.php的文件的源$ C ​​$ C它与阿帕奇用户作为所有者的罚款。

and here is the source code of "test.php" file which works fine with "apache" user as owner.

<?php exec( 'touch /opt/test/test.txt');?>

我尝试用PHP的完整路径,以取代PHP(在/ usr /本地/ PHP /斌/ PHP),而且它不能正常工作

I try to replace php with full path of php (/usr/local/php/bin/php) but also it doesn't work

在此先感谢,
请帮我

Thanks in advance, Please Help me

推荐答案

cron是在linux / Unix类电脑操作系统基于时间的调度服务。用于调度cron作业命令必须定期执行。
您可以设置命令或脚本,这将反复在设定的时间运行。 cron是在Linux或UNIX类操作系统的最有用的工具之一。 cron服务(守护进程)在后台运行,并不断检查的 / etc / crontab中的文件,的/ etc / cron的。的/ *目录。它还检查的的/ var /线轴/ cron的/ 的目录。

Automated Tasks: Cron

Cron is a time-based scheduling service in Linux / Unix-like computer operating systems. Cron job are used to schedule commands to be executed periodically. You can setup commands or scripts, which will repeatedly run at a set time. Cron is one of the most useful tool in Linux or UNIX like operating systems. The cron service (daemon) runs in the background and constantly checks the /etc/crontab file, /etc/cron./* directories. It also checks the /var/spool/cron/ directory.

在下面的例子中, crontab命令的下面显示将自动激活后台任务每隔十分钟:

In the following example, the crontab command shown below will activate the cron tasks automatically every ten minutes:

*/10 * * * * /usr/bin/php /opt/test.php

在上面的示例中,* / 10 * * * *重新presents任务的时候应该发生。第一个数字再presents 的 - 在这种情况下,每一个十分。其他数字分别重新present,小时星期的。

In the above sample, the */10 * * * * represents when the task should happen. The first figure represents minutes – in this case, on every "ten" minute. The other figures represent, respectively, hour, day, month and day of the week.

* 是通配符,每一次的意思

* is a wildcard, meaning "every time".

先从找出你的 PHP 的通过在命令行中输入的二进制:

Start with finding out your PHP binary by typing in command line:

whereis php

输出应该是这样的:

The output should be something like:

PHP:在/ usr / bin中/ PHP的/etc/php.ini中/etc/php.d在/ usr / lib64下/ PHP / usr / include目录/ PHP的/ usr /共享/ PHP的/ usr / share / man中/ man1目录/ php.1.gz

php: /usr/bin/php /etc/php.ini /etc/php.d /usr/lib64/php /usr/include/php /usr/share/php /usr/share/man/man1/php.1.gz

在您的命令正确指定的完整路径。

Specify correctly the full path in your command.

crontab -e

要看到你在得到的的crontab

要从vim编辑器不保存退出只需点击:

To exit from vim editor without saving just click:

Shift+:

然后键入!Q

这篇关于在cron作业执行的PHP脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆