How to run cron as a non-root user?

How can I configure a cron job to run as a non-root user? I want to schedule a task using cron, but I don’t want to run it with root privileges. What steps must I take to set up a cron job that runs as a specific non-root user?

Here is the cron I want to run.

* * * * * php /var/www/html/app/bin/legacy -C cron >/dev/null 2>&1

Method 1:

You can log in as a non-root user and then use the crontab -e command to edit the user’s crontab file. This will open crontab, especially for logged-in users. Then you can put any task will perform under that user.

Method 2 :

You can add -u <username> with crontab -e command which will also open the crontab to the user.

Method 3:

Before the scheduled task command, you can define the username. It will also run the task as that user.
For example.

* * * * * <username> php /var/www/html/app/bin/legacy -C cron >/dev/null 2>&1

If this does not work, just add sudo before the <username>.

* * * * * sudo <username> php /var/www/html/app/bin/legacy -C cron >/dev/null 2>&1

This should do the trick.