Warning: preg_match(): Unknown modifier '-' in /home/akl1986/public_html/support/wp-content/plugins/redux-framework/redux-core/inc/extensions/metaboxes/class-redux-extension-metaboxes.php on line 783
Warning: preg_match(): Unknown modifier '-' in /home/akl1986/public_html/support/wp-content/plugins/redux-framework/redux-core/inc/extensions/metaboxes/class-redux-extension-metaboxes.php on line 783
Warning: preg_match(): Unknown modifier '-' in /home/akl1986/public_html/support/wp-content/plugins/redux-framework/redux-core/inc/extensions/metaboxes/class-redux-extension-metaboxes.php on line 783
Warning: preg_match(): Unknown modifier '-' in /home/akl1986/public_html/support/wp-content/plugins/redux-framework/redux-core/inc/extensions/metaboxes/class-redux-extension-metaboxes.php on line 783
Warning: preg_match(): Unknown modifier '-' in /home/akl1986/public_html/support/wp-content/plugins/redux-framework/redux-core/inc/extensions/metaboxes/class-redux-extension-metaboxes.php on line 783
Warning: preg_match(): Unknown modifier '-' in /home/akl1986/public_html/support/wp-content/plugins/redux-framework/redux-core/inc/extensions/metaboxes/class-redux-extension-metaboxes.php on line 783
Warning: preg_match(): Unknown modifier '-' in /home/akl1986/public_html/support/wp-content/plugins/redux-framework/redux-core/inc/extensions/metaboxes/class-redux-extension-metaboxes.php on line 783
Warning: preg_match(): Unknown modifier '-' in /home/akl1986/public_html/support/wp-content/plugins/redux-framework/redux-core/inc/extensions/metaboxes/class-redux-extension-metaboxes.php on line 783
Warning: Cannot modify header information - headers already sent by (output started at /home/akl1986/public_html/support/wp-content/plugins/redux-framework/redux-core/inc/extensions/metaboxes/class-redux-extension-metaboxes.php:783) in /home/akl1986/public_html/support/wp-includes/rest-api/class-wp-rest-server.php on line 1794
Warning: Cannot modify header information - headers already sent by (output started at /home/akl1986/public_html/support/wp-content/plugins/redux-framework/redux-core/inc/extensions/metaboxes/class-redux-extension-metaboxes.php:783) in /home/akl1986/public_html/support/wp-includes/rest-api/class-wp-rest-server.php on line 1794
Warning: Cannot modify header information - headers already sent by (output started at /home/akl1986/public_html/support/wp-content/plugins/redux-framework/redux-core/inc/extensions/metaboxes/class-redux-extension-metaboxes.php:783) in /home/akl1986/public_html/support/wp-includes/rest-api/class-wp-rest-server.php on line 1794
Warning: Cannot modify header information - headers already sent by (output started at /home/akl1986/public_html/support/wp-content/plugins/redux-framework/redux-core/inc/extensions/metaboxes/class-redux-extension-metaboxes.php:783) in /home/akl1986/public_html/support/wp-includes/rest-api/class-wp-rest-server.php on line 1794
Warning: Cannot modify header information - headers already sent by (output started at /home/akl1986/public_html/support/wp-content/plugins/redux-framework/redux-core/inc/extensions/metaboxes/class-redux-extension-metaboxes.php:783) in /home/akl1986/public_html/support/wp-includes/rest-api/class-wp-rest-server.php on line 1794
Warning: Cannot modify header information - headers already sent by (output started at /home/akl1986/public_html/support/wp-content/plugins/redux-framework/redux-core/inc/extensions/metaboxes/class-redux-extension-metaboxes.php:783) in /home/akl1986/public_html/support/wp-includes/rest-api/class-wp-rest-server.php on line 1794
Warning: Cannot modify header information - headers already sent by (output started at /home/akl1986/public_html/support/wp-content/plugins/redux-framework/redux-core/inc/extensions/metaboxes/class-redux-extension-metaboxes.php:783) in /home/akl1986/public_html/support/wp-includes/rest-api/class-wp-rest-server.php on line 1794
Warning: Cannot modify header information - headers already sent by (output started at /home/akl1986/public_html/support/wp-content/plugins/redux-framework/redux-core/inc/extensions/metaboxes/class-redux-extension-metaboxes.php:783) in /home/akl1986/public_html/support/wp-includes/rest-api/class-wp-rest-server.php on line 1794
{"id":2739,"date":"2019-12-14T18:57:02","date_gmt":"2019-12-14T18:57:02","guid":{"rendered":"https:\/\/support.aklwebhost.com\/?post_type=manual_kb&p=2739"},"modified":"2019-12-14T19:08:52","modified_gmt":"2019-12-14T19:08:52","slug":"using-logrotate-to-manage-log-files","status":"publish","type":"manual_kb","link":"https:\/\/support.aklwebhost.com\/knowledgebase\/using-logrotate-to-manage-log-files\/","title":{"rendered":"Using Logrotate To Manage Log Files"},"content":{"rendered":"Introduction<\/h3>\n
Logrotate is a Linux utility that simplifies the administration of log files. It typically runs once a day via a cron job, and manages logs based on customized rules\/settings from its config file.<\/p>\n
Some of its useful settings include automatic rotation, compression, removal, and mailing of log files.<\/p>\n
Installation<\/h3>\n
Most Linux systems come with Logrotate installed by default. Check if you have it installed on your AKLWEB HOST instance by issuing the\u00a0logrotate<\/code>\u00a0command. You will see an output with the version of Logrotate that your server is running.<\/p>\nIf you don’t have it installed, perform the steps below to proceed with the installation.<\/p>\n
On Debian\/Ubuntu systems:<\/p>\n
sudo apt-get update\r\nsudo apt-get install logrotate\r\n<\/code><\/pre>\nOn Redhat\/CentOS systems:<\/p>\n
sudo yum update\r\nsudo yum install logrotate\r\n<\/code><\/pre>\nUnderstanding Logrotate<\/h3>\n
The moving parts that make logrotate run are:<\/p>\n
\n- The actual tool\u00a0
logrotate<\/code>.<\/li>\n- Logrotate’s configuration file located at\u00a0
\/etc\/logrotate.conf<\/code>. This file holds the configuration for all log files that Logrotate manages.<\/li>\n- A daily cron job\u00a0
\/etc\/cron.daily\/logrotate<\/code>\u00a0that issues the logrotate command to run based on settings in its configuration file. If this cron job does not exist on your system, create it and add code snippet below to it.\n#!\/bin\/sh\r\n\/usr\/sbin\/logrotate \/etc\/logrotate.conf\r\n<\/code><\/pre>\n<\/li>\n<\/ul>\nMore on Configuration<\/h4>\n
If you take a peek inside\u00a0\/etc\/logrotate.conf<\/code>, you will see that it has the line\u00a0include \/etc\/logrotate.d<\/code>\u00a0in it. What this line does is tell Logrotate to look inside the\u00a0\/etc\/logrotate.d<\/code>\u00a0directory and run every configuration file in it. This directory is typically where applications installed on your linux system will add their logrotate configurations. For example, Apache2 will typically create a\u00a0\/etc\/logrotate.d\/apache<\/code>\u00a0configuration file upon installation.<\/p>\nManaging Logs<\/h3>\n
To test Logrotate, we will:<\/p>\n
\n- Create a new test log file with 1MB or random data:\n
sudo base64 \/dev\/urandom | head -c 1000000 > \/tmp\/testfile.log\r\n<\/code><\/pre>\n<\/li>\n- Create a new Logrotate configuration by running\u00a0
sudo nano \/etc\/logrotate.d\/testlog<\/code>. Copy the following snippet into empty file and hit\u00a0Ctrl + X<\/strong>\u00a0to save and exit.\n\/tmp\/testfile.log {\r\n size 1k\r\n copytruncate\r\n missingok\r\n rotate 5\r\n}\r\n<\/code><\/pre>\n<\/li>\n<\/ul>\nThe configuration options in the snippet above instruct Logrotate to:<\/p>\n
\n\nsize 1k<\/code>: Rotate log file if size is greater than or equal to 1k.<\/li>\nmissingok<\/code>: Ignore error messages if\u00a0testfile.log<\/code>\u00a0does not exist.<\/li>\ncopytruncate<\/code>: Create a copy of current log file and then truncate it. This comes in handy when an application cannot close its log file because it continuously appends to it.<\/li>\nrotate 5<\/code>: limit the number of log file rotations to 5. This will delete old versions of log files greater than 5 days.<\/li>\n<\/ul>\n<\/blockquote>\nNOTE:<\/strong>\u00a0You can see all configuration options for logrotate\u00a0here<\/a><\/strong><\/span>.<\/p>\n\n- Run the Logrotate command manually:\n
sudo logrotate \/etc\/logrotate.conf\r\n<\/code><\/pre>\nAfter running above command, list all files in tmp directory\u00a0ls -l \/tmp<\/code>\u00a0to confirm testfile.log was indeed rotated. You should see a listing similar to the following that shows\u00a0testfile.log<\/code>\u00a0has indeed been rotated. This will continue every day and keep the latest 5 copies.<\/p>\n[root@aklwebhost ~]# ls -l \/tmp\r\n-rw-r--r-- 1 root root 0 Nov 14 23:31 testfile.log\r\n-rw-r--r-- 1 root root 1000000 Nov 14 23:30 testfile.log-20141114\r\n-rw-r--r-- 1 root root 634 Nov 10 00:23 aklwebhost_ipv6\r\n-rw-------. 1 root root 0 Oct 15 20:44 yum.log\r\n<\/code><\/pre>\n<\/li>\n<\/ul>\nConclusion<\/h3>\n
Logs can quickly create problems on a server by becoming too large and causing disk space issues. Managing logs is crucial for any system, but even better is automating this management. Logrotate makes rotating, archiving, and deleting logs easy.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","format":"standard","manualknowledgebasecat":[242],"manual_kb_tag":[413],"_links":{"self":[{"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/manual_kb\/2739"}],"collection":[{"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/manual_kb"}],"about":[{"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/types\/manual_kb"}],"author":[{"embeddable":true,"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/comments?post=2739"}],"version-history":[{"count":2,"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/manual_kb\/2739\/revisions"}],"predecessor-version":[{"id":2756,"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/manual_kb\/2739\/revisions\/2756"}],"wp:attachment":[{"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/media?parent=2739"}],"wp:term":[{"taxonomy":"manualknowledgebasecat","embeddable":true,"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/manualknowledgebasecat?post=2739"},{"taxonomy":"manual_kb_tag","embeddable":true,"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/manual_kb_tag?post=2739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}