I have the following set as a cron job:
*/1 * * * * /usr/bin/php /var/www/html/recalls/php/savesjson.php
I checked the status and I get this back.
Mar 10 16:29:01 big-hoster CRON[30417]: (root) CMD (/usr/bin/php /var/www/html/recalls/php/savesjson.php)
Mar 10 16:29:02 big-hoster CRON[30416]: pam_unix(cron:session): session closed for user root
The savesjson.php file should execute and basically save a new JSON file (current.json) in the same directory. If I just run this file (by going to the URL), it works perfectly. However, my cron isn't actually updating the current.json file.
current.json has permissions as follows:
-rwxr--r-- 1 root root 0 Mar 10 16:17 /var/www/html/recalls/php/current.json
savesjson.php has permissions as follows:
-rwxr-xr-x 1 www-data www-data 213 Mar 8 14:11 /var/www/html/recalls/php/savesjson.php
What am I missing here?
UPDATE: If I try to execute from the CLI, like so:
# /usr/bin/php /var/www/html/recalls/php/savesjson.php
It appears to execute, but the script doesn't do what it's supposed to (create new file) and there are no errors in my PHP logs. However, the PHP does work if I go directly to the URL (example.com/php/savesjson.php).
Related
I have a question about the exec function on PHP 7.0.
I write a shell script to copy/sync the files and log the details into a log file, everything happens in the tmp folder.
# /tmp/data
drwxrwxr-x 4 root www-data 4096 Apr 4 00:00 data
# /tmp/data/data.log
-rwxrwxr-x 1 root www-data 9551 Apr 4 04:19 sync.log
I tried to login as www-data user for testing the shell script, and it works fine via command line but it's not good with PHP exec as below:
exec('sh /var/www/sync.sh');
I tried to dump the data and I am sure that I can touch the .sh file.
Is there anyone have ideas about this issue?
Thanks.
after a week of trying i dont know what else to do.
I have a simple php script that is on my webserver called getpoem.php
The script opens up a website pulls its content and saves it to a poem.txt on the server
<?php
//File to extract the Poem of the day
$homepage = file_get_contents('http://SomeWebsite.com/today.php');
$poemALL = substr($homepage,strpos($homepage,"<p>"),strlen($homepage));
.
. // extracting the poem and saving it to $poemFinish
.
file_put_contents("poem.txt", $poemFinish); ?>
so this is a fairly simple script (it works fine if i manually execute it). This script should be executed with the www-data user with its cron so i opend up cron with this command and entered the command it should run
sudo crontab -u www-data -e
0 3 * * * php /var/www/html/getpoem.php
to avoid any permission problems i gave the getpoem.php and poem.txt rwx rights like this (i know i should change it when its live but this is just to test)
-rwxrwxrwx 1 www-data www-data 1189 Aug 17 15:07 getpoem.php
-rwxrwxrwx 1 www-data www-data 1335 Aug 17 15:07 poem.txt
So this is the setup, but it will not execute.
What i did so far changing the "php" to /usr/bin/php to secure that cron know what php is.
Next thing i did was making shure that cron was running so i changed the cronjob to
2 * * * */usr/bin/php /var/www/html/getpoem.php | > /var/www/html/test.txt
wich again did nothing .... so i changed it to
2 * * * */usr/bin/php /var/www/html/getpoem.php | > /tmp/test.txt
that did not run the php file but created a empty (-.-") file called test.txt in my tmp dir.
So i think the problem must be someware in the acces rights of my www-data user. It's just very wierd because all my webcontent (php files, webapp usw.) are also owned by the www-data user and they run smoothly.
Do i need to grand extra privileges for the cron of www-data ?
When running php from cron, your working directory usually isn't the directory the file is located in. When writing a file, it might try to write the file poem.txt relative to / which usually isn't writable.
So you either set the working directory or you should use "absolute" paths. So for example:
file_put_contents(__DIR__.'/poem.txt');
where __DIR__ is a magic constant that contains the directory where the current file is in.
I'm trying to build a sign-up workflow. On the first page someone enters a bunch of information, then submits the form. The processing script /var/www/html/command/old/perl.php needs to call a different script (/home/ec2-user/perl.php) which in turn calls a perl script.
From the command line I am able to run the /var/www/html/command/old/perl.php file (while logged in as ec2-user) without problem. When I try to run it by visiting the script in my browser, however, I get a blank screen.
Here are the permissions on the two perl.php files:
[root#ip-172-31-30-184 old]# ls -l /home/ec2-user/perl.php
-rwxr-xr-x 1 nginx nginx 672 Oct 23 18:21 /home/ec2-user/perl.php
and
[root#ip-172-31-30-184 old]# ls -l /var/www/html/command/old/perl.php
-rwxr-xr-x 1 nginx nginx 94 Oct 23 18:24 /var/www/html/command/old/perl.php
the perl script itself (though I don't think it matters if the PHP scripts are working) has the following:
[root#ip-172-31-30-184 old]# ls -l /home/ec2-user/dnsmeapi.pl
-rwxr-xr-x 1 nginx nginx 1605 Oct 23 02:02 /home/ec2-user/dnsmeapi.pl
What am I missing to make it work via the browser?
So this is my code for the raspberry pi to get a still shot from the raspicam and save it on a directory,
<?php
exec('raspistill -n -hf -o /var/www/img/image.jpg --timeout 1');
?>
I have given the ownership and the permission to read/write in that forlder using -R. so my ls -al in /var/www is this
drwxr-xr-x 3 www-data www-data 4096 Jun 19 08:05 .
drwxr-xr-x 12 root root 4096 Jun 19 05:54 ..
-rwxrwxrwx 1 www-data www-data 74 Jun 19 08:30 getImg
drwxrwxrwx 2 www-data www-data 4096 Jun 19 09:21 img
-rw-r--r-- 1 root root 70 Jun 19 10:07 index.php
getImg is the script i tried to run the script as a file like shell_exec('/bin/bash ./getImg'); that also doesn't work.
i have added /bash/bin and tried to run the script without using the script file too but that doesn't get the results.
How ever when i try to run the php file in the terminal, it creates the image as it normally should. So i figure this must be a permission issue, but what else should i do with the permissions? I have given all the rights to the directory.
EDIT
So I have found a workaround to this. since I don't know what the cause for the problem, i'd not mark this as an answer, but please vote it to appear at the top.
I now execute the script using the cgi scripts. I have created a shell script in the /usr/lib/cgi-bin/
#!/bin/bash
echo "Content-type:text/html\n"
sudo raspistill -vf -n -o /var/www/img/image.jpg --timeout 1200 --metering matrix
echo "Status: 204"
I saved this as capture and made this executable, did nothing with the permissions though.
sudo chmod +x capture
now when i open the link http://192.168.1.85/cgi-bin/capture the browser will still get me a 500 internal server error message. how ever, the image would still be created.
I would now need to get the 500 internal server error to be fixed.
[I'd add this as a comment but don't have enough points for it]
if you use the optional parameters $output and $return_var to capture the output and return value what do you get?
string exec ( string $command [, array &$output [, int &$return_var ]] )
does your command rely on environment variables that may be available when you run it as your user but not as www-data? (you can use the env command to check that)
does it still work if you run it via terminal after switching user to www-data?
I host a couple of websites which have live twitter feeds. My set up means that each website has a tweets.php file that will get the tweets using twitter OAUTH and then write it to a text file. I then use a javascript file to get the JSON from the raw text (.txt) file.
Until I learnt about cron jobs I was refreshing the tweets.php files myself as part of my day-to-day admin.
However, I've set up my cron jobs and they don't seem to be executing the php file or writing to the text file (both have 777 permissions on them).
Furthermore, I have both a cron file created using crontab -e but there is also a directory called cron.hourly. As I wasn't sure which one to use I have put the cron jobs in both but they don't seem to be executing. Here are my files:
#daily /etc/webmin/mysql/backup.pl --all
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /etc/webmin/status/monitor.pl
0 * * * * /var/www/html/websiteone.co.uk/wp-content/themes/websiteone/tweets.php
0 * * * * /var/www/html/websitetwo.co.uk/wp-content/themes/websitetwo/tweets.php
And the cron file (called 'twitter') within the cron.hourly directory is:
/var/www/html/websiteone.co.uk/wp-content/themes/websiteone/tweets.php
/var/www/html/websitetwo.co.uk/wp-content/themes/websitetwo/tweets.php
Now I've checked the cron logs and they seem to be running the jobs hourly but the tweets aren't updating and neither is the raw text file.
08:00:01 ip-10-142-232-156 CROND[25986]: (root) CMD (/var/www/html/websitetwo.co.uk/wp-content/themes/websitetwo/tweets.php)
Jun 5 08:00:01 ip-10-142-232-156 CROND[25987]: (root) CMD (/etc/webmin/status/monitor.pl)
Jun 5 08:00:01 ip-10-142-232-156 CROND[25988]: (root) CMD (/var/www/html/websiteone.co.uk/wp-content/themes/websiteone/tweets.php)
Jun 5 08:01:01 ip-10-142-232-156 CROND[26014]: (root) CMD (run-parts /etc/cron.hourly)
Jun 5 08:01:01 ip-10-142-232-156 run-parts(/etc/cron.hourly)[26014]: starting 0anacron
Jun 5 08:01:01 ip-10-142-232-156 run-parts(/etc/cron.hourly)[26023]: finished 0anacron
Jun 5 08:01:01 ip-10-142-232-156 run-parts(/etc/cron.hourly)[26014]: starting twitter
Jun 5 08:01:01 ip-10-142-232-156 run-parts(/etc/cron.hourly)[26033]: finished twitter
Apologies if I'm missing something obvious but scheduled task automation is a new area for me.