0

I'd like to create a cronjob to execute a .php file on Ubuntu but it currently does not worked. The .php file is placed at /var/www/createfile.php to create a text file. I've tried to executed some commands on ssh. as follow

    'php /usr/bin/php /var/www/createfile.php' after that,
    '/user/bin/php /var/www/createfile.php' after that,
    'php /var/www/createfile.php'

(those are executed one after I found that the previous command doesn't worked)

The results from those commands are the same, there is no '.txt' file as expected. However, when I run 'php createfile.php' when I was at the directory storing the 'createfile.php', I got a '.txt' file as intending.

I'm not quite sure what did I do wrong?

thanks in advance.

// below this is what I would update, the above statement might confusing some of you. I'm sorry for that.

this is the code in '.php' targeted to execute

 > <?php
 $myfile = fopen("newfile.txt", "w");
 $txt = "Mickey Mouse\n";
 fwrite($myfile, $txt);
 fclose($myfile);
 ?>

Obviously, it's just simply create a file with a line of phrase. here is the ways I'd tried but doesn't work

  1. create cronjob like 'minute * * * * php /var/www/createfile.php' << this did work. So, I tried the commands like what I described at root directory with root permission.

However, the file was created by other user (let's say by Alice) I have no idea if Alice vs. root permission is a certain point of concern?

HOWEVER: there currently is one way which work. I've to run the command like 'php createfile.php' when I locating at '/var/www' (it is the path the 'createfile.php' locates) << this is the way which currently work.

  • With out seeing the code it's hard to say. – Jason K Jan 12 '21 at 22:33
  • Your user doesn't have permission to write a file in the directory you are in. You need to do something in the php file like `./file.txt` or `__DIR__.'file.txt` or something. If you turn on error reporting it will tell you the issue. – AbraCadaver Jan 12 '21 at 23:13
  • Your second example will work. But it's "usr", not "user". :) – FoulFoot Jan 12 '21 at 23:15
  • See also: https://stackoverflow.com/questions/22358382/execute-php-script-in-cron-job – FoulFoot Jan 12 '21 at 23:16
  • Cron may be running as a different user, in a different directory, or with other permissions. – Scott Swezey Jan 13 '21 at 06:12
  • Thank you guys, especially FoulFoot. You're right. In the question, it was mistyping, I did usr while trying at ssh. it's not worked either. – KTD grachangpun Jan 13 '21 at 09:40

1 Answers1

0

Your issues is most likely that you're using a relative path in your script, maybe something like this:

<?php

file_put_contents('my/dir/file.txt', $variable)
?>

That will create the file.txt file relative to where the script is being run. If it's run by cron as your user, it would most likely put the file in /home/username/my/dir/file.txt.

Use the absolute path to where you want to file.txt to be created:

<?php

file_put_contents('/var/www/my/dir/file.txt', $variable)
?>
Dennis
  • 56
  • 7