在 cron 作业中执行 PHP 脚本 [英] Execute PHP script in cron job
问题描述
在我们的 centos6 服务器中.我想以 apache 用户的身份在 cron 作业中执行一个 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 -uapache -e)
here is the edition of crontab (crontab -uapache -e)
24 17 * * * php /opt/test.php
这里是test.php"文件的源代码,它可以在apache"用户作为所有者的情况下正常工作.
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 的完整路径 (/usr/local/php/bin/php) 替换 php 但它也不起作用
I try to replace php with full path of php (/usr/local/php/bin/php) but also it doesn't work
提前致谢,请帮帮我
推荐答案
自动化任务:Cron
Cron 是 Linux/类 Unix 计算机操作系统中基于时间的调度服务.Cron 作业用于调度要定期执行的命令.您可以设置命令或脚本,它们将在设定的时间重复运行.Cron 是 Linux 或类 UNIX 操作系统中最有用的工具之一.cron 服务(守护进程)在后台运行,不断检查 /etc/crontab 文件、/etc/cron./* 目录.它还检查 /var/spool/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 命令将每十分钟自动激活 cron 任务:
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 * * * * 表示任务应该发生的时间.第一个数字代表分钟——在这种情况下,每十"次分钟.其他数字分别代表小时、天、月和星期几.
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
输出应该是这样的:
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
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 中得到了什么.
To see what you got in crontab.
要退出 vim 编辑器而不保存,只需单击:
To exit from vim editor without saving just click:
Shift+:
然后输入q!
这篇关于在 cron 作业中执行 PHP 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!