⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 crontab

📁 linux下的命令
💻
字号:
NAME       crontab - maintain crontab files for individual users (ISC Cron V4.1)SYNOPSIS       crontab [-u user] file       crontab [-u user] [-l | -r | -e] [-i] [-s]       -e      (edit user's crontab)-l      (list user's crontab)-r      (delete user's crontab)-i      (prompt before deleting user's crontab)-s      (selinux context) DESCRIPTION       Crontab  is  the  program used to install,deinstall or list the tables       used to drive the cron(8) daemon in ISC Cron.Each user can have their       own  crontab,  and though these are files in /var/spool/ , they are not       intended to be edited directly. For SELinux in mls  mode  can  be  even       more crontabs - for each range. For more see selinux(8).       If the cron.allow file exists, then you must be listed there in order       to be allowed to use this command.  If the  cron.allow  file  does  not       exist but the cron.deny file does exist, then you must not be listed in       the cron.deny file in order to use this command.  If neither  of  these       files  exists, only the super user will be allowed to use this command.              OPTIONS-u     It specifies the name  of  the  user  whose  crontab  is  to  be        tweaked.If this option is not given, crontab examines "your"        crontab, i.e., the crontab of the person executing the  command.        Note  that su(8) can confuse crontab and that if you are running        inside of su(8) you should always use the -u option for safety’s        sake. The  first form of this command is used to install a new        crontab from some named file or standard input  if  the  pseudo-        filename "-" is given.-l     The current crontab will be displayed on standard output.-r     The current crontab will be be removed.-e     This option is used to edit the current crontab using the editor        specified by the VISUAL or EDITOR environment variables.After        you exit from the editor,the modified crontab will be installed        automatically.-i     This option modifies the -r option to  prompt  the  user  for  a        ’y/Y’ response before actually removing the crontab.        -s     It will append the current SELinux security context string as an        MLS_LEVEL setting to the crontab file before editing /replace-        ment  occurs - see the documentation of MLS_LEVEL in crontab(5).                  cron是一个linux下的定时执行工具,可以在无需人工干预的情况下运行作业。由于Cron是Linux的内置服务,但它不自动起来,可以用以下的方法启动、关闭这个服务:      /sbin/service crond start //启动服务      /sbin/service crond stop //关闭服务      /sbin/service crond restart //重启服务      /sbin/service crond reload //重新载入配置      你也可以将这个服务在系统启动的时候自动启动:      在/etc/rc.d/rc.local这个脚本的末尾加上:      /sbin/service crond start  现在Cron这个服务已经在进程里面了,我们就可以用这个服务了,Cron服务提供以下几种接口供大家使用:1.直接用crontab命令编辑  cron服务提供crontab命令来设定cron服务的,以下是这个命令的一些参数与说明:  crontab -u //设定某个用户的cron服务,一般root用户在执行这个命令的时候需要此参数  crontab -l //列出某个用户cron服务的详细内容  crontab -r //删除某个用户的cron服务  crontab -e //编辑某个用户的cron服务  比如说root查看自己的cron设置:    crontab -u root -l  再例如,root想删除fred的cron设置:    crontab -u fred -r  在编辑cron服务时,编辑的内容有一些格式和约定,输入:    crontab -u root -e     进入vi编辑模式,编辑的内容一定要符合下面的格式:*/1 * * * * ls >> /tmp/ls.txt这个格式的前一部分是对时间的设定,后面一部分是要执行的命令,如果要执行的命令太多,可以把这些命令写到一个脚本里面,然后在这里直接调用这个脚本就可以了,调用的时候记得写出命令的完整路径。          时间的设定我们有一定的约定,前面五个*号代表五个数字,数字的取值范围和含义如下:      分钟 (0-59)      小時 (0-23)      日期 (1-31)      月份 (1-12)      星期 (0-6)//0代表星期天      除了数字还有几个特殊的符号就是"*"、"/"和"-"、",",*代表所有的取值范围内的数字,"/"代表每的意思,"*/5"表示每5个单位,"-"代表从某个数字到某个数字,","分开几个离散的数字。            以下举几个例子说明问题:      每天早上6点      0 6 * * * echo "Good morning." >> /tmp/test.txt      		//注意单纯echo,从屏幕上看不到任何输出,因为cron把任何输出都email到root的信箱了。      每两个小时      0 */2 * * * echo "Have a break now." >> /tmp/test.txt      晚上11点到早上8点之间每两个小时,早上八点      0 23-7/2,8 * * * echo "Have a good dream:)" >> /tmp/test.txt      每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点      0 11 4 * 1-3 command line      1月1日早上4点      0 4 1 1 * command line      每次编辑完某个用户的cron设置后,cron自动在/var/spool/cron下生成一个与此用户同名的文件,此用户的cron信息都记录在这个文件中,这个文件是不可以直接编辑的,只可以用crontab -e 来编辑。cron启动后每过一份钟读一次这个文件,检查是否要执行里面的命令。因此此文件修改后不需要重新启动cron服务。      2.	编辑/etc/crontab文件配置cron    cron服务每分钟不仅要读一次/var/spool/cron内的所有文件,还需要读一次/etc/crontab,因此我们配置这个文件也能运用 cron服务做一些事情。用crontab配置是针对某个用户的,而编辑/etc/crontab是针对系统的任务。        此文件的文件格式是:  SHELL=/bin/bash  PATH=/sbin:/bin:/usr/sbin:/usr/bin  MAILTO=root 	//如果出现错误,或者有数据输出,数据作为邮件发给这个帐号  HOME=/ 				//使用者运行的路径,这里是根目录  # run-parts  01 * * * * root run-parts /etc/cron.hourly //每小时执行/etc/cron.hourly内的脚本  02 4 * * * root run-parts /etc/cron.daily //每天执行/etc/cron.daily内的脚本  22 4 * * 0 root run-parts /etc/cron.weekly //每星期执行/etc/cron.weekly内的脚本  42 4 1 * * root run-parts /etc/cron.monthly //每月去执行/etc/cron.monthly内的脚本		大家注意"run-parts"这个参数了,如果去掉这个参数的话,后面就可以写要运行的某个脚本名,而不是文件夹名了。

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -