I can't set cron job

I've tried to set a cron job in "crontab -e":

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

testCron.php file creates another file, it contains only this:

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

But the file cron-....txt doesn't appear. Were can be a problem?


I did/checked this:

  • Cron is working. I checked this with 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"

  • You can check the log from cron jobs to identify errors.

    Depending on your system, the cron log can be located in different places. Find the files containing cron logs:

    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 /var/log/syslog
    

    Or view the latest run of testCron.php in that file:

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

    Your script creates the cron-...txt file with no path; 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.

    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
  • Set a specific directory in your crontab configuration
  • 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
  • 链接地址: http://www.djcxy.com/p/56852.html

    上一篇: 如何从另一个shell脚本调用shell脚本?

    下一篇: 我无法设置cron作业