linux中普通用户的定时任务详解
程序员文章站
2022-04-19 11:23:00
前言
普通用户定义crontab定时任务:比如oracle用户定义一个定时任务:每分钟打印当前目录
[oracle@node2 ~]$ crontab -e...
前言
普通用户定义crontab定时任务:比如oracle用户定义一个定时任务:每分钟打印当前目录
[oracle@node2 ~]$ crontab -e
*/1 * * * * /bin/ls -al > /tmp/ls.log
如何判断定时任务有没有执行?首先crond服务要处于运行状态
[oracle@node2 ~]$ service crond status redirecting to /bin/systemctl status crond.service ● crond.service - command scheduler loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled) active: active (running) since sun 2018-07-15 08:27:38 edt; 2min 43s ago main pid: 6189 (crond) cgroup: /system.slice/crond.service └─6189 /usr/sbin/crond -n [oracle@node2 ~]$
1.在root用户下使用mail命令
[root@node2 ~]# mail heirloom mail version 12.5 7/5/10. type ? for help. "/var/spool/mail/root": 801 messages 9 new 94 unread n795 (cron daemon) sun jul 15 08:25 35/932 "cron <root@node2> /bin/ls" n796 (cron daemon) sun jul 15 08:26 35/932 "cron <root@node2> /bin/ls" n797 (cron daemon) sun jul 15 08:27 35/932 "cron <root@node2> /bin/ls" n798 (cron daemon) sun jul 15 08:28 35/932 "cron <root@node2> /bin/ls" n799 (cron daemon) sun jul 15 08:29 35/932 "cron <root@node2> /bin/ls" n800 (cron daemon) sun jul 15 08:30 35/932 "cron <root@node2> /bin/ls" & file "/var/spool/mail/root": 801 messages 9 new 94 unread & h >n793 (cron daemon) sun jul 15 08:23 35/932 "cron <root@node2> /bin/ls" n794 (cron daemon) sun jul 15 08:24 35/932 "cron <root@node2> /bin/ls" n795 (cron daemon) sun jul 15 08:25 35/932 "cron <root@node2> /bin/ls" n796 (cron daemon) sun jul 15 08:26 35/932 "cron <root@node2> /bin/ls" n797 (cron daemon) sun jul 15 08:27 35/932 "cron <root@node2> /bin/ls" n798 (cron daemon) sun jul 15 08:28 35/932 "cron <root@node2> /bin/ls" n799 (cron daemon) sun jul 15 08:29 35/932 "cron <root@node2> /bin/ls" n800 (cron daemon) sun jul 15 08:30 35/932 "cron <root@node2> /bin/ls" &800 message 800: from root@node2.matengbing.com sun jul 15 08:30:01 2018 return-path: <root@node2.matengbing.com> x-original-to: root delivered-to: root@node2.matengbing.com from: "(cron daemon)" <root@node2.matengbing.com> to: root@node2.matengbing.com subject: cron <root@node2> /bin/ls content-type: text/plain; charset=utf-8 auto-submitted: auto-generated precedence: bulk x-cron-env: <xdg_session_id=278> x-cron-env: <xdg_runtime_dir=/run/user/0> x-cron-env: <lang=en_us.utf-8> x-cron-env: <shell=/bin/sh> x-cron-env: <home=/root> x-cron-env: <path=/usr/bin:/bin> x-cron-env: <logname=root> x-cron-env: <user=root> date: sun, 15 jul 2018 08:30:01 -0400 (edt) status: r \ anaconda-ks.cfg desktop documents downloads initial-setup-ks.cfg music pictures public templates videos
在mial交互环境下,通过file命令显示当前邮件总数等信息通过head查看最近的邮件输入编号查看该邮件的详细信息
2.查看日志:tail -n 10 /var/log/cron
[root@node2 ~]# tail -n 10 /var/log/cron jul 15 08:31:01 node2 crond[6297]: (root) cmd (/bin/ls) jul 15 08:31:01 node2 crond[6298]: (oracle) cmd (/bin/ls -al > /tmp/ls.log ) jul 15 08:32:01 node2 crond[6321]: (oracle) cmd (/bin/ls -al > /tmp/ls.log ) jul 15 08:32:01 node2 crond[6322]: (root) cmd (/bin/ls) jul 15 08:33:02 node2 crond[6342]: (root) cmd (/bin/ls) jul 15 08:33:02 node2 crond[6343]: (oracle) cmd (/bin/ls -al > /tmp/ls.log ) jul 15 08:34:01 node2 crond[6362]: (root) cmd (/bin/ls) jul 15 08:34:01 node2 crond[6363]: (oracle) cmd (/bin/ls -al > /tmp/ls.log ) jul 15 08:35:01 node2 crond[6382]: (oracle) cmd (/bin/ls -al > /tmp/ls.log ) jul 15 08:35:01 node2 crond[6383]: (root) cmd (/bin/ls) [root@node2 ~]#
日志文件中记录了所有用户的定时任务执行情况
3.在普通用户下使用mail查看
(定时任务中要明确执行定时任务的用户,否则即使在oracle用户下定义的定时任务可能不能收到邮件)
[oracle@node2 ~]$ crontab -l */1 * * * * oracle /bin/ls -al > /tmp/ls.log [oracle@node2 ~]$ [oracle@node2 ~]$ mail heirloom mail version 12.5 7/5/10. type ? for help. "/var/spool/mail/oracle": 6 messages 1 unread 1 (cron daemon) mon mar 5 14:10 52/2476 "cron <oracle@localhost> ls -al" 2 (cron daemon) sun jul 15 06:10 57/2777 "cron <oracle@node2> /bin/ls -al" 3 (cron daemon) sun jul 15 06:11 57/2777 "cron <oracle@node2> /bin/ls -al" 4 (cron daemon) sun jul 15 06:12 57/2778 "cron <oracle@node2> /bin/ls -al" 5 (cron daemon) sun jul 15 06:13 57/2778 "cron <oracle@node2> /bin/ls -al" >u 6 (cron daemon) sun jul 15 08:38 26/929 "cron <oracle@node2> oracle /bin/ls -al > /tmp/ls.log" &
在普通用户下不能直接查看/var/log/cron文件
[oracle@node2 ~]$ tail -n 10 /var/log/cron tail: cannot open ‘/var/log/cron' for reading: permission denied you have new mail in /var/spool/mail/oracle [oracle@node2 ~]$
定时任务还有一种写在/etc/crontab文件中的方式,但是在centos7中写在该文件中执行会报错
[oracle@node2 ~]$ cat /etc/crontab shell=/bin/bash path=/sbin:/bin:/usr/sbin:/usr/bin mailto=root # for details see man 4 crontabs # example of job definition: # .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) or jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (sunday=0 or 7) or sun,mon,tue,wed,thu,fri,sat # | | | | | # * * * * * user-name command to be executed [oracle@node2 ~]$
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。
上一篇: 记录一次nuxt项目的创建
下一篇: iOS项目中混编flutter和打包发布