Shell script to emulate Logrotate
Clash Royale CLAN TAG#URR8PPP
I've a system that writes files to /var/tmp/log/my.log
, I want to write a shell script to rotate the log files when it reaches 1KB limit, so my my.log becomes my.log.1, my.log.1 becomes my.log.2 and so on until my.log.10 and then the other files are deleted.
I got some part of the script but don't know how to change the file names.
#!/bin/bash
file_size=`du -b /var/tmp/log/my.log`
if($file_size -gt 1024)
do
mv my.log my.log.1
done
fi
I need to move my.log to my.log.1 when my.log reaches 1KB limit and then move my.log.1 to my.log.2 so that the my.log can move to my.log.1 after it reaches 1KB second time and so on. Is there any way to do this without using logrotate ?
linux logs logrotate
add a comment |
I've a system that writes files to /var/tmp/log/my.log
, I want to write a shell script to rotate the log files when it reaches 1KB limit, so my my.log becomes my.log.1, my.log.1 becomes my.log.2 and so on until my.log.10 and then the other files are deleted.
I got some part of the script but don't know how to change the file names.
#!/bin/bash
file_size=`du -b /var/tmp/log/my.log`
if($file_size -gt 1024)
do
mv my.log my.log.1
done
fi
I need to move my.log to my.log.1 when my.log reaches 1KB limit and then move my.log.1 to my.log.2 so that the my.log can move to my.log.1 after it reaches 1KB second time and so on. Is there any way to do this without using logrotate ?
linux logs logrotate
What is the language you write your script? IMO it's notbash
– Romeo Ninov
Dec 23 '18 at 8:53
I want to write a shell script, it doesn't matter which shell, but I prefer bash
– shanu
Dec 23 '18 at 8:55
So write it inbash
And define how many old logs you want to keep
– Romeo Ninov
Dec 23 '18 at 8:57
add a comment |
I've a system that writes files to /var/tmp/log/my.log
, I want to write a shell script to rotate the log files when it reaches 1KB limit, so my my.log becomes my.log.1, my.log.1 becomes my.log.2 and so on until my.log.10 and then the other files are deleted.
I got some part of the script but don't know how to change the file names.
#!/bin/bash
file_size=`du -b /var/tmp/log/my.log`
if($file_size -gt 1024)
do
mv my.log my.log.1
done
fi
I need to move my.log to my.log.1 when my.log reaches 1KB limit and then move my.log.1 to my.log.2 so that the my.log can move to my.log.1 after it reaches 1KB second time and so on. Is there any way to do this without using logrotate ?
linux logs logrotate
I've a system that writes files to /var/tmp/log/my.log
, I want to write a shell script to rotate the log files when it reaches 1KB limit, so my my.log becomes my.log.1, my.log.1 becomes my.log.2 and so on until my.log.10 and then the other files are deleted.
I got some part of the script but don't know how to change the file names.
#!/bin/bash
file_size=`du -b /var/tmp/log/my.log`
if($file_size -gt 1024)
do
mv my.log my.log.1
done
fi
I need to move my.log to my.log.1 when my.log reaches 1KB limit and then move my.log.1 to my.log.2 so that the my.log can move to my.log.1 after it reaches 1KB second time and so on. Is there any way to do this without using logrotate ?
linux logs logrotate
linux logs logrotate
edited Dec 23 '18 at 12:44
Jeff Schaller
39k1053125
39k1053125
asked Dec 23 '18 at 8:51
shanu
12
12
What is the language you write your script? IMO it's notbash
– Romeo Ninov
Dec 23 '18 at 8:53
I want to write a shell script, it doesn't matter which shell, but I prefer bash
– shanu
Dec 23 '18 at 8:55
So write it inbash
And define how many old logs you want to keep
– Romeo Ninov
Dec 23 '18 at 8:57
add a comment |
What is the language you write your script? IMO it's notbash
– Romeo Ninov
Dec 23 '18 at 8:53
I want to write a shell script, it doesn't matter which shell, but I prefer bash
– shanu
Dec 23 '18 at 8:55
So write it inbash
And define how many old logs you want to keep
– Romeo Ninov
Dec 23 '18 at 8:57
What is the language you write your script? IMO it's not
bash
– Romeo Ninov
Dec 23 '18 at 8:53
What is the language you write your script? IMO it's not
bash
– Romeo Ninov
Dec 23 '18 at 8:53
I want to write a shell script, it doesn't matter which shell, but I prefer bash
– shanu
Dec 23 '18 at 8:55
I want to write a shell script, it doesn't matter which shell, but I prefer bash
– shanu
Dec 23 '18 at 8:55
So write it in
bash
And define how many old logs you want to keep– Romeo Ninov
Dec 23 '18 at 8:57
So write it in
bash
And define how many old logs you want to keep– Romeo Ninov
Dec 23 '18 at 8:57
add a comment |
1 Answer
1
active
oldest
votes
You can make rotation with something like this:
for i in 9..1; do
if [[ -f my.log.$i ]]; then
mv -f my.log.$i my.log.$((i+1))
fi
done
mv -f my.log my.log.1
touch my.log
this will keep 10 rotated logs, up to my.log.10, which will be overwritten with newer logs on every rotation once script does more then 10 rotations.
Depending on how many logs you want to keep you need to lower or increase the range in for loop.
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2funix.stackexchange.com%2fquestions%2f490567%2fshell-script-to-emulate-logrotate%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can make rotation with something like this:
for i in 9..1; do
if [[ -f my.log.$i ]]; then
mv -f my.log.$i my.log.$((i+1))
fi
done
mv -f my.log my.log.1
touch my.log
this will keep 10 rotated logs, up to my.log.10, which will be overwritten with newer logs on every rotation once script does more then 10 rotations.
Depending on how many logs you want to keep you need to lower or increase the range in for loop.
add a comment |
You can make rotation with something like this:
for i in 9..1; do
if [[ -f my.log.$i ]]; then
mv -f my.log.$i my.log.$((i+1))
fi
done
mv -f my.log my.log.1
touch my.log
this will keep 10 rotated logs, up to my.log.10, which will be overwritten with newer logs on every rotation once script does more then 10 rotations.
Depending on how many logs you want to keep you need to lower or increase the range in for loop.
add a comment |
You can make rotation with something like this:
for i in 9..1; do
if [[ -f my.log.$i ]]; then
mv -f my.log.$i my.log.$((i+1))
fi
done
mv -f my.log my.log.1
touch my.log
this will keep 10 rotated logs, up to my.log.10, which will be overwritten with newer logs on every rotation once script does more then 10 rotations.
Depending on how many logs you want to keep you need to lower or increase the range in for loop.
You can make rotation with something like this:
for i in 9..1; do
if [[ -f my.log.$i ]]; then
mv -f my.log.$i my.log.$((i+1))
fi
done
mv -f my.log my.log.1
touch my.log
this will keep 10 rotated logs, up to my.log.10, which will be overwritten with newer logs on every rotation once script does more then 10 rotations.
Depending on how many logs you want to keep you need to lower or increase the range in for loop.
edited Dec 23 '18 at 9:51
answered Dec 23 '18 at 9:42
rAlen
845510
845510
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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%2funix.stackexchange.com%2fquestions%2f490567%2fshell-script-to-emulate-logrotate%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
What is the language you write your script? IMO it's not
bash
– Romeo Ninov
Dec 23 '18 at 8:53
I want to write a shell script, it doesn't matter which shell, but I prefer bash
– shanu
Dec 23 '18 at 8:55
So write it in
bash
And define how many old logs you want to keep– Romeo Ninov
Dec 23 '18 at 8:57