简体   繁体   English

我无法设定Cron工作

[英]I can't set cron job

I've tried to set a cron job in "crontab -e" : 我试图在“ crontab -e”中设置cron作业:

* * * * * /usr/bin/php /home/vagrant/Code/xxx/testCron.php

testCron.php file creates another file, it contains only this: testCron.php文件创建另一个文件,它仅包含以下内容:

<?php
$f = fopen('cron-' . date('H-i-s') . '.txt', 'w+');
fwrite($f, 'asdf');
fclose($f);

But the file cron-....txt doesn't appear. 但是文件cron -.... txt没有出现。 Were can be a problem? 可能是个问题吗?


I did/checked this: 我做了/检查了这个:

  • Cron is working. Cron正在工作。 I checked this with sudo "service cron start" commad. 我用sudo“ service cron start” commad检查了一下。
  • the $PATH contains "/home/vagrant/.composer/vendor/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games" $ PATH包含“ /home/vagrant/.composer/vendor/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/游戏:/ usr / local / games”

You can check the log from cron jobs to identify errors. 您可以检查cron作业中的日志以识别错误。

Depending on your system, the cron log can be located in different places. 根据您的系统,cron日志可以位于不同的位置。 Find the files containing cron logs: 查找包含cron日志的文件:

grep -ic cron /var/log/* | grep -v :0
./auth.log:1246
./auth.log.1:3054
./bootstrap.log:3
./syslog:187
./syslog.1:220

Then view the contents of the log in question, eg via cat : 然后查看相关日志的内容,例如通过cat

cat /var/log/syslog

Or view the latest run of testCron.php in that file: 或在该文件中查看testCron.php的最新运行:

grep -i testCron.php /var/log/syslog | tail -1

Your script creates the cron-...txt file with no path; 您的脚本创建没有路径的cron-...txt文件; therefore it appears in the current directory - if it has write permission. 因此,它显示在当前目录中-如果具有写权限。 Cronjobs will have a different start directory - usually the user's home, but may be anywhere, depending on configuration. Cronjobs将具有不同的开始目录-通常是用户的家,但可能在任何地方,具体取决于配置。

Try one of these: 尝试以下方法之一:

  • Change your script to create the file in a specific directory 更改脚本以在特定目录中创建文件
  • Do a chdir() to set a specific current working directory 执行chdir()来设置特定的当前工作目录
  • Set a specific directory in your crontab configuration 在crontab配置中设置特定目录
  • Look for the files in the user's home directory 在用户的主目录中查找文件
  • Ensure the user has write permission to the directory you are placing the files in 确保用户对要放置文件的目录具有写权限

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM