3

I found my crontab scripts do not work as expected because they cannot write on /var/log. I tried executing command:

sudo /usr/bin/php /var/www/html/iPhone/inarrivo/php/rome/process.php >> /var/log/romeLoading.log 2>&1

by hand and got:

-bash: /var/log/romeLoading.log: Permission Denied

/var/log permissions are:

drwxr-xr-x. 13 root root 4096 15 ago 16.20 .

If I conversely execute:

sudo touch /var/log/loadRome.log

I get no error whatsoever.

What could be the issue?

Please note Apache is not at stake: I am calling those scripts from the root crontab and from the shell with sudo as a test.

3
  • Permissions. It's typical (and good practice) to not allow a webserver to run as a privileged user. If the info in the log is public, chmodd'ing the file to 0777 should allow it to work. Commented Aug 15, 2017 at 16:36
  • I am not talking of apache. Commented Aug 15, 2017 at 16:53
  • OK, and thank you for clarifying that in the question. So you're running this from crontab ... note that cron(8) doesn't run as you, so its environment variables, etc., are not the same; in particular, paths can be at issue. I would start by verifying that you're using the complete path to sudo. However, as you say this is root's crontab ... that's interesting. What does a cronjob with simply "/usr/bin/env whoami" return? And, do you really need "sudo" if you're running on the root crontab? Commented Aug 15, 2017 at 18:14

2 Answers 2

5

best guess: the user running the shell doesn't have write access to /var/log/romeLoading.log , and the stdout redirect (>>) is redirected by the shell user, not the sudo user, thus the access denied on >> , but not on sudo touch. maybe try

sudo sh -c '/usr/bin/php /var/www/html/iPhone/inarrivo/php/rome/process.php >> /var/log/romeLoading.log 2>&1'

that should run sh as root, and have the root-sh do the redirect with root permissions. untested though.

and next time you want to post permissions for debugging, post the namei -l path/to/file output, it gives much more info than stating the single file itself when debugging permission issues, as the issue can be higher up than the file itself, like the folder its in, or the folder that the folder it's in, is in, etc~ and namei gives you, recursively, detailed permission information on all of them.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, it works like this. Perhaps the problem on the crontab was due to another issue. Thanks.
@FabrizioBartolomucci Mark this as the correct answer! it worked for me as well
3

It's a permissions issue as the log file belongs to root user and apache runs off www-data. Try chown www-data:www-data /var/log/loadRome.log.

1 Comment

Nope. I run the command from the shell and from the root crontab, not from apache pages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.