cron/php how to run command as a non root user
I need to run command (only one command) as a non root user from cron.
Have tried two versions and both fails;
Cron:
* * * * * php script.php
script.php:
exec("whoami"); // returns 'root'
// version 1
exec("runuser -u www-data -- ls"); // error -> sh: 1: runuser: not found
// version 2
exec("su www-data -c 'ls'"); // error -> This account is currently not available.
Solution 1:
Don't use root
crontab.
Use crontab for www-data
user:
crontab -e -u www-data
or use a system crontab in /etc/cron.d/
and specify user www-data
.
* * * * * www-data php script.php