Introduction to the Cron task for Linux

Source: Internet
Author: User

Cron Introduction
    • The Linux system has a process cron dedicated to managing timed tasks, typically set to boot, and can be viewed with the following commands
ps aux | grep cron
    • The cron process reads the/etc/crontab file every other minute,/etc/cron.d/all crontab files under the/var/spool/cron/, and all the crontab files under it;
    1. /var/spool/cron/: Change directory under the system of each user needs to perform the crontab task, the file has the user's name command, is the user after the Cron task is submitted automatically created, a user only a crontab file;

    2. /etc/crontab: This is a crontab file, which holds the entire system level of the task, is generally the root user operation, ordinary users do not modify this file;

    3. /etc/cron.d/: This folder is generally a system-level crontab file for managing other scheduled tasks;

    4. /var/log/cron This is the log file that the cron task runs;

    • So if the user's own task will appear in the/var/spool/cron/directory;
# /etc/crontab文件SHELL=/bin/bash  # 指定运行的shellPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=root  # 当/etc/crontab文件中的例行性命令发生错误时,会将错误信息传递给谁,一般改成用户账户,当发生错误时,错误信息发送到/var/spool/mail/下的用户名的文件# For details see 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

Extended

    • by ls/etc/| The grep cron command looks at several files or folders that appear:
anacrontab  # 这是系统级别的用来管理异步定时任务的文件cron.d  # 系统级别定时任务文件cron.daily  # 每天需要执行的cron任务目录cron.deny   # 定义不允许使用crontab命令的用户文件cron.hourly # 每小时需要执行的cron任务目录cron.monthly # 每个月需要执行的cron任务目录crontab   # 这是系统级别的用来管理定时任务的文件cron.weekly # 每周需要执行的cron任务目录# 另外还有cron.allow文件,定义允许使用crontab命令的用户,系统默认没有
Operation of the Cron task
    • The Cron tool has its own operation commands to automate the management of the user's cron tasks.

    • Management of Cron Services

service crond start    //启动cron进程service crond stop     //关闭cron进程service crond restart  //重启cron进程service crond reload   //重新载入配置+service crond status   //查看服务状态
    • If the system does not recognize the service command, use the/usr/sbin/crond start Format command;

    • Crontab Task Command

crontab [-u username] [-l|-e|-r]选项与参数:-u  :root权限才能进行这个任务,缺省时默认为当前的用户-e  :编辑用户的crontab文件,保存好后会自动提交-l  :查阅当前用户的crontab的工作内容-r  :移除当前用户所有的 crontab 的工作内容,若仅要移除一项,请去修改crontab文件
    • We can use CRONTAB-E to edit our scheduled tasks directly, or we can edit a custom creontab file and submit it.
Edit your own cron task
    • The first step: Choose a folder, such as the one you want to perform timed tasks, such as the installation file for Redis, to create a script file:
touch test# 授予该文件可执行权限chmod +x test# 编辑文件vi test
    • The syntax of the cron task;
# 格式minute hour day-of-month month-of-year day-of-week commandsminute:分钟,范围0--59;hour:小时,范围0--23;day-of-month:天,范围01-31;month-of-year:月份,01-12;day-of-week:周,范围0-6,(0表示Sunday);
    • Example
# 每隔五分钟*/5 * * * * echo "aaaa"# 每天18点0 18 * * * echo "aaa"# 执行目录下的文件0 18 * * * root run-parts /etc/cron.d/ # 执行目录下的所有文件
    • Step two: Submit a task
crontab 文件名
    • Step three: See if the task was successfully submitted
crontab -l
    • Note: Each commit task is to overwrite the original crontab file, so you need to add more cron tasks when modifying the files under/var/spool/cron/;

Introduction to the Cron task for Linux

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.