3

There is a Cron job on Ubuntu 16.04 which runs a PHP script

$parseinfo = 'parseme.json';
$handle = fopen($parseinfo, 'w');
fwrite($handle, json_encode($res));

to parse data into a JSON file. The PHP script is supposed to write data within the same folder where this PHP script is located in but I've faced a problem that JSON is being saved into root's home directory:

This is how I set Cron job:

*/10 * * * *  root    /usr/bin/php    /var/www/somederictory/somefolder/parse.php > /dev/null

From terminal:

ssh root@example.com
cd /etc
crontab -e
:x

In short words: JSON is being saved into /root while I want it to be saved into /var/www/somederictory/somefolder/ What should I do to fix it?

Edit: It is not a duplicate, PHP works, JSON is being saved, but in a wrong way. And I'm asking to help me understand what's wrong with current Cron settings.

galoget
  • 2,963
  • It is not a dublicate, php works, json is being saved, but in a wrong way. And I'm asking to help me understand what's wrong with current cron settings – vNottbeck Mar 02 '18 at 11:17
  • IMO this is a exact duplication, although the proposed duplication has a meaningless title, the solution is the same. However, I wrote an answer. – pa4080 Mar 02 '18 at 11:49
  • 1
    It certainly is a duplicate. If you read the question you will see that OP has the exact same problem: a Cron jobs saves its data in an unexpected location. If you read the answers you will notice that the solution is extremely similar and even written by the same author. I'm voting to close. Altogether your question is still well stated, so +1. – David Foerster Mar 02 '18 at 13:25

1 Answers1

6

By default Cron jobs are executed in the user's home directory. While in your script is not provided path where the output file to be saved, it will be saved into the directory where the script is executed.

According to the question, you want to generate the .json file into the same directory where the script is located. So (in this case) you have to change your code in some way, similar as this:

$parseinfo = 'parseme.json';
$path = realpath(dirname(__FILE__));
$handle = fopen("$path/" . $parseinfo, 'w');
fwrite($handle, json_encode($res));

If you don't want to change the script you could change the Cron job in this way:

*/10 * * * * root    cd /var/www/somederictory/somefolder/ && php parse.php > /dev/null
David Foerster
  • 36,264
  • 56
  • 94
  • 147
pa4080
  • 29,831
  • Assuming you're using bash, You can also set the environment for all cronjobs by setting BASH_ENV in your crontab, e.g. BASH_ENV=~/.profile – ShadSterling Mar 05 '18 at 13:57
  • Hi, @ShadSterling, I can't see what is the relation of your comment to the current topic. The problem here is not the Cron job itself, but the location of the output of the executed file. – pa4080 Mar 05 '18 at 14:11
  • Setting the environment can include changing the current directory. If the file is written to the current directory, it might be preferable to change the current directory of the cron environment than to hardcode a path into the script(s), or changing each of the jobs. – ShadSterling Mar 05 '18 at 17:45