Adding cron tasks to Linux

Source: Internet
Author: User

/sbin/service Crond Start//Startup service

/sbin/service Crond stop//Shut down service

/sbin/service crond Restart//Restart service

/sbin/service Crond Reload//Reload Configuration

You can also start the service automatically when the system starts:

At the end of the/etc/rc.d/rc.local script, add:

/sbin/service Crond Start

    1. Crontab-u//Set a user's Cron service, which is usually required by the root user when executing this command

      CRONTAB-L//list details of a user cron service

      Crontab-r//Delete a user's cron service

      CRONTAB-E//Edit a user's cron service

$ sudo crontab-u <username>-e

    1. For example, root to view your cron settings: Crontab-u root-l

      Again, for example, Root wants to delete Fred's cron settings: Crontab-u fred-r

You will see a text editing window where you can add or edit cron tasks. The Nono editor is used by default.

The format for each cron task is as follows.

    1. < minutes > < hours > < > < month > < week > < command >

The first 5 elements define the schedule for the task, and the last element is the full path to the command or script.

Here are some examples of cron tasks.

    • * * * * * * /home/dan/bin/script.sh: Run every minute.
    • 0 ** * */home/dan/bin/script.sh: Run every hour.
    • 0 0 ** */home/dan/bin/script.sh: Run at 0 o ' Day.
    • 0 9,18* * */home/dan/bin/script.sh: Run at 9AM and 6PM per day.
    • 0 9-18* * */home/dan/bin/script.sh: Runs every hour from 9AM to 6PM.
    • 0 9-18 * * 1-5/home/dan/bin/script.sh: 9AM to 6PM per hour from Monday to Friday.
    • */10* * * */home/dan/bin/script.sh: Runs every 10 minutes.

Once you have completed the above setup steps, press Ctrl+x to save and exit the editor. At this point, the new scheduled task should already be activated.

The crontab file under/var/spool/cron cannot be created directly or modified directly. The crontab file is obtained through the crontab command. Now suppose you have a user named Foxy, and you need to create your own crontab file. You can start by using any text editor to create a new file, and then write to it the command you want to run and the time you want to perform it periodically.

Then save the disk and exit. Assume that the file is/tmp/test.cron. After that, use the crontab command to install the file, making it the user's crontab file. Type:

Crontab Test.cron

When the program is executed at the time you specify, the system will send you a letter showing what the program is doing, and if you do not wish to receive such a letter, please add >/dev/null 2>&1 after each line is empty.

Adding cron tasks to Linux

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.