Use PHP to make a website, one of the statisticians can, need to be timed weekly. Want to see what method, before looking at other people's things, generally there are 2 of the way, one is the PHP comes with the scheduled task, one is to use the system
Crontab under Linux and Timed tasks under Windows.
Before using the PHP self-timer task can, found that the implementation is very simple, the middle did not appear a lot of problems, while doing the Ob_flush () and flush () and other functions are not very familiar, and later read some related articles,
Have a deep understanding of what is in it. But later found that PHP timer task opened, seemingly can not stop, do not need to restart the server to line. cannot be resolved by modifying PHP files.
Later, I intend to use the Linux crontab to achieve. This time using Ubuntu to do the deployment system, Nginx to do the server. This system comes with crontab, so do not install it yourself.
Turn on crontab log
Crontab default is not to perform the open day, so after the beginning of writing, cannot execute, want to view the log, but can not find. So start the log of the scheduled task to see
Modify the Rsyslog service to delete the #cron. * in the/etc/rsyslog.d/50-default.conf file; Restart the Rsyslog service with the following command:
Service Rsyslog Restart;
Then restart the crontab service:
Service cron Restart;
Many people on the internet said to open, stop or restart the Contab service with the command services Crond restart, the execution of this command will be reported unrecognised service, here Crond changed to cron.
After that, in this position/var/log/cron.log can view the file log file of the scheduled task;
The following command also allows you to view the status of the crontab
Service cron status;
View the log file at run time if a statement is executed in the log file:
No MTA installed, discarding output
Then crontab executes the script is not directly wrong information output, but will be sent in the form of mail to the mailbox, the mail server is required, if the mail server is not installed, it will report this error.
You can add after each timed script:
>/dev/null 2>&1
All information is entered into the empty device/dev/null in the Linux system.
Can solve the problem of no MTA installed, discarding output.
Write timed Tasks
With the following command, you can open the user's timing file, the file is stored in the/var/spool/cron/crontabs folder
And a file named after the name of the
Crontab-e
You can use the following command to list the details of a cron service for a user
Crontab-l
I wrote a timed task to test the following, write a function in PHP, every minute to write a data to the database, scheduled tasks every minute to execute the URL can be
1 * * * * Curl servername/home/test/test >/dev/null 2>&1
Curl also comes with Ubuntu, without installing it separately. This command can be performed separately on the command line.
Found in the crontab seems unable to execute, and later found that every minute can not write, minutes to write */1, in order to execute, the following is a copy from someone else that a few commands, but I have tried
Execute */5 every five minutes * * * * hourly execution 0 * * * * daily execution 0 0 * * * weekly execution 0 0 * * 0 monthly Execution
0 0 1 * * annual execution 0 0 1 1 *
I ran the following command before I could finally use it, giving root permission. But when I finally run, I don't know if this command is going to work. But let's write it first. Need to restart Crontab service after operation
-U root/var/spool/cron/crontabs/root
Before you can not run the time, see a lot of people say that can't run is an environment variable problem, need to modify. But I haven't met you this time. But write it down first. May be used in the future.
I use the root user to write a timed task, and see what others have written below, next time to try
/etc/cron.deny represents a user who cannot use the crontab command
/etc/cron.allow represents a user who can use crontab.
If two files exist at the same time, then/etc/cron.allow is preferred.
If none of the two files exist, only the Superuser can schedule the job.
Use Crontab to deploy scheduled tasks under Ubuntu