Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a cronjob which for creat thumbnails. the folder /var/www/html/work/ has permission 744(I think 744 is more safe than 777)

Crontabs

* * * * * /usr/bin/php /var/www/html/work/images.php

php script

$newstore = dirname(__FILE__) . '/images/.$data->name.'jpg';
$width = (int)($data->width);
$height = (int)($data->height);
$desired_width = round((300*$width)/$height);
$desired_height = 300;
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
imagecopyresized($virtual_image,$source_image,0,0,0,0,$desired_width,$desired_height,$width,$height);
imagejpeg($virtual_image,$newstore);

EDIT

In browser part. I have login into the phpmyadmin with root, So when I try localhost/work/images.php, it works well., and works in/usr/bin/php /var/www/html/work/images.phpinPuTTY SSH`.

but the job failed in crond. I have restart cron service crond restart all the thing [OK], then tried /etc/cron.allow, return -bash: /etc/cron.allow: No such file or directory, run /etc/cron.deny, return -bash: /etc/cron.deny: Permission denied.

BTY: vi /var/log/httpd/error_log there have no errors hint.

Where is the problem? crond setting? or folder permission?

share|improve this question
    
When you SSH in and run /usr/bin/php /var/www/html/work/images.php, what error message do you get? I imagine that you're not able to write to the /var/www/html/images directory as the user you are running as. –  cjc Jan 21 '12 at 12:24
    
Whats in php error log? Also wouldn't $newstore be /var/www/html/work/images and not /var/www/html/images ? –  faker Jan 21 '12 at 12:24
    
@cjc, @faker, thanks for reply. I used chmod u+s /usr/bin/crontab and chmod u+s /usr/bin/php, now /usr/bin/php /var/www/html/work/images.php can work. but cron still not work. I have restart cron service crond restart all the thing [OK], then tried /etc/cron.allow, -bash: /etc/cron.allow: No such file or directory, run /etc/cron.deny, -bash: /etc/cron.deny: Permission denied. So at this time, my problem is cron not excused. BTY: vi /var/log/httpd/error_log there have no errors hint. Thanks. –  yuli chika Jan 21 '12 at 13:43
    
/var/log/httpd/error_log is Apache error log - check your PHP error log. –  faker Jan 21 '12 at 15:20
add comment

2 Answers 2

up vote 4 down vote accepted

Oh, god, stop chmoding basic system binaries. No good will come of it.

If you're able to run your script from the command line as root, but it fails in cron, then the most likely issues are:

1) a permission issue (specifically, a basic permission problem, not something system-wide, which is what you're trying to fix with your chmod statements).

2) a PATH issue.

For #1, have you looked at the ownership and permissions on the /var/www/html/images directory? Will the ownership and permissions on this directory allow the user running the cron to modify files?

Actually, how are you running the cron, in terms of the user? Do you have it in the /etc/cron.d directory? If you have placed the cronjob file in /etc/cron.d, then the format will be slightly different, in that you have to put the user who will execute the cron in between the date/time fields and the command.

For #2, I think this is unlikely, since you're specifying the absolute pathname of the executable (/usr/bin/php) and your script doesn't include any extra libraries.

share|improve this answer
    
I will find and fix my problem as your suggestion. I set the php scirpt folder permission is 744. I do not want anyone can run my script. So how to chown the permission to /usr/bin/crond and /usr/bin/php? Thanks. –  yuli chika Jan 21 '12 at 15:23
1  
/usr/sbin/crond should have permissions 0755 and is owned by root, group root (so "chmod 0755 /usr/sbin/crond ; chown root:root /usr/sbin/crond". /usr/bin/php has the same permissions, so the chmod and chown commands as above will also work. /usr/bin/crontab is permission 6755, so you will want to use "chmod 6755 /usr/bin/crontab". Ownership is root:root, as with the other files. –  cjc Jan 21 '12 at 16:11
    
The php script folder will probably need permssions 0755 (744 will allow the non-owner to read the directory contents but not enter it; you need to set the execute bit on the directory to allow non-owners to enter it). The folder you're writing to should be owned by whoever is executing the cron job. –  cjc Jan 21 '12 at 16:15
    
I have done change permssions 0755 for folder, chmod 0755 /usr/sbin/crond, chmod 0755 /usr/bin/php, chmod 0755 /var/www/html/work/images.php, chown root:root /usr/sbin/crond, chown root:root /usr/bin/php, chown root:root /var/www/html/work/images.php. But still not work for cron. I tried: vi /var/log/cron, there some lines like: crond[3808]: (root) CMD (root /usr/bin/php /var/www/html/work/images.php), is this run well? or no? Many thanks. –  yuli chika Jan 21 '12 at 19:16
    
The log entry shows that cron is running the file. So, root is running the cronjob? How are you editing the cron table? Using "crontab -e" as root? –  cjc Jan 21 '12 at 20:25
show 1 more comment

Change your cron job to

* * * * * /usr/bin/php /var/www/html/work/images.php &>/tmp/job.log

this will capture any output from your script in /tmp/job.log. With any luck it will have the information you need to figure out what is going on.

Have a look in /var/log/cron and see if there are any related messages in there too.

share|improve this answer
    
I have add a new file: vi /tmp/job.log, and do the thing what you suggest, but now 15 minutes past, there still nothing in /tmp/job.log. Now I also try to change * * * * * root /usr/bin/php /var/www/html/work/images.php, still not working. BTW, my cron crontab -e is saving in /tmp/crontab.XXXXZWOFCH –  yuli chika Jan 21 '12 at 15:04
    
No, what I said was change your cron job, use crontab -e to add &>/tmp/job.log to the end of your current job. Don't worry about where crontab -e is saving the job. –  Iain Jan 21 '12 at 15:18
    
thanks. I have checked my folder again, I set the folder is 755 but my colleague set the script is 744. he said 744 can provide custom run your script play a joke. So how to grain the privileges to the crond and php run the script as the root permession? many thanks. –  yuli chika Jan 21 '12 at 15:41
    
A script needs both read and execute permissions. –  Iain Jan 21 '12 at 15:44
    
yes, 744 is that the root has read, write, execute permession. but user/group only have read permession. –  yuli chika Jan 21 '12 at 15:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.