Skip to content Skip to main navigation Skip to footer

Ubuntu 16.04:如何将计划任务日志保存到特定的cron.log文件

本文将会讲述如何在Ubuntu 16.04 LTS 系统里查看默认的计划任务日志文件以及如何将默认的计划日志保存到特定的cron.log文件里。

默认的计划任务crontab 日志文件

执行下面的命令来查看默认cron日志:

$ sudo cat /etc/var/log/syslog | grep -i cron 

命令输出如下:

 root@ubuntutest:~# cat /var/log/syslog | grep -i cron
Aug  1 14:49:31 ubuntutest cron[930]: (CRON) INFO (pidfile fd = 3)
Aug  1 14:49:32 ubuntutest cron[930]: (CRON) INFO (Running @reboot jobs)
Aug  1 15:17:01 ubuntutest CRON[1571]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Aug  1 16:17:01 ubuntutest CRON[1770]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Aug  2 10:08:50 ubuntutest cron[926]: (CRON) INFO (pidfile fd = 3)
Aug  2 10:08:50 ubuntutest cron[926]: (CRON) INFO (Running @reboot jobs)
Aug  2 10:17:01 ubuntutest CRON[1604]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Aug  2 10:23:09 ubuntutest cron[933]: (CRON) INFO (pidfile fd = 3)
Aug  2 10:23:09 ubuntutest cron[933]: (CRON) INFO (Running @reboot jobs)
。。。

使用systemctl命令来查看默认crontab 日志文件

我们可以在Ubuntu16.04 LTs 系统上使用systemctl命令来查看最近的计划任务CRON列表,如下:

 $ sudo systemctl status cron

命令输出如下:

 root@ubuntutest:~# systemctl status cron
● cron.service - Regular background program processing daemon
   Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)
   Active: active (running) since 一 2017-08-14 14:41:26 CST; 45min ago
     Docs: man:cron(8)
 Main PID: 916 (cron)
    Tasks: 1
   Memory: 532.0K
      CPU: 49ms
   CGroup: /system.slice/cron.service
           └─916 /usr/sbin/cron -f
8月 14 14:41:26 ubuntutest systemd[1]: Started Regular background program processing daemon.
8月 14 14:41:26 ubuntutest cron[916]: (CRON) INFO (pidfile fd = 3)
8月 14 14:41:26 ubuntutest cron[916]: (CRON) INFO (Running @reboot jobs)
8月 14 15:00:01 ubuntutest CRON[1508]: pam_unix(cron:session): session opened for user root by (uid=0)
8月 14 15:00:01 ubuntutest CRON[1509]: (root) CMD ([ -x /usr/sbin/update-openssh-known-hosts ] && { sleep `od -An -N1 -tu1 /dev/urandom`; /usr/sbin/update-
8月 14 15:02:32 ubuntutest CRON[1508]: pam_unix(cron:session): session closed for user root
8月 14 15:17:01 ubuntutest CRON[1649]: pam_unix(cron:session): session opened for user root by (uid=0)
8月 14 15:17:01 ubuntutest CRON[1650]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
8月 14 15:17:01 ubuntutest CRON[1649]: pam_unix(cron:session): session closed for user root

使用 journalctl命令来查看默认crontab 日志文件

我们可以在Ubuntu16.04 LTs 系统上使用journalctl命令来查看最近的计划任务CRON列表,如下:

 $ sudo journalctl -u cron 

命令输出如下:  

root@ubuntutest:~# journalctl -u cron
-- Logs begin at 一 2017-08-14 14:41:21 CST, end at 一 2017-08-14 15:17:01 CST. --
8月 14 14:41:26 ubuntutest systemd[1]: Started Regular background program processing daemon.
8月 14 14:41:26 ubuntutest cron[916]: (CRON) INFO (pidfile fd = 3)
8月 14 14:41:26 ubuntutest cron[916]: (CRON) INFO (Running @reboot jobs)
8月 14 15:00:01 ubuntutest CRON[1508]: pam_unix(cron:session): session opened for user root by (uid=0)
8月 14 15:00:01 ubuntutest CRON[1509]: (root) CMD ([ -x /usr/sbin/update-openssh-known-hosts ] && { sleep `od -An -N1 -tu1 /dev/urandom`; /usr/sbin/update-
8月 14 15:02:32 ubuntutest CRON[1508]: pam_unix(cron:session): session closed for user root
8月 14 15:17:01 ubuntutest CRON[1649]: pam_unix(cron:session): session opened for user root by (uid=0)
8月 14 15:17:01 ubuntutest CRON[1650]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
8月 14 15:17:01 ubuntutest CRON[1649]: pam_unix(cron:session): session closed for user root

创建单独的CRON 日志

编辑配置文件/etc/rsyslog.d/50-default.conf, 找到“#cron.*”行,将改行前面的注释去掉,保存并退出当前文件。
重启rsyslog服务,执行下面命令:

 $ sudo systemctl restart rsyslog

查看最新的计划任务Cron日志是否保存到cron.log文件里

 $ sudo tail -f /var/log/cron.log

输出如下: 

root@ubuntutest:~# tail -f /var/log/cron.log
Aug 14 15:46:35 ubuntutest cron[1746]: (CRON) DEATH (can't lock /var/run/crond.pid, otherpid may be 916: Resource temporarily unavailable)