Linux to create a scheduled task to use tools crond

Foreword

  • crond is a regular implementation of the tool under linux (equivalent scheduled task under windows), the task can be run periodically without the need for manual intervention. crond crontab command to set the tool provides regular tasks, are daemon, can only be accurate to the minute, can be set to periodically execute Linux commands or Shell script, every minute crond checks for regular tasks to be performed
  • The experimental system is CentOS 7

Steps

(1) Check the crond tool is installed

yum list installed | grep crontabs

Check crond tool is installed

  • If not installed, the installation using the command shown in the following
sudo yum install crontabs

(2) Check the crond service is turned on

  • Because it is so CentOS7 use systemctl command, rather than the service command
systemctl status crond.service

Check the crond service is open

  • If not turned on, use the command shown in the following Open Service
sudo systemctl start crond.service

(3) using the Task Scheduler tool to create crond

  • crontab command to use
Usage:
 crontab [options] file
 crontab [options]
 crontab -n [hostname]

Options:
 -u <user>  define user
 -e         edit user's crontab
 -l         list user's crontab
 -r         delete user's crontab
 -i         prompt before deleting
 -n <host>  set host in cluster to run users' crontabs
 -c         get host in cluster to run users' crontabs
 -s         selinux context
 -x <mask>  enable debugging
 # 注意 crontab -r 是删除用户的所有定时任务(慎用!)
  • You can view the job definition format and set the task execution environment by / etc / crontab file
    Task Definition Format

  • To "time to write the date specified file every minute" as an example

Method 1: Use the crontab command to edit the current user cron job (with immediate effect) **
crontab -e
  • Insert the following instruction in the editor (note that this time not adding a user, or can not be performed, because the current timing is provided directly to user tasks)
*/1 * * * * date >> /home/TomAndersen/currentDate
  • Check the insertion results
[tomandersen@hadoop101 bin]$ crontab -l
*/1 * * * * date >> /home/TomAndersen/currentDate
Method 2: Edit / etc / crontab file format according insert (Commencement slower) **
*/1 * * * * tomandersen date >> /home/TomAndersen/currentDate

(4) Check to see if the success

[tomandersen@hadoop101 bin]$ cat /home/TomAndersen/currentDate 
2020年 02月 09日 星期日 18:12:01 CST
2020年 02月 09日 星期日 18:13:01 CST
2020年 02月 09日 星期日 18:14:01 CST
2020年 02月 09日 星期日 18:15:01 CST
2020年 02月 09日 星期日 18:16:02 CST
2020年 02月 09日 星期日 18:17:01 CST
2020年 02月 09日 星期日 18:18:01 CST
2020年 02月 09日 星期日 18:19:01 CST
2020年 02月 09日 星期日 18:20:01 CST

End~

Published 15 original articles · won praise 5 · views 10000 +

Guess you like

Origin blog.csdn.net/TomAndersen/article/details/104237945