Apache以用户nobody运行php脚本,cron以用户身份运行php脚本

My default cpanel set up runs apache as user "nobody". So when I run a php script via a browser that outputs a file, that file has ownership nobody:nobody. When I run the script from a cron job logged in as user "fred", the output files are owned by fred:fred I need both browser and cron to overwrite the same file. The issue I have is that if one "user" creates the file, the other one can't overwrite it. Please can you let me know where the fundamental problem is and a possible solution. Permissions on the files are 0775.

  • Do I need to set up groups - adding the user to the same group as nobody? If so how?
  • How do I get the cron job to run as user nobody?

Many thanks, Lloyd

Try you code after changing permission of the file as 0777. But that creates a security issue as anyone can edit your file then.

I have a logic you need to implement to:

  1. Create a shell script that copy the contents of temp file to your actual file.
  2. From PHP you need to update only temp file and Shell can read it as temp file will have read permission for everyone.
  3. Use ssh2_exec command to execute the shell script with your Linux UserName and Password.

For ssh2_exec manual follow this link : http://php.net/manual/en/function.ssh2-exec.php

Hope This will solve your problem.

Ok, my solution to this was to create a crontab for user "nobody". I've got a standard cpanel installation and so went to /var/spool/cron created an entry for nobody. Ran crontab -e to edit it and install. Now the php runs as nobody in the cron job exactly the same as if through a browser. All files written belong to nobody with only rw permissions for nobody.