Use crontab to deploy scheduled tasks in Ubuntu

Source: Internet
Author: User
Tags rsyslog

Use crontab to deploy scheduled tasks in Ubuntu

Using php to build a website, one of which can be used regularly every week. I want to see what methods I used to look at other people's things. There are generally two methods: one is a timing task provided by php and the other is carried by the system, crontab in Linux and scheduled tasks in Windows.

Previously, I used the timer task work function provided by php. I found that the implementation was very simple and there were no many problems in the middle. At the same time, I was not very familiar with functions such as ob_flush () and flush, later I read some related articles,

I have a deep understanding of the content. However, after the php scheduled task is enabled, it seems that the task cannot be stopped and the server does not need to be restarted. You cannot modify the PHP file.

Later I planned to use crontab in Linux. This time, we use Ubuntu to deploy the system and nginx as the server. This system comes with crontab, so you do not need to install it yourself.

Enable crontab log

Crontab is disabled by default. Therefore, it cannot be executed at the beginning, but cannot be found if you want to view logs. So enable the log of the scheduled task to view

Modify the rsyslog service and delete # Before # cron. * In the/etc/rsyslog. d/50-default.conf file. Run the following command to restart the rsyslog service:

Service rsyslog restart;

Then restart the crontab service:

Service cron restart;

Many people on the Internet say that to enable, stop, or restart the contab service, run the command "service crond restart". When you execute this command, the unrecognised service will be reported. Here, change "crond" to "cron.

Then, you can view the log files of the scheduled task at/var/log/cron. log;

The following command can also view the crontab status

Service cron status;

View the log file at run time. If a statement is executed in the log file, the following occurs:

No MTA installed, discarding output

When executing the script, crontab will not directly output the error information, but will be sent to the mailbox in the form of an email. If the email server is not installed, it will report this error.

You can add the following content after each scheduled script:

>/Dev/null 2> & 1

Input all information to the empty device/dev/null in linux.

You can solve the problem of No MTA installed and discarding output.

Write scheduled tasks

Run the following command to enable the scheduled file of the current user. The file is stored in the/var/spool/cron/crontabs folder.

File named after

Crontab-e

You can use the following command to list the details of a user's cron service.

Crontab-l

I first wrote a scheduled task to test the following, and used php to write a data entry to the database every minute. The scheduled task can execute a url every minute.

1 * curl servername/Home/Test/test>/dev/null 2> & 1

Curl is also provided by Ubuntu without additional installation. This command can be executed independently on the command line.

It seems that the crontab file cannot be executed. Later, the crontab file cannot be written in this way every minute. The minute bit must be written as */1 before execution. Below are several commands copied from others, but I have tried

Copy code

Run */5 * every five minutes ****

0 *** per hour ****

Execute 0 0 every day ***

Execute 0 0 every week ** 0

0 0 1 per month **

Execute 0 0 1 1 * every year *

Copy code

I also run the following command before it can be used in the end to grant permissions to the root user. However, when I finally run the command, I don't know whether the command works. But write it first. Restart the crontab service.

Crontab-u root/var/spool/cron/crontabs/root

When I was not able to run it before, many people said that it was an environment variable and needed to modify it. But I did not meet this time. But write it down first. It may be used later.

I am using a scheduled task written by the root user. I can see the following content written by others. I will try it next time.

/Etc/cron. deny indicates that the crontab command cannot be used.

/Etc/cron. allow indicates the user who can use crontab.

If two files exist at the same time,/etc/cron. allow takes priority.

If neither of the two files exists, only the Super User can schedule a job.

Use crontab in Linux to create scheduled tasks

Routine scheduling of crontab in Linux

Linux crontab does not run troubleshooting

Ubuntu uses crontab for scheduled tasks

Linux scheduled task (at batch crontab anacron)

Linux task scheduler (at, crontab)

This article permanently updates the link address:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.