Use php to do a website, one of the statistics, we need to use the line every week. I wanted to see what way before other people see things, in general there are two ways, one is the php built-timed task with a system band, scheduled tasks under Windows Linux crontab and under.
Before using the php built-in timer task work can be found is simple to implement, in the middle there is no a lot of problems while doing time for ob_flush () and flush () and other functions are not very familiar with, and later saw some related article,
To which things have a deeper understanding. But later found after the php regular tasks on, seemingly can not stop, you do not need to restart the server down. You can not be resolved by modifying the php file.
Later, it intends to use the crontab Linux under to achieve. This time using Ubuntu to do the deployment system, nginx server to do. Crontab comes under this system, so do not install their own.
Open crontab log
crontab by default not performed open day, so after a start to finish, can not perform, you want to see the log, but can not find. So the first task of opening timing of logs to see
Modify rsyslog service the /etc/rsyslog.d/50-default.conf file before the # # cron * deleted; restart the rsyslog service with the following command:
service rsyslog restart;
And then restart the crontab service:
service cron restart;
Many people say the Internet open, stop or restart contab service with the command service crond restart, execute this command reported unrecognised service, where it crond cron into it.
After that, in this position /var/log/cron.log can view the scheduled task log files files;
The following command can also view the status of crontab
service cron status;
Log File Viewer runtime execution if a statement appears in the log file:
No MTA installed, discarding output
When the crontab script execution is not directly output an error message, but will be sent as e-mail to the mailbox, you need a mail server, if not installed on the mail server, it will report the mistake.
Each timer can be added later in the script:
> / Dev / null 2> & 1
About to enter all the information into the linux system null device / dev / null in.
To solve No MTA installed, discarding output problems.
Write regular tasks
By the following command, you can turn on the user's time file, the file is stored in the folder / var / spool / cron / crontabs file
And to use the named file
Through the following command to list the detailed contents of a user cron services
I first wrote a timed test the following tasks using php to write a function, a write data to the database every minute, every minute timed task can be executed url
1 * * * * curl servername / Home / Test / test> / dev / null 2> & 1
curl also comes with Ubuntu, without additional installation. This command at the command line alone is possible.
Crontab can not seem to find the execution, later found to be written every minute, minute bits to be written * / 1, to perform the following is copied from others of several commands, but I have tried
Copy the code
Every five minutes the implementation * / 5 * * * *
Hourly 0 * * * *
00 performed per day * * *
0 * 0 * 0 performed weekly
Executive Monthly 001 * *
Executive 0011 per year *
Copy the code
Before the final I can also run through the following commands are assigned to root privileges. But in my final run, I do not know this command has no effect. But still the first to write on. After running services need to restart the crontab
crontab -u root / var / spool / cron / crontabs / root
When you can not run before you see a lot of people say the problem is not run environment variables need to be amended. But this time I did not encounter. But first down. The future may be used.
I use the root user to write regular tasks, and you see others write the following content, try the next
/etc/cron.deny that they can not use the crontab command
/etc/cron.allow represents can use crontab users.
If both files exist, then /etc/cron.allow priority.
If neither file exists, only the super user can schedule the job.