Cronjob saves json into a wrong directory [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP








up vote
3
down vote

favorite













This question already has an answer here:



  • php /var/www/html/*.php is not working in terminal which is open from Menu php *.php is working in terminal which is open inside php holding folder

    2 answers



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.







share|improve this question














marked as duplicate by pa4080, Arronical, Eric Carvalho, David Foerster, Zanna Mar 2 at 15:05


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 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 2 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 2 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 2 at 13:25















up vote
3
down vote

favorite













This question already has an answer here:



  • php /var/www/html/*.php is not working in terminal which is open from Menu php *.php is working in terminal which is open inside php holding folder

    2 answers



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.







share|improve this question














marked as duplicate by pa4080, Arronical, Eric Carvalho, David Foerster, Zanna Mar 2 at 15:05


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 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 2 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 2 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 2 at 13:25













up vote
3
down vote

favorite









up vote
3
down vote

favorite












This question already has an answer here:



  • php /var/www/html/*.php is not working in terminal which is open from Menu php *.php is working in terminal which is open inside php holding folder

    2 answers



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.







share|improve this question















This question already has an answer here:



  • php /var/www/html/*.php is not working in terminal which is open from Menu php *.php is working in terminal which is open inside php holding folder

    2 answers



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.





This question already has an answer here:



  • php /var/www/html/*.php is not working in terminal which is open from Menu php *.php is working in terminal which is open inside php holding folder

    2 answers









share|improve this question













share|improve this question




share|improve this question








edited Mar 13 at 12:25









galoget

2,1062820




2,1062820










asked Mar 2 at 11:02









vNottbeck

183




183




marked as duplicate by pa4080, Arronical, Eric Carvalho, David Foerster, Zanna Mar 2 at 15:05


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by pa4080, Arronical, Eric Carvalho, David Foerster, Zanna Mar 2 at 15:05


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 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 2 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 2 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 2 at 13:25

















  • 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 2 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 2 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 2 at 13:25
















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 2 at 11:17




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 2 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 2 at 11:49





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 2 at 11:49





1




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 2 at 13:25





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 2 at 13:25











1 Answer
1






active

oldest

votes

















up vote
6
down vote



accepted










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





share|improve this answer






















  • 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 5 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 5 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 5 at 17:45


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
6
down vote



accepted










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





share|improve this answer






















  • 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 5 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 5 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 5 at 17:45















up vote
6
down vote



accepted










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





share|improve this answer






















  • 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 5 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 5 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 5 at 17:45













up vote
6
down vote



accepted







up vote
6
down vote



accepted






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





share|improve this answer














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






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 2 at 13:23









David Foerster

26k1361106




26k1361106










answered Mar 2 at 11:36









pa4080

12k52255




12k52255











  • 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 5 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 5 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 5 at 17:45

















  • 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 5 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 5 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 5 at 17:45
















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 5 at 13:57




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 5 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 5 at 14:11




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 5 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 5 at 17:45





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 5 at 17:45



Popular posts from this blog

pylint3 and pip3 broken

Missing snmpget and snmpwalk

How to enroll fingerprints to Ubuntu 17.10 with VFS491