-
Notifications
You must be signed in to change notification settings - Fork 4
/
logrotate_ros
121 lines (91 loc) · 3.57 KB
/
logrotate_ros
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# Based on:
# https://github.com/mikepurvis/ros-system-daemon/blob/master/debian/ros-system-daemon.ros.logrotate
# logrotate cannot read environment variables, so we need to hardcode paths. Please substitute your own paths.
# unless you want to do something like this: https://stackoverflow.com/a/4417165/3363527
/var/log/ros/latest/*.log {
# Run scripts once per set of logs
sharedscripts
# runs logrotate as root. May be necessary to you.
# su root root
# runs before anything, but after checking which files need to be rotated
firstaction
# sources ROS so we have rosparam on this terminal ROS VERSION ALSO HARDCODED HERE
. /opt/ros/indigo/setup.sh
# get run_id of ROS if it is running, returns empty if not running
RUN_ID=`rosparam get /run_id 2>/dev/null`
# Create a tarball if ROS is no longer using a given log subdirectory
cd /var/log/ros
for i in $( ls /var/log/ros | grep -v \.gz$ ); do
# if ROS is not running, will compress all files
if [ $i != "$RUN_ID" ] || [ ! "$RUN_ID" ] ; then
if [ -d $i ] && [ "$i" != "latest" ] ; then # keeps the latest folder
# will compress old folders
tar -zpcf $i.tar.gz $i
fi
fi
done
endscript
# runs after everything
lastaction
. /opt/ros/indigo/setup.sh
RUN_ID=`rosparam get /run_id 2>/dev/null`
# Remove log subdirectory after tarball has been created
for i in $( ls /var/log/ros | grep -v \.gz$ ); do
if [ $i != "$RUN_ID" ] && [ -e /var/log/ros/$i.tar.gz ]; then
# removes old directories
rm -r /var/log/ros/$i
fi
done
# Delete tarballs after a week
for i in $( find /var/log/ros -mtime 7 | grep \.gz$ ); do
rm /var/log/ros/$i
done
endscript
# Do not report errors if ROS has not created logs
missingok
# Rotate logs daily
daily
# Do not rotate the file if empty
notifempty
# Keep 1 week worth of logs for active processes
rotate 7
# Won't close current file, will create a new file for rotation instead
copytruncate
# will compress .log files
compress
}
# will rotate rostopic log files on ROS_LOG_DIR folder
# when you call some ROS tools using command line (like rostopic pub or rosservice call), ROS generates a log file
# on ROS_LOG_DIR. These files never get deleted by ROS and they have an unique identifier, so they might never be
# used again if you run a command twice. Thus, this command rotates log files that are in use and will delete
# files that have not been modified on last 7 days
/var/log/ros/*.log {
# Run scripts once per set of logs
sharedscripts
# runs logrotate as root
su root root
# runs after everything
lastaction
cd /var/log/ros
# Delete files that have not been modified in a week
for i in $( find /var/log/ros -ctime 7 | grep \.log$ ); do
rm /var/log/ros/$i
done
# Delete files older than a week
for i in $( find /var/log/ros -mtime 7 | grep \.gz$ ); do
rm /var/log/ros/$i
done
endscript
# Do not report errors if ROS has not created logs
missingok
# Do not rotate the file if empty
notifempty
# Rotate logs daily
daily
# Keep 1 week worth of logs for active processes
rotate 7
# Won't close current file, will create a new file for rotation instead
copytruncate
# will compress .log files
compress
}