How to compress , keep and empty original file with gzip
up vote
2
down vote
favorite
For some reason I'm not being able to use logotate to compress files periodically.
So I decided to write my own script, it's not the most hard thing but one small detail is pinning me down.
When using zip to compress the file , logrotate keeps the original files and empty them.
I can use gzip then echo to achieve the same result but assuming my application is writing a lot of data into logs , there will be certainly some lost logs in timestamps after running gzip and before echo
gzip -k file.log
echo "" > file.log
Any idea how can I make it ? Am I missing an option of gzip that empties the original file ?
Thanks :)
Edit
Solved by sending signal to the process (which is a node script) and enforce it to reopen the log file as suggested by AlexP.
command-line scripts gzip logrotate
add a comment |
up vote
2
down vote
favorite
For some reason I'm not being able to use logotate to compress files periodically.
So I decided to write my own script, it's not the most hard thing but one small detail is pinning me down.
When using zip to compress the file , logrotate keeps the original files and empty them.
I can use gzip then echo to achieve the same result but assuming my application is writing a lot of data into logs , there will be certainly some lost logs in timestamps after running gzip and before echo
gzip -k file.log
echo "" > file.log
Any idea how can I make it ? Am I missing an option of gzip that empties the original file ?
Thanks :)
Edit
Solved by sending signal to the process (which is a node script) and enforce it to reopen the log file as suggested by AlexP.
command-line scripts gzip logrotate
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
For some reason I'm not being able to use logotate to compress files periodically.
So I decided to write my own script, it's not the most hard thing but one small detail is pinning me down.
When using zip to compress the file , logrotate keeps the original files and empty them.
I can use gzip then echo to achieve the same result but assuming my application is writing a lot of data into logs , there will be certainly some lost logs in timestamps after running gzip and before echo
gzip -k file.log
echo "" > file.log
Any idea how can I make it ? Am I missing an option of gzip that empties the original file ?
Thanks :)
Edit
Solved by sending signal to the process (which is a node script) and enforce it to reopen the log file as suggested by AlexP.
command-line scripts gzip logrotate
For some reason I'm not being able to use logotate to compress files periodically.
So I decided to write my own script, it's not the most hard thing but one small detail is pinning me down.
When using zip to compress the file , logrotate keeps the original files and empty them.
I can use gzip then echo to achieve the same result but assuming my application is writing a lot of data into logs , there will be certainly some lost logs in timestamps after running gzip and before echo
gzip -k file.log
echo "" > file.log
Any idea how can I make it ? Am I missing an option of gzip that empties the original file ?
Thanks :)
Edit
Solved by sending signal to the process (which is a node script) and enforce it to reopen the log file as suggested by AlexP.
command-line scripts gzip logrotate
command-line scripts gzip logrotate
edited Dec 4 at 14:17
asked Dec 5 '16 at 21:22
storm
3,91032132
3,91032132
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
logrotate
works by (1) renaming the log file as if using mv
, (2) creating a new file with the old name, (3) signalling somehow to the process(es) writing to the log file to close and reopen it (this switches them to the new file), and finally (4) compressing the renamed file.
The tricky step is of course (3), because how to tell a process that it should close and reopen its log(s) depends on the specific process. For example, if you look in /etc/logrotate.d/apache2
, it uses the reload
command to make Apache HTTP Server close and reopen the logs, while /etc/logrotate.d/samba
shows that for Samba, it sends the signal SIGHUP.
Indeed , I need to force the process to write to the new file without restarting the service , actually logrotate is doing it well (without defining any postrotate directive in conf )
– storm
Dec 5 '16 at 21:57
add a comment |
up vote
1
down vote
Here's how you can do it, open a terminal and type :
cat file.log | gzip -9 > tmp_file.gz && echo > file.log
Just tested .. will not work if the original file is sized enough to take time to be archived .. some log will be lost
– storm
Dec 4 at 14:13
thank you for this remark
– adnane hosni
Dec 5 at 15:57
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f857434%2fhow-to-compress-keep-and-empty-original-file-with-gzip%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
logrotate
works by (1) renaming the log file as if using mv
, (2) creating a new file with the old name, (3) signalling somehow to the process(es) writing to the log file to close and reopen it (this switches them to the new file), and finally (4) compressing the renamed file.
The tricky step is of course (3), because how to tell a process that it should close and reopen its log(s) depends on the specific process. For example, if you look in /etc/logrotate.d/apache2
, it uses the reload
command to make Apache HTTP Server close and reopen the logs, while /etc/logrotate.d/samba
shows that for Samba, it sends the signal SIGHUP.
Indeed , I need to force the process to write to the new file without restarting the service , actually logrotate is doing it well (without defining any postrotate directive in conf )
– storm
Dec 5 '16 at 21:57
add a comment |
up vote
1
down vote
accepted
logrotate
works by (1) renaming the log file as if using mv
, (2) creating a new file with the old name, (3) signalling somehow to the process(es) writing to the log file to close and reopen it (this switches them to the new file), and finally (4) compressing the renamed file.
The tricky step is of course (3), because how to tell a process that it should close and reopen its log(s) depends on the specific process. For example, if you look in /etc/logrotate.d/apache2
, it uses the reload
command to make Apache HTTP Server close and reopen the logs, while /etc/logrotate.d/samba
shows that for Samba, it sends the signal SIGHUP.
Indeed , I need to force the process to write to the new file without restarting the service , actually logrotate is doing it well (without defining any postrotate directive in conf )
– storm
Dec 5 '16 at 21:57
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
logrotate
works by (1) renaming the log file as if using mv
, (2) creating a new file with the old name, (3) signalling somehow to the process(es) writing to the log file to close and reopen it (this switches them to the new file), and finally (4) compressing the renamed file.
The tricky step is of course (3), because how to tell a process that it should close and reopen its log(s) depends on the specific process. For example, if you look in /etc/logrotate.d/apache2
, it uses the reload
command to make Apache HTTP Server close and reopen the logs, while /etc/logrotate.d/samba
shows that for Samba, it sends the signal SIGHUP.
logrotate
works by (1) renaming the log file as if using mv
, (2) creating a new file with the old name, (3) signalling somehow to the process(es) writing to the log file to close and reopen it (this switches them to the new file), and finally (4) compressing the renamed file.
The tricky step is of course (3), because how to tell a process that it should close and reopen its log(s) depends on the specific process. For example, if you look in /etc/logrotate.d/apache2
, it uses the reload
command to make Apache HTTP Server close and reopen the logs, while /etc/logrotate.d/samba
shows that for Samba, it sends the signal SIGHUP.
answered Dec 5 '16 at 21:38
AlexP
7,36711228
7,36711228
Indeed , I need to force the process to write to the new file without restarting the service , actually logrotate is doing it well (without defining any postrotate directive in conf )
– storm
Dec 5 '16 at 21:57
add a comment |
Indeed , I need to force the process to write to the new file without restarting the service , actually logrotate is doing it well (without defining any postrotate directive in conf )
– storm
Dec 5 '16 at 21:57
Indeed , I need to force the process to write to the new file without restarting the service , actually logrotate is doing it well (without defining any postrotate directive in conf )
– storm
Dec 5 '16 at 21:57
Indeed , I need to force the process to write to the new file without restarting the service , actually logrotate is doing it well (without defining any postrotate directive in conf )
– storm
Dec 5 '16 at 21:57
add a comment |
up vote
1
down vote
Here's how you can do it, open a terminal and type :
cat file.log | gzip -9 > tmp_file.gz && echo > file.log
Just tested .. will not work if the original file is sized enough to take time to be archived .. some log will be lost
– storm
Dec 4 at 14:13
thank you for this remark
– adnane hosni
Dec 5 at 15:57
add a comment |
up vote
1
down vote
Here's how you can do it, open a terminal and type :
cat file.log | gzip -9 > tmp_file.gz && echo > file.log
Just tested .. will not work if the original file is sized enough to take time to be archived .. some log will be lost
– storm
Dec 4 at 14:13
thank you for this remark
– adnane hosni
Dec 5 at 15:57
add a comment |
up vote
1
down vote
up vote
1
down vote
Here's how you can do it, open a terminal and type :
cat file.log | gzip -9 > tmp_file.gz && echo > file.log
Here's how you can do it, open a terminal and type :
cat file.log | gzip -9 > tmp_file.gz && echo > file.log
edited Dec 3 at 22:49
efthialex
2,4761730
2,4761730
answered Dec 3 at 20:23
adnane hosni
111
111
Just tested .. will not work if the original file is sized enough to take time to be archived .. some log will be lost
– storm
Dec 4 at 14:13
thank you for this remark
– adnane hosni
Dec 5 at 15:57
add a comment |
Just tested .. will not work if the original file is sized enough to take time to be archived .. some log will be lost
– storm
Dec 4 at 14:13
thank you for this remark
– adnane hosni
Dec 5 at 15:57
Just tested .. will not work if the original file is sized enough to take time to be archived .. some log will be lost
– storm
Dec 4 at 14:13
Just tested .. will not work if the original file is sized enough to take time to be archived .. some log will be lost
– storm
Dec 4 at 14:13
thank you for this remark
– adnane hosni
Dec 5 at 15:57
thank you for this remark
– adnane hosni
Dec 5 at 15:57
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f857434%2fhow-to-compress-keep-and-empty-original-file-with-gzip%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown