Linux Command crontab timed task instance

Source: Internet
Author: User

In the Linux server deployment timing tasks, often use crontab, mainly for the execution of Web site timing scripts, system maintenance, backup timing tasks, this article describes crontab deployment tasks and commands


The Linux system uses crontab to run scheduled tasks, which are timed tasks, and the basic syntax format is:

2 */1 * * */data/vhosts/example/cronjob/_run_1_hour.sh

The sh script file is a specific execution file, such as running one or more scripts with PHP:

Shell

#!/bin/sh
Cd/data/vhosts/web/cronjob
Runpath=/data/vhosts/web/cronjob
phpexec=/usr/local/php-fcgi/bin/php

# Run this script once per hour

$PHPEXEC $RUNPATH/test_crontab.php

#!/bin/sh
Cd/data/vhosts/web/cronjob
Runpath=/data/vhosts/web/cronjob
phpexec=/usr/local/php-fcgi/bin/php

# Run this script once per hour

$PHPEXEC $RUNPATH/test_crontab.php

The format description for the crontab command is as follows:

Crontab

Minute: Represents a minute, which can be any integer from 0 to 59.

Hour: Represents an hour, which can be any integer from 0 to 23.

Day: Represents a date, which can be any integer from 1 to 31.

Month: Represents the month, which can be any integer from 1 to 12.

Week: Indicates the day of the week, can be any integer from 0 to 7, where 0 or 7 represent Sunday.

Command: The commands you want to execute, either system commands or script files that you write yourself.

A few useful crontab commands

Crontab-l, list all scheduled tasks.

Crontab-e, edit Scheduled Tasks, which you can use to add scheduled tasks.

For example, execute once per minute:

0 */1 * * */data/vhosts/web/cronjob/_run_1_hour.sh >>/data/vhosts/web/log/_run_1_hour_stop_tips.log

This write can also record the contents of the script input to the log file. If you need to append rather than overwrite, you can use >> instead of >

/etc/crontab,/etc/cron.deny,/etc/cron.allow file Introduction

The task of scheduling the system is generally stored in the/etc/crontab file, which contains some of the system running scheduler, through the command we can look at the contents:


[Root@xiaoluo ~]# Cat/etc/crontab
Shell=/bin/bash
Path=/sbin:/bin:/usr/sbin:/usr/bin
Mailto=root
home=/

# for details, man 4 crontabs

# Example of Job definition:
#.----------------Minute (0-59)
# | .-------------Hour (0-23)
# |  | .----------Day of Month (1-31)
# |  |  | .-------month (1-12) OR jan,feb,mar,apr ...
# |  |  |  | .----Day of Week (0-6) (sunday=0 or 7) or Sun,mon,tue,wed,thu,fri,sat
# |  |  |  | |
# * * * * * user-name command to be executed

These tasks will be automatically scheduled when the system is running. /etc/cron.deny and/etc/cron.allow files are also stored in the/etc directory.

/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 first.

If none of the two files exist, only the root user can schedule the job.

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.