答案1
假设您正在使用最新版本的 Ubuntu,您可以使用它systemctl
来获取错误消息的概述或列表。例如:
$ systemctl status cron.service
● cron.service - Regular background program processing daemon
Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2019-07-09 07:50:56 CDT; 6 days ago
Docs: man:cron(8)
Main PID: 884 (cron)
Tasks: 1 (limit: 4915)
CGroup: /system.slice/cron.service
└─884 /usr/sbin/cron -f
Jul 15 09:25:01 opal CRON[20797]: pam_unix(cron:session): session opened for user root by (uid=0)
Jul 15 09:25:01 opal CRON[20798]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Jul 15 09:25:01 opal CRON[20797]: pam_unix(cron:session): session closed for user root
Jul 15 09:35:01 opal CRON[21319]: pam_unix(cron:session): session opened for user root by (uid=0)
Jul 15 09:35:01 opal CRON[21319]: pam_unix(cron:session): session closed for user root
Jul 15 09:39:01 opal CRON[21517]: pam_unix(cron:session): session opened for user root by (uid=0)
Jul 15 09:39:01 opal CRON[21518]: (root) CMD ( [ -x /usr/lib/php/sessionclean ] && if [ ! -d /run/systemd/system ]; then /usr/lib/php/sessionclean; fi)
Jul 15 09:39:01 opal CRON[21517]: pam_unix(cron:session): session closed for user root
Jul 15 09:45:01 opal CRON[21982]: pam_unix(cron:session): session opened for user root by (uid=0)
Jul 15 09:45:01 opal CRON[21982]: pam_unix(cron:session): session closed for user root
接下来,您可以strace
在单个进程上运行。例如:
$ pgrep -x cron
884
$ sudo strace -p 884
strace: Process 884 attached
restart_syscall(<... resuming interrupted nanosleep ...>^Cstrace: Process 884 detached
<detached ...>
在这里,我使用 Ctrl-C 在看够之后分离。您也可以将输出通过管道传输到less
。
$ sudo strace -p 884 2>&1 | less -c
在我的例子中,cron 守护进程处于空闲状态,因此它正在运行nanosleep
系统调用。要查看哪些系统调用正在使用 CPU,请使用以下标志-c
:
$ sudo strace -c -p 884
strace: Process 884 attached
^Cstrace: Process 884 detached
% time seconds usecs/call calls errors syscall
------ ----------- ----------- --------- --------- ----------------
61.11 0.000253 253 1 clone
16.18 0.000067 34 2 1 wait4
9.66 0.000040 3 12 stat
6.28 0.000026 26 1 restart_syscall
5.80 0.000024 24 1 1 nanosleep
0.97 0.000004 4 1 1 rt_sigreturn
------ ----------- ----------- --------- --------- ----------------
100.00 0.000414 18 3 total
您还可以使用它gdb
来获取回溯。
$ sudo apt install cron-dbgsym
$ sudo gdb -p 884
(gdb) backtrace
#0 0x00007f84cdf399a4 in __GI___nanosleep (requested_time=0x7ffdcdd18d10, remaining=0x7ffdcdd18d10)
at ../sysdeps/unix/sysv/linux/nanosleep.c:28
#1 0x00007f84cdf398aa in __sleep (seconds=0) at ../sysdeps/posix/sleep.c:55
#2 0x000055af6a6206ab in cron_sleep (target=<optimized out>) at cron.c:415
#3 main (argc=<optimized out>, argv=<optimized out>) at cron.c:173
这再次告诉我它在空闲时处于休眠状态,但您的信息可能更丰富。
我也推荐你
改变cron.service
到限制 CPU 和 I/O 优先级:
$ sudo systemctl edit cron.service
# edits go to /etc/systemd/system/cron.service.d/override.conf
我使用这样的东西:
[Service]
Nice=19
CPUSchedulingPolicy=idle
IOSchedulingClass=idle