Cronjob saves json into a wrong directory [duplicate]
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgO9GURib1T8z7lCwjOGLQaGtrueEthgQ8LO42ZX8cOfTqDK4jvDDpKkLFwf2J49kYCMNW7d4ABih_XCb_2UXdq5fPJDkoyg7-8g_YfRUot-XnaXkNYycsNp7lA5_TW9td0FFpLQ2APzKcZ/s1600/1.jpg)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYQ0N5W1qAOxLP7t7iOM6O6AzbZnkXUy16s7P_CWfOb5UbTQY_aDsc727chyphenhyphen5W4IppVNernMMQeaUFTB_rFzAd95_CDt-tnwN-nBx6JyUp2duGjPaL5-VgNO41AVsA_vu30EJcipdDG409/s400/Clash+Royale+CLAN+TAG%2523URR8PPP.png)
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.
16.04 php cron json
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.
add a comment |Â
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.
16.04 php cron json
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
add a comment |Â
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.
16.04 php cron json
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
16.04 php cron json
edited Mar 13 at 12:25
![](https://i.stack.imgur.com/ypy8w.png?s=32&g=1)
![](https://i.stack.imgur.com/ypy8w.png?s=32&g=1)
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
add a comment |Â
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
add a comment |Â
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
Assuming you're using bash, You can also set the environment for all cronjobs by settingBASH_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
add a comment |Â
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
Assuming you're using bash, You can also set the environment for all cronjobs by settingBASH_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
add a comment |Â
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
Assuming you're using bash, You can also set the environment for all cronjobs by settingBASH_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
add a comment |Â
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
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
edited Mar 2 at 13:23
![](https://i.stack.imgur.com/E0SEH.png?s=32&g=1)
![](https://i.stack.imgur.com/E0SEH.png?s=32&g=1)
David Foerster
26k1361106
26k1361106
answered Mar 2 at 11:36
![](https://i.stack.imgur.com/Lrlbx.jpg?s=32&g=1)
![](https://i.stack.imgur.com/Lrlbx.jpg?s=32&g=1)
pa4080
12k52255
12k52255
Assuming you're using bash, You can also set the environment for all cronjobs by settingBASH_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
add a comment |Â
Assuming you're using bash, You can also set the environment for all cronjobs by settingBASH_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
add a comment |Â
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