0
votes

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"
2
Use absolute path to testCron.phpfrz3993
Where/how did you "set" that line?Mat
@frz3993 - With absolute path is the same errorSimon
"But the file cron-....txt doesn't appear." - Where? grinKaroly Horvath
You changed the question substantially, but I'm going to give similar comment. Use absolute path, even in the PHP file.frz3993

2 Answers

0
votes

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, e.g. 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
0
votes

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