Running PHP script as background process

Ok, I’ll do this quickly. This will only work on a linux machine.

What is Crontab?

cron is a time-based job scheduler in Unix-like computer operating systems. cron enables users to schedule jobs (commands or shell scripts) to run periodically at certain times or dates. It is commonly used to automate system maintenance or administration, though its general-purpose nature means that it can be used for other purposes, such as connecting to the Internet and downloading email.

cron is a unix, solaris utility that allows tasks to be automatically run in the background at regular intervals by the cron daemon. These tasks are often termed as cron jobs in unix , solaris. Crontab (CRON TABle) is a file which contains the schedule of cron entries to be run and at specified times.

So here’s the code…

59 7 * * * /var/www/dummy/mail.php

Simple code explanation

59 – minutes (0 – 59)
7 – hour (0 – 23)
* – day of month (1 – 31)
* – month (1 – 12)
* – day of week (0 – 6) Sunday = 0
/var/www/dummy/mail.php – path to file

What the code does?

The code actually executes the script in php file (mail.php) everyday 7:59 in the morning. The php file contains php scripts to send email notifications.

Where to put these code?

Open up your terminal (Ctrl+Alt+T) and type “crontab -e” (without quotes). Enter your password if asked.

At the end of the line, put your code to run your script as a background process.

Then, Ctrl+O -> Enter (to save) and Ctrl+X to close. Now restart your system and it should work!

Sorry for my bad english but I hope you learned something.