shell script to exit out of the script if diskspace is more than 75
I want a script to exit out of the script if disk space is beyond threshold(ex:75%). Trying below things, But no luck.
df -kh | awk '{if( 0+$5 >= 75 ) exit;}'
Trying above command, its not working. Can anyone help me on this.
awk
add a comment |
I want a script to exit out of the script if disk space is beyond threshold(ex:75%). Trying below things, But no luck.
df -kh | awk '{if( 0+$5 >= 75 ) exit;}'
Trying above command, its not working. Can anyone help me on this.
awk
add a comment |
I want a script to exit out of the script if disk space is beyond threshold(ex:75%). Trying below things, But no luck.
df -kh | awk '{if( 0+$5 >= 75 ) exit;}'
Trying above command, its not working. Can anyone help me on this.
awk
I want a script to exit out of the script if disk space is beyond threshold(ex:75%). Trying below things, But no luck.
df -kh | awk '{if( 0+$5 >= 75 ) exit;}'
Trying above command, its not working. Can anyone help me on this.
awk
awk
edited Nov 13 '18 at 3:33
Inian
38.8k63770
38.8k63770
asked Nov 13 '18 at 3:15
Himavanth
103
103
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
This is because your df output is NOT coming in a single line or so, to make this you need to add -P option with it try following once.
df -hP | awk '{if( 0+$5 >= 75 ){print "exiting now..";exit 1}}'
OR
df -hP | awk '$5+0>=75{print "exiting now..";exit 1}'
OR with mount name who is the culprit for breaching threshold.
df -hP | awk '$5+0>=75{print "Mount " $1 " has crossed threshold so exiting now..";exit 1}'
In case you don't have -P option in your box then try following.
df -k | awk '/^ +/ && $4+0>=75{print "Mount " prev" has crossed threshold so exiting now..";exit 1} !/^ +/{prev=$0}'
I am using print statement to make sure exit is working. also -P option was tested on BASH systems.
Since OP told he needs to exit from complete script itself so requesting OP to add following code outside of for loop of his code.(I haven't tested it though but this should work)
if [[ $? -eq 1 ]]
then
echo "Exiting the complete script now..."
exit
else
echo "Looks good so going further in script now.."
fi
Thanks for the info, I am not able to exit out of the script, its just exiting from that particular loop(for loop I am using). My concern is I have to exit out of the script without continuing with the further loops.
– Himavanth
Nov 13 '18 at 3:36
@Himavanth, useexit 1in my above code'sawkcommands. Then outside of yourforloop from which it is coming out put a condition likeif [[ $? -eq 1 ]]; then echo "Exiting the complete script now..."; exit; else echo "Looks good so going further in script now.."; fiI will post this to my code now too, let me know how it goes then?
– RavinderSingh13
Nov 13 '18 at 3:39
add a comment |
If you are using this in a script to exit the script (as opposed to exiting a long awk script) then you need to call exit from the outer script:
if df -kh | awk '{if ($5+0 > 75) exit 1 }'; then echo OK; else echo NOT; fi
Don't forget that df returns one line per mount point, you can do:
if dk -kh /home ....
to check for a particular mount point.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: 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%2fstackoverflow.com%2fquestions%2f53273270%2fshell-script-to-exit-out-of-the-script-if-diskspace-is-more-than-75%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
This is because your df output is NOT coming in a single line or so, to make this you need to add -P option with it try following once.
df -hP | awk '{if( 0+$5 >= 75 ){print "exiting now..";exit 1}}'
OR
df -hP | awk '$5+0>=75{print "exiting now..";exit 1}'
OR with mount name who is the culprit for breaching threshold.
df -hP | awk '$5+0>=75{print "Mount " $1 " has crossed threshold so exiting now..";exit 1}'
In case you don't have -P option in your box then try following.
df -k | awk '/^ +/ && $4+0>=75{print "Mount " prev" has crossed threshold so exiting now..";exit 1} !/^ +/{prev=$0}'
I am using print statement to make sure exit is working. also -P option was tested on BASH systems.
Since OP told he needs to exit from complete script itself so requesting OP to add following code outside of for loop of his code.(I haven't tested it though but this should work)
if [[ $? -eq 1 ]]
then
echo "Exiting the complete script now..."
exit
else
echo "Looks good so going further in script now.."
fi
Thanks for the info, I am not able to exit out of the script, its just exiting from that particular loop(for loop I am using). My concern is I have to exit out of the script without continuing with the further loops.
– Himavanth
Nov 13 '18 at 3:36
@Himavanth, useexit 1in my above code'sawkcommands. Then outside of yourforloop from which it is coming out put a condition likeif [[ $? -eq 1 ]]; then echo "Exiting the complete script now..."; exit; else echo "Looks good so going further in script now.."; fiI will post this to my code now too, let me know how it goes then?
– RavinderSingh13
Nov 13 '18 at 3:39
add a comment |
This is because your df output is NOT coming in a single line or so, to make this you need to add -P option with it try following once.
df -hP | awk '{if( 0+$5 >= 75 ){print "exiting now..";exit 1}}'
OR
df -hP | awk '$5+0>=75{print "exiting now..";exit 1}'
OR with mount name who is the culprit for breaching threshold.
df -hP | awk '$5+0>=75{print "Mount " $1 " has crossed threshold so exiting now..";exit 1}'
In case you don't have -P option in your box then try following.
df -k | awk '/^ +/ && $4+0>=75{print "Mount " prev" has crossed threshold so exiting now..";exit 1} !/^ +/{prev=$0}'
I am using print statement to make sure exit is working. also -P option was tested on BASH systems.
Since OP told he needs to exit from complete script itself so requesting OP to add following code outside of for loop of his code.(I haven't tested it though but this should work)
if [[ $? -eq 1 ]]
then
echo "Exiting the complete script now..."
exit
else
echo "Looks good so going further in script now.."
fi
Thanks for the info, I am not able to exit out of the script, its just exiting from that particular loop(for loop I am using). My concern is I have to exit out of the script without continuing with the further loops.
– Himavanth
Nov 13 '18 at 3:36
@Himavanth, useexit 1in my above code'sawkcommands. Then outside of yourforloop from which it is coming out put a condition likeif [[ $? -eq 1 ]]; then echo "Exiting the complete script now..."; exit; else echo "Looks good so going further in script now.."; fiI will post this to my code now too, let me know how it goes then?
– RavinderSingh13
Nov 13 '18 at 3:39
add a comment |
This is because your df output is NOT coming in a single line or so, to make this you need to add -P option with it try following once.
df -hP | awk '{if( 0+$5 >= 75 ){print "exiting now..";exit 1}}'
OR
df -hP | awk '$5+0>=75{print "exiting now..";exit 1}'
OR with mount name who is the culprit for breaching threshold.
df -hP | awk '$5+0>=75{print "Mount " $1 " has crossed threshold so exiting now..";exit 1}'
In case you don't have -P option in your box then try following.
df -k | awk '/^ +/ && $4+0>=75{print "Mount " prev" has crossed threshold so exiting now..";exit 1} !/^ +/{prev=$0}'
I am using print statement to make sure exit is working. also -P option was tested on BASH systems.
Since OP told he needs to exit from complete script itself so requesting OP to add following code outside of for loop of his code.(I haven't tested it though but this should work)
if [[ $? -eq 1 ]]
then
echo "Exiting the complete script now..."
exit
else
echo "Looks good so going further in script now.."
fi
This is because your df output is NOT coming in a single line or so, to make this you need to add -P option with it try following once.
df -hP | awk '{if( 0+$5 >= 75 ){print "exiting now..";exit 1}}'
OR
df -hP | awk '$5+0>=75{print "exiting now..";exit 1}'
OR with mount name who is the culprit for breaching threshold.
df -hP | awk '$5+0>=75{print "Mount " $1 " has crossed threshold so exiting now..";exit 1}'
In case you don't have -P option in your box then try following.
df -k | awk '/^ +/ && $4+0>=75{print "Mount " prev" has crossed threshold so exiting now..";exit 1} !/^ +/{prev=$0}'
I am using print statement to make sure exit is working. also -P option was tested on BASH systems.
Since OP told he needs to exit from complete script itself so requesting OP to add following code outside of for loop of his code.(I haven't tested it though but this should work)
if [[ $? -eq 1 ]]
then
echo "Exiting the complete script now..."
exit
else
echo "Looks good so going further in script now.."
fi
edited Nov 13 '18 at 3:40
answered Nov 13 '18 at 3:23
RavinderSingh13
25.7k41438
25.7k41438
Thanks for the info, I am not able to exit out of the script, its just exiting from that particular loop(for loop I am using). My concern is I have to exit out of the script without continuing with the further loops.
– Himavanth
Nov 13 '18 at 3:36
@Himavanth, useexit 1in my above code'sawkcommands. Then outside of yourforloop from which it is coming out put a condition likeif [[ $? -eq 1 ]]; then echo "Exiting the complete script now..."; exit; else echo "Looks good so going further in script now.."; fiI will post this to my code now too, let me know how it goes then?
– RavinderSingh13
Nov 13 '18 at 3:39
add a comment |
Thanks for the info, I am not able to exit out of the script, its just exiting from that particular loop(for loop I am using). My concern is I have to exit out of the script without continuing with the further loops.
– Himavanth
Nov 13 '18 at 3:36
@Himavanth, useexit 1in my above code'sawkcommands. Then outside of yourforloop from which it is coming out put a condition likeif [[ $? -eq 1 ]]; then echo "Exiting the complete script now..."; exit; else echo "Looks good so going further in script now.."; fiI will post this to my code now too, let me know how it goes then?
– RavinderSingh13
Nov 13 '18 at 3:39
Thanks for the info, I am not able to exit out of the script, its just exiting from that particular loop(for loop I am using). My concern is I have to exit out of the script without continuing with the further loops.
– Himavanth
Nov 13 '18 at 3:36
Thanks for the info, I am not able to exit out of the script, its just exiting from that particular loop(for loop I am using). My concern is I have to exit out of the script without continuing with the further loops.
– Himavanth
Nov 13 '18 at 3:36
@Himavanth, use
exit 1 in my above code's awk commands. Then outside of your for loop from which it is coming out put a condition like if [[ $? -eq 1 ]]; then echo "Exiting the complete script now..."; exit; else echo "Looks good so going further in script now.."; fi I will post this to my code now too, let me know how it goes then?– RavinderSingh13
Nov 13 '18 at 3:39
@Himavanth, use
exit 1 in my above code's awk commands. Then outside of your for loop from which it is coming out put a condition like if [[ $? -eq 1 ]]; then echo "Exiting the complete script now..."; exit; else echo "Looks good so going further in script now.."; fi I will post this to my code now too, let me know how it goes then?– RavinderSingh13
Nov 13 '18 at 3:39
add a comment |
If you are using this in a script to exit the script (as opposed to exiting a long awk script) then you need to call exit from the outer script:
if df -kh | awk '{if ($5+0 > 75) exit 1 }'; then echo OK; else echo NOT; fi
Don't forget that df returns one line per mount point, you can do:
if dk -kh /home ....
to check for a particular mount point.
add a comment |
If you are using this in a script to exit the script (as opposed to exiting a long awk script) then you need to call exit from the outer script:
if df -kh | awk '{if ($5+0 > 75) exit 1 }'; then echo OK; else echo NOT; fi
Don't forget that df returns one line per mount point, you can do:
if dk -kh /home ....
to check for a particular mount point.
add a comment |
If you are using this in a script to exit the script (as opposed to exiting a long awk script) then you need to call exit from the outer script:
if df -kh | awk '{if ($5+0 > 75) exit 1 }'; then echo OK; else echo NOT; fi
Don't forget that df returns one line per mount point, you can do:
if dk -kh /home ....
to check for a particular mount point.
If you are using this in a script to exit the script (as opposed to exiting a long awk script) then you need to call exit from the outer script:
if df -kh | awk '{if ($5+0 > 75) exit 1 }'; then echo OK; else echo NOT; fi
Don't forget that df returns one line per mount point, you can do:
if dk -kh /home ....
to check for a particular mount point.
answered Nov 13 '18 at 3:32
perreal
71.8k9110138
71.8k9110138
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53273270%2fshell-script-to-exit-out-of-the-script-if-diskspace-is-more-than-75%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