I have a bash script in /var/www/html/exe. This folder is owned by www-data:www-data
PHP code calls this script, running as www-data. I test it using
sudo -u www-data script.sh
sudo tee: I get permission denied trying to write a file from the script, e.g. as a test:
echo 10 | sudo tee tttt
It prompts for the www-data sudo password. This won't work.
Temp file: I also tried writing a temp file to the /home/ubuntu folder, also permission denied
Nopasswd: I could also apparently add www-data to require no password in the sudoers file. This is bad because it requires extra custom setup. Deployment is automated and this increases the complexity of the deployment code.
I tried various combinations of running in a different shell, e.g.
$ sudo sh -c "cd /home ; du -s * | sort -rn > USAGE" These resulted in prompting for the www-data sudo password, can't work.
Sudo: I don't think I should have to (nor do I want to) run the shell as sudo. Besides, this is a service and would require a change to code I don't own.
Manual intervention: Many of the answers I found suggest things like "log in as root". There is no human around to do this: the code is automatically built and deployed into multiple environments.
Hacky workaround: Write a cpp exe which writes the file and calls it from the script. I can write files this way with no problem. But this feels awful and adds complexity to build/deploy.
After reading dozens of pages about how Linux handles permissions and shells, I am still not sure I completely understand how shells and subshells interact with permissions. I suspect there is a way to do this, but how?