收藏文章 楼主

Linux crontab crond 计划任务添加 和 无法执行脚本问题解决 centos debian ubuntu

版块:linux   类型:普通   作者:小绿叶技术博客   查看:531   回复:0   获赞:0   时间:2020-09-19 11:32:13

Linux计划任务添加

yum install -y cronie    # centos 安装crontab crond

apt install -y cron         # ubuntu debian 方式安装


计划任务添加方法:

vi /etc/crontab

1 */1 * * * root sh /anfang/ip/ip.sh         # 每小时的零1分钟执行, 以root用户执行命令 sh 一个脚本

1 */2 * * *                                               # 每两小时里面的  每分钟


chmod +x /etc/crontab                         # 给计任务附加执行权限

systemctl restart crond                          # centos 重启计划任务生效

systemctl restart cron                           # ubuntu debian重启计划任务生效


其他方案:crontab -e   方式编写


*/1 * * * * root echo `date` > /root/1.txt


这个是每分钟打印时间到文件


systemctl 系统服务参考:https://www.eisc.cn/index.php?c=read&id=1060&page=1

crontab 无法执行脚本


正确案例:*/1 * * * * root /yunxiang/2.sh


错误:*/1 * * * * root  sh /yunxiang/2.sh

多了sh  ,其他服务器可以加sh的


############  crond 服务命令  ############

/sbin/service crond start  # 启动服务

service crond status # 查看状态

/sbin/service crond stop  # 关闭服务

/sbin/service crond restart # 重启服务

/sbin/service crond reload # 重新载入配置

chkconfig –level 35 crond on # 加入开机启动

ntsysv  # 查看状态


cd /etc/logrotate.d/        # 处理任务的路径

less syslog  # 查看系统日志


ls /etc/ | grep cron* # 过滤cron 任务路径

cron.d                  # 同路径2 ,可以写定时任务 

cron.deny               # 控制普通用户使用定时任务crontab



crond常用命令

systemctl start   crond         启动服务

systemctl stop    crond         停止服务

systemctl restart crond         重启服务

systemctl reload  crond         重载配置文件

systemctl status  crond         查看状态

crontab常用命令

crontab -u         设定某个用户的cron服务

crontab -l               显示crontab文件(显示已设置的定时任务)

crontab -e 编辑crontab文件(编辑定时任务)

crontab -r 删除crontab文件(删除定时任务)

crontab -i        删除crontab文件提醒用户(删除定时任务)


#   crontab -e 写入的文件路径是:/var/spool/cron/crontabs/eisc

#  写入后 需要重启 crontab :    sudo service cron restart



############  crond 基本用法  ############


crontab [-u user] file

        crontab [-u user] [ -e | -l | -r ]

                (default operation is replace   [dɪˈfɔlt] 默认   [ˌɑpəˈreʃn] 操作 是 [ˌriˈples] 替换)

        -e      (edit user's crontab    [ˈɛdət] 编辑   [ˈjuzər] 用户的  )

        -l      (list user's crontab  [lɪst] 列出  用户的  )

        -r      (delete user's crontab    [dɪˈlit] 删除  用户的 )

        -i      (prompt before deleting user's crontab    [prɑmpt] 提示 [bɪˈfɔr] 以前 [dɪ'litɪŋ]  删除 用户的 

删除用户前的提示)

        -s      (selinux context   [ˈkɑntɛkst] 环境)


-r  删除某个用户的cron服务(慎用)



############  cat /etc/crontab ############


SHELL=/bin/bash # 变量指定了系统要使用哪个shell,这里是bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

# PATH变量指定了系统执行命令的路径

MAILTO=root # mail to电子邮件任务执行信息发送给root用户


# For details see man 4 crontabs

# details ['di:teɪlz] 详情,see  [siː] 领悟,参见


# Example of job definition:   工作定义示例

# [ɪgˈzæmpəl] 示例, [ʌv] 属于...的, [ʤɑb] 工作, [ˌdɛfəˈnɪʃən] 定义


# .----------------  minute (0 - 59) [ˈmɪnɪt]  分钟

# |  .-------------  hour (0 - 23) [ˈaᴜər] 小时

# |  |  .---------- day of month (1 - 31) [de] 天,[ʌv] 属于...的,[mʌnθ] 月:每月的第几天

# |  |  |

# |  |  |  .-------  month (1 - 12)  [mʌnθ] 月,1到12月

# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) 

# |  |  |  |  | [wik] 周, 周的第几天:星期几;

# |  |  |  |  |

# * * *  *  * user-name  command to be executed

用户名字, [kəˈmænd] 命令, [tu bi] 的, ['ɛksɪ,kjʊtɪd] 执行

# 分  时  日  月  周   命令


1 * * * *   # 每小时的第一分钟,小时:1分  如:1:01 2:01  3:01  为等效于每小时,是属于时钟



# 星号(*) 代表所有可能的值

# 逗号(,)  列表范围: 1,2,3,4

# 中杠(-) 整数范围1到4: 1-4  

# 正斜线(/) 时间的间隔频率: 0-23/2  每两个小时执行一次


5 * * * * Command                          每小时的第5分钟执行一次命令

30 18 * * * Command                      指定每天下午的 6:30 执行一次命令 

30 7 8 * * Command                       指定每月8号的7:30分执行一次命令 

30 5 8 6 * Command                      指定每年的6月8日5:30执行一次命令 

30 6 * * 0 Command                       指定每星期日的6:30执行一次命令

*/1 * * * * root  每分钟执行  * 号表示每(每分,每秒);  */2 每两分钟

0 4 1 jan * 1月1号4:00 

0 11 4 * mon-wed  每月的4号与每周一到周三的11点

mon 星期1; wed 星期3


1 */1 * * * root sh /anfang/ip/ip.sh  # 每小时的零1分钟执行

1 */2 * * *                                        # 每两小时里面的  每分钟


20-30/1 1-10/5 * *  * # 1点的20-30分和6点的20-30分执行

# 注意开始时间也要执行,先有时间,然后才有间隔




一月 January  [ˈʤænjuˌɛri] 八月 August 

二月 February  [ˈfɛb(j)uˌɛri] 九月 September 

三月 March  十月 October

四月 April  七月 July  

五月 May  十一月 November

六月 June  十二月 December



星期一Monday  星期二Tuesday 

星期三Wednesday  星期四Thursday 

星期五Friday  星期六Saturday 

星期天Sunday


注意:

    数字的表示最好用2为阿拉伯数字显示

    周和日最好不要同时用

    定时任务要加注解

    可以定向到日志文件或者空文件

    定时任务一定是绝对路径,且目录必须存在才能出结果

    crontab 服务一定要开启运行




############  crond 定义定时任务  ############


vi /etc/crontab # 编辑任务配置文件,追加一行任务


*/1 * * * * root /root/1.sh

# 每 1分钟执行一次,使用root用户 root 执行脚本/root/1.sh

# 直接将 /root/1.sh  修改为命令即可,如:echo "1111" >> 1.txt

# */1 * * * * root  reboot   每分钟重启服务器


systemctl restart crond  # 重启服务,开始执行计划任务




vi 1.sh

#!/bin/bash


# catalog=(                                     # 素组的格式是小括号:请将IP地址放置括号内,分隔符是换行或者空格

# 111                                    

# 222

# )


# catalog[$i] # 指定数组角标对应的元素

${#filecatalog[*]} # 素组元素个数


read -p "请输入目录:" catalog

 [ˈkætəlɔg]目录

filecatalog=(

$(ls $catalog)

)

quantity=${#filecatalog[*]} #  [ˈkwɑn(t)ədi] 数量;kuan t ti

echo "当前目录(数组)中文件个数数量为:$quantity"

frequency=$quantity # [ˈfrikwənsi] 次数; fei ou kuan si

for((i=1;i<=$frequency; i++))

do

sleep 1

echo "

`date`

正在依次打印改目录的的文件信息:  ${filecatalog[$i]} 

"

done

sed -i "/^$/d" 1.sh ; chmod 755 1.sh 






# =========

其他方式执行命令:

crontable -e  #写入命令

crontable -l #查看记录

提供企业建站服务,免费网防系统,提交信息登录 http://yundun.ddoss.cn 邮箱: proposal@ddoss.cn 
回复列表
默认   热门   正序   倒序

回复:Linux crontab crond 计划任务添加 和 无法执行脚本问题解决 centos debian ubuntu

头像

用户名:

粉丝数:

签名:

资料 关注 好友 消息