Run Cron Job as www-data or Different User

To run a cron job as a specific user or www-data, follow these simple steps.

Type in: nano /etc/crontab

Then, add your cron job at the end of this file with the user’s name before the command to run. For example:

0 0-3,5-23 * * * www-data /usr/bin/php /var/website/script.php >> /var/website/log/`date "+\%Y-\%m-\%d.\%H-\%M-\%S"`.script.cron.txt 2>&1

The above cron job runs hourly except at 4am, and is run by www-data. It also saves output to a log file (named based on the current time) and disables email alerts sent to you upon cron execution.