Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of creating scheduled tasks by using crond tools in Linux

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Preface

Crond is a timing execution tool under linux (equivalent to scheduled task under windows), which can run tasks regularly without human intervention. The crond tool provides crontab commands to set scheduled tasks. It is a daemon and can only be accurate to minutes. It can be set to execute Linux commands or Shell scripts periodically. Every minute crond will check whether there are any scheduled tasks that need to be executed. The experimental system is CentOS 7.

Operation steps

(1) check whether the crond tool is installed

Yum list installed | grep crontabs

If it is not installed, install it using the following command

Sudo yum install crontabs

(2) check whether the crond service is enabled, so use the systemctl command instead of the service command because it is CentOS7.

Systemctl status crond.service

If it is not enabled, start the service using the command shown below

Sudo systemctl start crond.service

(3) how to use the crontab command to create a task plan using crond tool

Usage: crontab [options] file crontab [options] crontab-n [hostname] Options:-u define user-e edit user's crontab-l list user's crontab-r delete user's crontab-i prompt before deleting-n set host in cluster to run users' crontabs-c get host in cluster to run users' crontabs-s selinux context-x enable debugging # Note crontab-r is to delete all scheduled tasks for the user (use with caution!)

You can view the task definition format and set the task execution environment through the / etc/crontab file

Take "write the date to the specified file at the same time every minute" as an example.

Method 1: use the crontab command to edit the current user scheduled task (effective immediately) * *

Crontab-e

Insert the following instruction in the editor (be careful not to append the user at this time, otherwise it cannot be executed, because this method is to set the current user's scheduled task directly)

* / 1 * date > > / home/TomAndersen/currentDate

Check the insertion result

[tomandersen@hadoop101 bin] $crontab-lump date 1 * date > > / home/TomAndersen/currentDate

Method 2: edit / etc/crontab file and insert it according to format (slow to take effect) * *

* / 1 * tomandersen date > > / home/TomAndersen/currentDate

(4) check whether the setting is successful.

[tomandersen@hadoop101 bin] $cat / home/TomAndersen/currentDate 09 Sunday 18:12:01 CST2020 09 Sunday 18:13:01 CST2020 09 Sunday 18:14:01 CST2020 09 Sunday 18:15:01 CST2020 09 Sunday 18:16:02 CST2020 09 Sunday 18:17:01 CST2020 09 Sunday 18:18:01 CST2020 Sunday, February 9, CST2020, 18:19:01, Sunday, February 9, 2000, 18:20:01 CST

Summary

The above is the editor to introduce to you in the Linux using crond tools to create scheduled tasks, I hope to help you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report