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.

If you read this far, tweet to the author to show them you care. Tweet a thanks
Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

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?

    Reply
      • 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 :-)

        Reply
        • @Chris

          Sure, we will always do our best to produce top quality and free articles for Linux users out there. Many thanks for the kind words of appreciation and for always following us.

          Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.