How to Run PHP Script as Normal User with Cron

Cron is a powerful utility for time-based scheduling of jobs in Unix-like operating systems including Linux. It runs as a daemon and can be used to schedule jobs such as commands or shell scripts to perform backups, schedule updates plus many more, that run periodically and automatically in the background at specific times, dates, or intervals.

One limitation of cron is that it assumes a system will run forever; so it is suitable for servers other than desktops machines. Additionally, you can schedule a task on given or later time, using the ‘at’ or ‘batch’ commands: but the task is only run once (it’s not repeated).

Suggested Read: How to Schedule Jobs Using Anacron on Linux

In this article, we will explain how to allow a normal system user to run or execute a PHP script via a cron job scheduler in Linux.

You can schedule jobs using crontab (CRON TABle) program. Each user can have their own crontab file which is made up of six fields for defining a job:

  • Minute – accepts values between 0-59.
  • Hour – accepts values between 0-23.
  • Day of Month – stores values between 1-31.
  • Month of the year – stores values between 1-12 or Jan-Dec, you can use first three letters of each month’s name i.e Jan or Jun.
  • Day of week – holds values between 0-6 or Sun-Sat, Here also you can use first three letters of each day’s name i.e Sun or Wed.
  • Command – command to be executed.

To create or edit entries in your own crontab file, type:

$ crontab -e

And to view all your crontab entries, type this command (which will simply print the crontab file to std output):

$ crontab -l

However, if you are a system administrator and want to execute a PHP script as another user, you need to schedule it in the /etc/crontab file or root user’s crontab file which support an extra filed for specifying the username:

$ sudo vi /etc/crontab

And schedule your PHP script to be executed like this, specify the username after the timing section.

0 0 * * * tecmint /usr/bin/php -f /var/www/test_site/cronjobs/backup.php

The above entry executes the script /var/www/test_site/cronjobs/backup.php everyday at midnight as user tecmint.

If you want to execute above script automatically every ten minutes, then add the following entry to crontab file.

*/10 * * * * tecmint /usr/bin/php -f /var/www/test_site/cronjobs/backup.php

In the above example, the */10 * * * * represents when the job should happen. The first figure shows minutes – in this scenario, on every "ten" minute. The other figures show, respectively, hour, day, month and day of the week.

You may also like to read these following related articles.

  1. Using Shell Scripting to Automate Linux System Maintenance Tasks
  2. 12 Useful PHP Commandline Usage Every Linux User Must Know
  3. How to Run PHP Codes in Linux Terminal
  4. 30 Useful Linux Commands for System Administrators

That’s all! We hope you find this article useful. If you have any questions or extra ideas to share concerning this topic, use the comment form below.

Tutorial Feedback...
Was this article helpful? If you don't find this article helpful or found some outdated info, issue or a typo, do post your valuable feedback or suggestions in the comments to help improve this article...

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

5 thoughts on “How to Run PHP Script as Normal User with Cron”

  1. As a sysadmin, is there an advantage to doing this over placing the command in the user’s crontab like this?

    crontab -eu tecmint

    Actually I just thought of one. The tecmint user can’t change root’s crontab so they can’t change your entry. Are there any other reasons?

      • We are saying the same thing. Sorry I wasn’t clear. Anyway, I can see that there may be times I would need to do this so it’s reason enough to bookmark this article. I hope you keep writing articles, Aaron.

        Thanks for creating this one and responding to my question :-)


Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.