0

How can I run a cron task in Linux?

Following this Q&A,I have this cron task to run - just writing some info to a txt file,

// /var/www/cron.php
$myfile = fopen("cron.txt", "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);

But after adding the cron task via my terminal,

crontab -e

and then,

* * * * * /usr/bin/php /var/www/cron.php &> /dev/null

But when I open cron.txt, there is nothing in it.

Why? What have I missed?

Community
  • 1
  • 1
Run
  • 54,938
  • 169
  • 450
  • 748

2 Answers2

3

Change cron.txt by full path /var/www/my_system/cron.txt

// /var/www/cron.php
$myfile = fopen("/var/www/my_system/cron.txt", "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);

Or move to directory:

chdir("/var/www/my_system");
$myfile = fopen("cron.txt", "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);

And try again.

0

I would eliminate the redirect to /dev/null until you're sure you're not getting an error message.

My guess is "permissions".

SUGGESTIONS:

  1. Execute /usr/bin/php /var/www/cron.php manually, from the command prompt, to make sure the PHP script is OK.

  2. Identify the directory "myfile.txt" is being written to.

  3. Make sure both the directory and myfile.txt are writable.

Here are a couple of links with other troubleshooting hints:

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • thanks. both have the permission. how can I `execute /usr/bin/php /var/www/cron.php` via my terminal? – Run Feb 03 '16 at 04:31
  • 1
    By simply entering `/usr/bin/php /var/www/cron.php` – ksealey Feb 03 '16 at 04:32
  • Q: How can I execute /usr/bin/php /var/www/cron.php via my terminal? A: By opening up a terminal (ssh, or Linux terminal) and typing `/usr/bin/php /var/www/cron.php` ;). You should see "myfile.txt" in your current directory: `ls -lt *.txt`. – paulsm4 Feb 03 '16 at 04:33
  • I just did that on my terminal. but nothing heppen. – Run Feb 03 '16 at 04:33
  • Type "`ls -lt *.txt`. You should see a newly created file :). Then type `cat myfile.txt`. It should print "John Doe", and "Jane Doe" ;) – paulsm4 Feb 03 '16 at 04:35
  • I just did and I get this on my terminal - `ls -lt *.txt -rw-rw-r-- 1 xxx xxx 18 Feb 3 12:36 cron.txt` so where is `cron.txt` now? it is not in this dir - `/var/www/` at all. – Run Feb 03 '16 at 04:37
  • 1
    Dude, "cron.txt" is where you put it. In the *current directory*!!! With "ls -l", you're literally *LOOKING AT IT*. If you want it to go "elsewhere", you can 1) [chdir(/some/other/folder)](http://php.net/manual/en/function.chdir.php), then [fopen()](http://php.net/manual/en/function.fopen.php), or 2) better, simply give a [fully qualified path](https://en.wikipedia.org/wiki/Fully_qualified_name) in your fopen()". PS: You can run this command: `find / -name cron.txt -print` to find other copies of "cron.txt" on your filesystem. – paulsm4 Feb 04 '16 at 18:55