atop log does not rotate in debian stretch

atop is a sar-like tool which saves system diagnostic data and allows to view it if a fancy readable way. By default it’s configured to rotate logs every midnignt, but recently I found out that they aren’t rotating for a month, which made impossible finding any useful info:

# ls -ltrh /var/log/atop/
total 7.8G
-rw-r--r-- 1 root root 3.4M Jul 6 00:15 atop_20160705
-rw-r--r-- 1 root root 290M Jul 21 20:20 atop_20160706
-rw-r--r-- 1 root root 864M Aug 22 17:53 atop_20160721
-rw-r--r-- 1 root root 669M Aug 30 20:26 atop_20160822
-rw-r--r-- 1 root root 87M Aug 31 16:27 atop_20160830
-rw-r--r-- 1 root root 2.7G Sep 30 01:30 atop_20160831
-rw-r--r-- 1 root root 0 Sep 30 01:32 daily.log
-rw-r--r-- 1 root root 3.3G Oct 31 16:56 atop_20160930

atop doesn’t use logrotate to rotate it’s files, there is a file in /etc/cron.d, which invokes init script with _cron key every day at 00:00:

# cat /etc/cron.d/atop
PATH=/sbin:/usr/sbin:/bin:/usr/bin

# start atop daily at midnight
0 0 * * * root invoke-rc.d atop _cron

I tried to run it manually, but result was the same, no logs were rotated. Logic suggests that if it keeps writing the same file, it may be not successfully restarted. atop’s init script contains the _cron case, which uses the do_stop function:

restart|force-reload|_cron)
      [ "$1" = "_cron" ] && VERBOSE="no"
      [ "$VERBOSE" != no ] && log_daemon_msg "Restarting $DESC" "$NAME"
     do_stop

Let’s check with strace (I won’t post it’s whole output):

# strace invoke-rc.d atop stop
execve("/usr/sbin/invoke-rc.d", ["invoke-rc.d", "atop", "stop"], [/* 20 vars */]) = 0
<...>
<...>
+++ exited with 0 +++
# /etc/init.d/atop status
● atop.service - LSB: Monitor for system resources and process activity
Loaded: loaded (/etc/init.d/atop)
Active: active (running) since Thu 2016-11-10 13:33:39 MSK; 1 day 5h ago
CGroup: /system.slice/atop.service
└─24693 /usr/bin/atop -a -w /var/log/atop/atop_20161110 20

So, although the return code is 0, process is still running and writing the old log file. invoke-rc.d has the –force key, let’s try it:

# invoke-rc.d --force atop stop
# systemctl status atop
● atop.service - LSB: Monitor for system resources and process activity
   Loaded: loaded (/etc/init.d/atop)
   Active: inactive (dead) since Fri 2016-11-11 19:38:57 MSK; 1s ago
  Process: 24994 ExecStop=/etc/init.d/atop stop (code=exited, status=0/SUCCESS)
  Process: 23446 ExecStart=/etc/init.d/atop start (code=exited, status=0/SUCCESS)

Excellent, it works. Now we need to update the /etc/cron.d/atop file to add this key to command running every midnight. File must look like this:

# cat /etc/cron.d/atop
PATH=/sbin:/usr/sbin:/bin:/usr/bin

# start atop daily at midnight
0 0 * * * root invoke-rc.d --force atop _cron

Now logs will rotate as usual.

Leave a Reply

Your email address will not be published. Required fields are marked *