VOOZH about

URL: https://www.tecmint.com/run-php-script-as-normal-user-via-cron-job-scheduler/

⇱ How to Run PHP Script as Normal User with Cron


Skip to content

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 this article helped, share it with someone on your team.
TecMint Weekly Newsletter
Get the Learn Linux 7 Days Crash Course free when you join 34,000+ Linux professionals reading every Thursday.
Check your email for a magic link to get started.
Something went wrong. Please try again.
TecMint has been free for 14 years. Help keep it that way.
Google AI Overviews and tools like ChatGPT have cut into search traffic for independent tech sites like TecMint. Running this site costs over $2,000 every month for hosting, infrastructure, and paying authors to keep the content accurate and tested.

If this article helped you solve a problem, consider buying a coffee. It helps keep TecMint free, supports the authors, and keeps the project going.
☕ Buy Me a Coffee
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.

5 Comments

Leave a Reply
  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
    • @Chris

      Another possible reason is a user can change their own crontab file and possibly modify an entry saved by root.

      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
        • @Chris

          Personally, this is a great source of encouragement, thanks a lot.

          Reply

Got Something to Say? Join the Discussion... Cancel reply

Free Course
Get a free Linux course before you go.
Subscribe to TecMint Weekly and get the Learn Linux 7 Days Crash Course free. Read by 34,000+ Linux professionals every Thursday.
Check your email for a magic link to get started.