In our centos6 server. I would like to execute a php script in cron job as apache user but unfortunately it does not work.
在我们的centos6服务器中。我想在cron job中执行一个php脚本作为apache用户,但不幸的是它不起作用。
here is the edition of crontab (crontab -uapache -e)
这是crontab的版本(crontab -uapache -e)
24 17 * * * php /opt/test.php
and here is the source code of "test.php" file which works fine with "apache" user as owner.
这里是“test.php”文件的源代码,它与“apache”用户作为所有者一起正常工作。
<?php exec( 'touch /opt/test/test.txt');?>
I try to replace php with full path of php (/usr/local/php/bin/php) but also it doesn't work
我尝试用php的完整路径替换php(/ usr / local / php / bin / php),但它也无法正常工作
Thanks in advance, Please Help me
在此先感谢,请帮助我
3 个解决方案
#1
52
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.
Cron是一种基于时间的调度服务,适用于Linux / Unix类计算机操作系统。 Cron作业用于安排定期执行的命令。您可以设置命令或脚本,这些命令或脚本将在设定的时间重复运行。 Cron是Linux或UNIX操作系统中最有用的工具之一。 cron服务(守护程序)在后台运行并不断检查/ etc / crontab文件/etc/cron./*目录。它还检查/ var / spool / cron /目录。
Configuring Cron Tasks
In the following example, the crontab command shown below will activate the cron tasks automatically every ten minutes:
在以下示例中,下面显示的crontab命令将每十分钟自动激活一次cron任务:
*/10 * * * * /usr/bin/php /opt/test.php
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.
在上面的示例中,* / 10 * * * *表示任务应该发生的时间。第一个数字代表分钟 - 在这种情况下,每隔“十”分钟。其他数字分别代表小时,日,月和星期几。
*
is a wildcard, meaning "every time".
*是一个通配符,意思是“每次”。
Start with finding out your PHP binary by typing in command line:
首先输入命令行找出您的PHP二进制文件:
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/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 。广州
Specify correctly the full path in your command.
正确指定命令中的完整路径。
Type the following command to enter cronjob:
crontab -e
To see what you got in crontab.
看看你在crontab中得到了什么。
EDIT 1:
To exit from vim editor without saving just click:
要退出vim编辑器而不保存,只需单击:
Shift+:
And then type q!
然后输入q!
#2
8
I had the same problem... I had to run it as a user.
我遇到了同样的问题......我不得不以用户身份运行它。
00 * * * * root /usr/bin/php /var/virtual/hostname.nz/public_html/cronjob.php
#3
1
You may need to run the cron job as a user with permissions to execute the PHP script. Try executing the cron job as root, using the command runuser
(man runuser
). Or create a system crontable and run the PHP script as an authorized user, as @Philip described.
您可能需要以具有执行PHP脚本权限的用户身份运行cron作业。尝试使用runuser(man runuser)命令以root身份执行cron作业。或者创建一个系统crontable并以授权用户身份运行PHP脚本,如@Philip所述。
I provide a detailed answer how to use cron in this * post.
我在这个*帖子中提供了如何使用cron的详细解答。
How to write a cron that will run a script every day at midnight?
如何编写一个每天午夜运行脚本的cron?
#1
52
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.
Cron是一种基于时间的调度服务,适用于Linux / Unix类计算机操作系统。 Cron作业用于安排定期执行的命令。您可以设置命令或脚本,这些命令或脚本将在设定的时间重复运行。 Cron是Linux或UNIX操作系统中最有用的工具之一。 cron服务(守护程序)在后台运行并不断检查/ etc / crontab文件/etc/cron./*目录。它还检查/ var / spool / cron /目录。
Configuring Cron Tasks
In the following example, the crontab command shown below will activate the cron tasks automatically every ten minutes:
在以下示例中,下面显示的crontab命令将每十分钟自动激活一次cron任务:
*/10 * * * * /usr/bin/php /opt/test.php
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.
在上面的示例中,* / 10 * * * *表示任务应该发生的时间。第一个数字代表分钟 - 在这种情况下,每隔“十”分钟。其他数字分别代表小时,日,月和星期几。
*
is a wildcard, meaning "every time".
*是一个通配符,意思是“每次”。
Start with finding out your PHP binary by typing in command line:
首先输入命令行找出您的PHP二进制文件:
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/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 。广州
Specify correctly the full path in your command.
正确指定命令中的完整路径。
Type the following command to enter cronjob:
crontab -e
To see what you got in crontab.
看看你在crontab中得到了什么。
EDIT 1:
To exit from vim editor without saving just click:
要退出vim编辑器而不保存,只需单击:
Shift+:
And then type q!
然后输入q!
#2
8
I had the same problem... I had to run it as a user.
我遇到了同样的问题......我不得不以用户身份运行它。
00 * * * * root /usr/bin/php /var/virtual/hostname.nz/public_html/cronjob.php
#3
1
You may need to run the cron job as a user with permissions to execute the PHP script. Try executing the cron job as root, using the command runuser
(man runuser
). Or create a system crontable and run the PHP script as an authorized user, as @Philip described.
您可能需要以具有执行PHP脚本权限的用户身份运行cron作业。尝试使用runuser(man runuser)命令以root身份执行cron作业。或者创建一个系统crontable并以授权用户身份运行PHP脚本,如@Philip所述。
I provide a detailed answer how to use cron in this * post.
我在这个*帖子中提供了如何使用cron的详细解答。
How to write a cron that will run a script every day at midnight?
如何编写一个每天午夜运行脚本的cron?