首页 > 系统相关 >基于Linux下的定时任务

基于Linux下的定时任务

时间:2023-01-03 12:36:17浏览次数:42  
标签:基于 foundation21 westos crontab cron Linux 定时 root


定时任务:

[root@foundation21 ~]# systemctl status crond.service  首先查看定时服务是否开启

基于Linux下的定时任务_linux

[root@foundation21 ~]# crontab -u root -e    建立定时任务,注意使用date命令看一下系统时间,有时候系统时间和真实时间是不一致

no crontab for root - using an empty one

crontab: installing new crontab

[root@foundation21 ~]# crontab -u root -l     查看定时任务

54 21 * * * /bin/rm -fr /mnt/file*

[root@foundation21 ~]# cat /var/spool/cron/root   查看自己编写的内容,到自己的定时之后就自动执行命令。

格式说明:

54     21     *      *    *       /bin/rm -fr /mnt/file*

分钟  小时    天    月  周       意思是每天的九点五十四清空一次/mnt/目录下的file文件

[root@foundation21 ~]# crontab -u root -e    重新建立定时任务

crontab: installing new crontab

[root@foundation21 ~]# crontab -u root -l    查看定时任务

56 09 * * * /bin/rm -fr /mnt/file*

[root@foundation21 ~]# crontab -u root -r    删除定时任务

[root@foundation21 ~]# crontab -u root -l    查看定时任务已经被删除

no crontab for root

[root@foundation21 ~]# cat /var/spool/cron/root   查看文件内容,文件被删掉

cat: /var/spool/cron/root: No such file or directory

基于Linux下的定时任务_vim_02

crontab定时命令的黑白名单:

[root@foundation21 ~]# crontab -e     超级用户可以执行定时任务

no crontab for root - using an empty one

crontab: installing new crontab

[root@foundation21 ~]# su - westos    切换到普通用户,用户直接使用useradd建立即可

Last login: Sun Apr 22 10:04:43 CST 2018 on pts/3

[westos@foundation21 ~]$ crontab -e   普通用户可以执行定时任务

no crontab for westos - using an empty one

crontab: installing new crontab

[westos@foundation21 ~]$ logout

[root@foundation21 ~]# su - linux      切换到普通用户

Last login: Sun Apr 22 10:04:51 CST 2018 on pts/3

[linux@foundation21 ~]$ crontab -e     普通用户可以执行定时任务

no crontab for linux - using an empty one

crontab: installing new crontab

[linux@foundation21 ~]$ logout

基于Linux下的定时任务_linux_03

[root@foundation21 ~]# vim /etc/cron.deny

[root@foundation21 ~]# cat /etc/cron.deny   将westos用户加入黑名单

westos

[root@foundation21 ~]# su - westos

Last login: Sun Apr 22 10:14:14 CST 2018 on pts/0

[westos@foundation21 ~]$ crontab -e         不可以执行定时任务

You (westos) are not allowed to use this program (crontab)

See crontab(1) for more information

[westos@foundation21 ~]$ logout

[root@foundation21 ~]# su - linux

Last login: Sun Apr 22 10:14:31 CST 2018 on pts/0

[linux@foundation21 ~]$ crontab -e          不在黑名单用户可以执行定时任务

crontab: installing new crontab

[linux@foundation21 ~]$ logout

基于Linux下的定时任务_vim_04

[root@foundation21 ~]# vim /etc/cron.deny   删除黑名单

[root@foundation21 ~]# vim /etc/cron.allow

[root@foundation21 ~]# cat /etc/cron.allow  将westos用户加入白名单

westos

[root@foundation21 ~]# su - linux

Last login: Sun Apr 22 10:16:00 CST 2018 on pts/0

[linux@foundation21 ~]$ crontab -e          不在白名单用户不可以执行定时任务

You (linux) are not allowed to use this program (crontab)

See crontab(1) for more information

[linux@foundation21 ~]$ logout

[root@foundation21 ~]# su - westos

Last login: Sun Apr 22 10:15:54 CST 2018 on pts/0

[westos@foundation21 ~]$ crontab -e         白名单用户可以执行定时任务

crontab: installing new crontab

[westos@foundation21 ~]$ logout

[root@foundation21 ~]# cat /etc/cron.allow

westos

[root@foundation21 ~]# vim /etc/cron.allow   删除白名单用户

基于Linux下的定时任务_vim_05

定时任务的另外一种表示:

[root@foundation21 ~]# cd /etc/cron.d       切换路径

[root@foundation21 cron.d]# vim westos      

[root@foundation21 cron.d]# cat westos      建立五个文件

基于Linux下的定时任务_定时任务_06

* * * * * root touch /mnt/xfl{1..5}

基于Linux下的定时任务_定时任务_07

[root@foundation21 cron.d]# vim westos

[root@foundation21 cron.d]# cat westos

* * * * * root rm -fr /mnt/xfl{1..5}      删除五个文件

基于Linux下的定时任务_定时任务_08

标签:基于,foundation21,westos,crontab,cron,Linux,定时,root
From: https://blog.51cto.com/u_13831562/5985259

相关文章

  • 基于Linux下的临时文件的管理
    对临时文件的管理:[[email protected]]#cd/usr/lib/tmpfiles.d/ 切换路径[[email protected]]#vimwestos.conf  [[email protected]]......
  • 基于linux下的shell脚本练习
    shell脚本的简介:打开文本编辑器(可以使用vi/vim命令来创建文件),新建一个文件test.sh,扩展名为sh(sh代表shell),扩展名并不影响脚本执行,见名知意就好,如果你用php写shell脚本,扩......
  • 基于Linux下的yum源的搭建与共享
    yum命令:###yum命令仅仅是对软件进行管理,rpm命令才是真正安装软件的,yum的好处是可以解决软件依赖性。yum源的搭建:mkdir/iso    建立个目录mv/home/kiosk/Desktop/*.is......
  • 基于Linux下的虚拟机安装详解
    首先我们打开一个shell切换到超级用户,输入如下命令:[kiosk@foundation21Desktop]$pwd/home/kiosk/Desktop[kiosk@foundation21Desktop]$su-rootPassword:Lastlogin:F......
  • 利用Barrier使局域网下的Linux(Majaro)和Window10共享同一套键盘鼠标
    项目场景:现有两台电脑:一台笔记本(Win10)连接无线网(DHCP)一台工作站(Linux-Manjaro)连接无线网(DHCP)为了简化桌面、便于操作,打算让笔记本和工作站共用一套键盘鼠标。问题分......
  • Linux下的pip和pip3的冲突问题
    大佬们有啥解决办法还请不吝赐教!前言最近在使用pip命令的时候,总是报如下错误:查询了很多解决方式还是未能解决.但是pip3命令的使用却没任何问题.问题初步解决今天翻看到......
  • 用Xftp给linux虚拟机传输文件时失败:Permission is not allowed!
    一直提示错误:点开发现了传输错误信息 Permissionisnotallowed!意思就是权限不够,那就到虚拟机更改该文件夹的权限。sudochmod777opt ......
  • linux回炉第十三天
    systemdsystemctlis-activenginxsystemctlstatusnginxsystemctlstartnginxsystemctlstopnginxsystemctlenablenginxsystemctldisablenginxsystemctlenable--n......
  • 定时调度插件------Longbow.Tasks
    官网地址Longbow.Tasks使用说明dll引用使用NuGet搜索Longbow.Task可找到相关版本的dll目前最新的为7.0.0版本,需net6.0+如果低版本用户可使用5.2.1直接下载源码......
  • Linux安装node
    说明亲测在Ubuntu环境下安装成功如果包管理器中有合适版本建议使用apt-get或yum等命令直接安装以Ubuntu系统为例,可以使用如下命令查看nodejs版本sudoapt-getupdat......