Conditional crontab command in bash script
How do I invoke a crontab command so that I can schedule to run a script with a 20 minute delay based on some condition?
Edit: What if I wanted a command that schedules a script to be executed only as many times as the condition evaluated to true in the system? What are my options?
bash cron scheduling
add a comment |
How do I invoke a crontab command so that I can schedule to run a script with a 20 minute delay based on some condition?
Edit: What if I wanted a command that schedules a script to be executed only as many times as the condition evaluated to true in the system? What are my options?
bash cron scheduling
add a comment |
How do I invoke a crontab command so that I can schedule to run a script with a 20 minute delay based on some condition?
Edit: What if I wanted a command that schedules a script to be executed only as many times as the condition evaluated to true in the system? What are my options?
bash cron scheduling
How do I invoke a crontab command so that I can schedule to run a script with a 20 minute delay based on some condition?
Edit: What if I wanted a command that schedules a script to be executed only as many times as the condition evaluated to true in the system? What are my options?
bash cron scheduling
bash cron scheduling
edited Oct 4 '12 at 11:35
asked Oct 4 '12 at 11:11
Arpith
4892921
4892921
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Put the logic code for testing your condition in the script itself, don't try and put it in (or associate it with) cron - complex logic is not what cron was designed for.
So, in your script you test the condition and, if it evaluates to true, your processing code runs. If it evaluates to false, exit the script cleanly.
Assuming that your 'conditions' change as a result of processing the script (e.g. watching a folder for incoming files that need processing and processing one file every 20m), then eventually your condition will evaluate to false all the time because all the work has been done.
From your comments it looks like you are monitoring the availability of some server.
I don't do a heck of a lot with bash but how about this:
#!/bin/bash
if [ `ps ax | grep $0 | wc -l` -le 3 ]; then #1
if [ `arping ...` -ne 1 ]; then #2
sleep 1200
if [ `arping ...` -eq 1 ]; then #3
# do your processing here
fi
fi
fi
The first if statement (#1) makes sure that this is the only instance of this particular script that is running. If another script is (still) running we exit and don't do anything.
The second (#2) is your initial 'is host pingable' test. If it is not, then the script waits 1200s (20min) before testing again (#3).
So, if two pings -- 20 minutes apart -- show that your host has become reachable then your processing code will run.
If you want to simplify things a little, try this:
#!/bin/bash
if [ `arping -w 59 ...` -ne 1 ]; then
sleep 1079
if [ `arping -w 59 ...` -eq 1 ]; then
# do your processing here
fi
fi
If you impose an arping deadline of a little under 1 minute (-w 59) for your checks, and tweak the sleep amount, then you can pretty much guarantee that the two tests and the sleep in between will be completed within your 20 minute period, so there should be no overlap with adjacent periods and no need to check to see if another script is still running.
Either of the above scripts would, of course, be invoked via a static cron entry which runs every 20 minutes:
*/20 * * * * /path/to/script.sh
1
The condition is to check if a particular hostname/IP address is ping-able. I am using arping in this context. If it returns 1, then the address is ping-able, if not I want the code to go and wait as a scheduled cron job. After 20 minutes I again want to ping the same address and only if the address is up, I want to run the scheduled cron job. Otherwise I want to schedule it again after a delay of 20 minutes. Is this do-able using cron and a bash script?
– Arpith
Oct 5 '12 at 2:16
I might be totally wrong but I believe the second condition should be different from the first condition. Right? The second arping condition in the first script should have-ne
rather than-eq
– Arpith
Oct 7 '12 at 15:17
Apologies the confusion. Great answer. Thank you for your time.
– Arpith
Oct 8 '12 at 17:19
@Arpith Yes, you are right. Fixed.
– Tim
Oct 8 '12 at 17:28
Also, if the script is called every 20 minutes it may start overlapping with the sleep period, probably want to cron run every 30 minutes.
– Efren
Aug 3 at 8:08
add a comment |
You would have to build a bash script that checked for the conditional and assigned a sleep to it. Then schedule that bash script to run at a specific time using cron. Cron itself doesn't not have support for conditionals or 'run after a delay'.
add a comment |
Okay you updated the question to minutes now.
So you would run every 20 minutes = run at 0 / 20 / 40 / 60 (=0) minute of all hours.
The entry would be:
0,20,40 * * * * /path/to/some_command
And if you want to execute it for limited times, use a state file to store how many times you've launched that script.
Sorry. You're right. Made the appropriate changes in the question.
– Arpith
Oct 4 '12 at 11:35
You have not said anything about the execution of the cron job based on the evaluation of a conditional statement.
– Arpith
Oct 4 '12 at 13:02
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%2f49868%2fconditional-crontab-command-in-bash-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Put the logic code for testing your condition in the script itself, don't try and put it in (or associate it with) cron - complex logic is not what cron was designed for.
So, in your script you test the condition and, if it evaluates to true, your processing code runs. If it evaluates to false, exit the script cleanly.
Assuming that your 'conditions' change as a result of processing the script (e.g. watching a folder for incoming files that need processing and processing one file every 20m), then eventually your condition will evaluate to false all the time because all the work has been done.
From your comments it looks like you are monitoring the availability of some server.
I don't do a heck of a lot with bash but how about this:
#!/bin/bash
if [ `ps ax | grep $0 | wc -l` -le 3 ]; then #1
if [ `arping ...` -ne 1 ]; then #2
sleep 1200
if [ `arping ...` -eq 1 ]; then #3
# do your processing here
fi
fi
fi
The first if statement (#1) makes sure that this is the only instance of this particular script that is running. If another script is (still) running we exit and don't do anything.
The second (#2) is your initial 'is host pingable' test. If it is not, then the script waits 1200s (20min) before testing again (#3).
So, if two pings -- 20 minutes apart -- show that your host has become reachable then your processing code will run.
If you want to simplify things a little, try this:
#!/bin/bash
if [ `arping -w 59 ...` -ne 1 ]; then
sleep 1079
if [ `arping -w 59 ...` -eq 1 ]; then
# do your processing here
fi
fi
If you impose an arping deadline of a little under 1 minute (-w 59) for your checks, and tweak the sleep amount, then you can pretty much guarantee that the two tests and the sleep in between will be completed within your 20 minute period, so there should be no overlap with adjacent periods and no need to check to see if another script is still running.
Either of the above scripts would, of course, be invoked via a static cron entry which runs every 20 minutes:
*/20 * * * * /path/to/script.sh
1
The condition is to check if a particular hostname/IP address is ping-able. I am using arping in this context. If it returns 1, then the address is ping-able, if not I want the code to go and wait as a scheduled cron job. After 20 minutes I again want to ping the same address and only if the address is up, I want to run the scheduled cron job. Otherwise I want to schedule it again after a delay of 20 minutes. Is this do-able using cron and a bash script?
– Arpith
Oct 5 '12 at 2:16
I might be totally wrong but I believe the second condition should be different from the first condition. Right? The second arping condition in the first script should have-ne
rather than-eq
– Arpith
Oct 7 '12 at 15:17
Apologies the confusion. Great answer. Thank you for your time.
– Arpith
Oct 8 '12 at 17:19
@Arpith Yes, you are right. Fixed.
– Tim
Oct 8 '12 at 17:28
Also, if the script is called every 20 minutes it may start overlapping with the sleep period, probably want to cron run every 30 minutes.
– Efren
Aug 3 at 8:08
add a comment |
Put the logic code for testing your condition in the script itself, don't try and put it in (or associate it with) cron - complex logic is not what cron was designed for.
So, in your script you test the condition and, if it evaluates to true, your processing code runs. If it evaluates to false, exit the script cleanly.
Assuming that your 'conditions' change as a result of processing the script (e.g. watching a folder for incoming files that need processing and processing one file every 20m), then eventually your condition will evaluate to false all the time because all the work has been done.
From your comments it looks like you are monitoring the availability of some server.
I don't do a heck of a lot with bash but how about this:
#!/bin/bash
if [ `ps ax | grep $0 | wc -l` -le 3 ]; then #1
if [ `arping ...` -ne 1 ]; then #2
sleep 1200
if [ `arping ...` -eq 1 ]; then #3
# do your processing here
fi
fi
fi
The first if statement (#1) makes sure that this is the only instance of this particular script that is running. If another script is (still) running we exit and don't do anything.
The second (#2) is your initial 'is host pingable' test. If it is not, then the script waits 1200s (20min) before testing again (#3).
So, if two pings -- 20 minutes apart -- show that your host has become reachable then your processing code will run.
If you want to simplify things a little, try this:
#!/bin/bash
if [ `arping -w 59 ...` -ne 1 ]; then
sleep 1079
if [ `arping -w 59 ...` -eq 1 ]; then
# do your processing here
fi
fi
If you impose an arping deadline of a little under 1 minute (-w 59) for your checks, and tweak the sleep amount, then you can pretty much guarantee that the two tests and the sleep in between will be completed within your 20 minute period, so there should be no overlap with adjacent periods and no need to check to see if another script is still running.
Either of the above scripts would, of course, be invoked via a static cron entry which runs every 20 minutes:
*/20 * * * * /path/to/script.sh
1
The condition is to check if a particular hostname/IP address is ping-able. I am using arping in this context. If it returns 1, then the address is ping-able, if not I want the code to go and wait as a scheduled cron job. After 20 minutes I again want to ping the same address and only if the address is up, I want to run the scheduled cron job. Otherwise I want to schedule it again after a delay of 20 minutes. Is this do-able using cron and a bash script?
– Arpith
Oct 5 '12 at 2:16
I might be totally wrong but I believe the second condition should be different from the first condition. Right? The second arping condition in the first script should have-ne
rather than-eq
– Arpith
Oct 7 '12 at 15:17
Apologies the confusion. Great answer. Thank you for your time.
– Arpith
Oct 8 '12 at 17:19
@Arpith Yes, you are right. Fixed.
– Tim
Oct 8 '12 at 17:28
Also, if the script is called every 20 minutes it may start overlapping with the sleep period, probably want to cron run every 30 minutes.
– Efren
Aug 3 at 8:08
add a comment |
Put the logic code for testing your condition in the script itself, don't try and put it in (or associate it with) cron - complex logic is not what cron was designed for.
So, in your script you test the condition and, if it evaluates to true, your processing code runs. If it evaluates to false, exit the script cleanly.
Assuming that your 'conditions' change as a result of processing the script (e.g. watching a folder for incoming files that need processing and processing one file every 20m), then eventually your condition will evaluate to false all the time because all the work has been done.
From your comments it looks like you are monitoring the availability of some server.
I don't do a heck of a lot with bash but how about this:
#!/bin/bash
if [ `ps ax | grep $0 | wc -l` -le 3 ]; then #1
if [ `arping ...` -ne 1 ]; then #2
sleep 1200
if [ `arping ...` -eq 1 ]; then #3
# do your processing here
fi
fi
fi
The first if statement (#1) makes sure that this is the only instance of this particular script that is running. If another script is (still) running we exit and don't do anything.
The second (#2) is your initial 'is host pingable' test. If it is not, then the script waits 1200s (20min) before testing again (#3).
So, if two pings -- 20 minutes apart -- show that your host has become reachable then your processing code will run.
If you want to simplify things a little, try this:
#!/bin/bash
if [ `arping -w 59 ...` -ne 1 ]; then
sleep 1079
if [ `arping -w 59 ...` -eq 1 ]; then
# do your processing here
fi
fi
If you impose an arping deadline of a little under 1 minute (-w 59) for your checks, and tweak the sleep amount, then you can pretty much guarantee that the two tests and the sleep in between will be completed within your 20 minute period, so there should be no overlap with adjacent periods and no need to check to see if another script is still running.
Either of the above scripts would, of course, be invoked via a static cron entry which runs every 20 minutes:
*/20 * * * * /path/to/script.sh
Put the logic code for testing your condition in the script itself, don't try and put it in (or associate it with) cron - complex logic is not what cron was designed for.
So, in your script you test the condition and, if it evaluates to true, your processing code runs. If it evaluates to false, exit the script cleanly.
Assuming that your 'conditions' change as a result of processing the script (e.g. watching a folder for incoming files that need processing and processing one file every 20m), then eventually your condition will evaluate to false all the time because all the work has been done.
From your comments it looks like you are monitoring the availability of some server.
I don't do a heck of a lot with bash but how about this:
#!/bin/bash
if [ `ps ax | grep $0 | wc -l` -le 3 ]; then #1
if [ `arping ...` -ne 1 ]; then #2
sleep 1200
if [ `arping ...` -eq 1 ]; then #3
# do your processing here
fi
fi
fi
The first if statement (#1) makes sure that this is the only instance of this particular script that is running. If another script is (still) running we exit and don't do anything.
The second (#2) is your initial 'is host pingable' test. If it is not, then the script waits 1200s (20min) before testing again (#3).
So, if two pings -- 20 minutes apart -- show that your host has become reachable then your processing code will run.
If you want to simplify things a little, try this:
#!/bin/bash
if [ `arping -w 59 ...` -ne 1 ]; then
sleep 1079
if [ `arping -w 59 ...` -eq 1 ]; then
# do your processing here
fi
fi
If you impose an arping deadline of a little under 1 minute (-w 59) for your checks, and tweak the sleep amount, then you can pretty much guarantee that the two tests and the sleep in between will be completed within your 20 minute period, so there should be no overlap with adjacent periods and no need to check to see if another script is still running.
Either of the above scripts would, of course, be invoked via a static cron entry which runs every 20 minutes:
*/20 * * * * /path/to/script.sh
edited Oct 8 '12 at 17:36
answered Oct 4 '12 at 14:24
Tim
28128
28128
1
The condition is to check if a particular hostname/IP address is ping-able. I am using arping in this context. If it returns 1, then the address is ping-able, if not I want the code to go and wait as a scheduled cron job. After 20 minutes I again want to ping the same address and only if the address is up, I want to run the scheduled cron job. Otherwise I want to schedule it again after a delay of 20 minutes. Is this do-able using cron and a bash script?
– Arpith
Oct 5 '12 at 2:16
I might be totally wrong but I believe the second condition should be different from the first condition. Right? The second arping condition in the first script should have-ne
rather than-eq
– Arpith
Oct 7 '12 at 15:17
Apologies the confusion. Great answer. Thank you for your time.
– Arpith
Oct 8 '12 at 17:19
@Arpith Yes, you are right. Fixed.
– Tim
Oct 8 '12 at 17:28
Also, if the script is called every 20 minutes it may start overlapping with the sleep period, probably want to cron run every 30 minutes.
– Efren
Aug 3 at 8:08
add a comment |
1
The condition is to check if a particular hostname/IP address is ping-able. I am using arping in this context. If it returns 1, then the address is ping-able, if not I want the code to go and wait as a scheduled cron job. After 20 minutes I again want to ping the same address and only if the address is up, I want to run the scheduled cron job. Otherwise I want to schedule it again after a delay of 20 minutes. Is this do-able using cron and a bash script?
– Arpith
Oct 5 '12 at 2:16
I might be totally wrong but I believe the second condition should be different from the first condition. Right? The second arping condition in the first script should have-ne
rather than-eq
– Arpith
Oct 7 '12 at 15:17
Apologies the confusion. Great answer. Thank you for your time.
– Arpith
Oct 8 '12 at 17:19
@Arpith Yes, you are right. Fixed.
– Tim
Oct 8 '12 at 17:28
Also, if the script is called every 20 minutes it may start overlapping with the sleep period, probably want to cron run every 30 minutes.
– Efren
Aug 3 at 8:08
1
1
The condition is to check if a particular hostname/IP address is ping-able. I am using arping in this context. If it returns 1, then the address is ping-able, if not I want the code to go and wait as a scheduled cron job. After 20 minutes I again want to ping the same address and only if the address is up, I want to run the scheduled cron job. Otherwise I want to schedule it again after a delay of 20 minutes. Is this do-able using cron and a bash script?
– Arpith
Oct 5 '12 at 2:16
The condition is to check if a particular hostname/IP address is ping-able. I am using arping in this context. If it returns 1, then the address is ping-able, if not I want the code to go and wait as a scheduled cron job. After 20 minutes I again want to ping the same address and only if the address is up, I want to run the scheduled cron job. Otherwise I want to schedule it again after a delay of 20 minutes. Is this do-able using cron and a bash script?
– Arpith
Oct 5 '12 at 2:16
I might be totally wrong but I believe the second condition should be different from the first condition. Right? The second arping condition in the first script should have
-ne
rather than -eq
– Arpith
Oct 7 '12 at 15:17
I might be totally wrong but I believe the second condition should be different from the first condition. Right? The second arping condition in the first script should have
-ne
rather than -eq
– Arpith
Oct 7 '12 at 15:17
Apologies the confusion. Great answer. Thank you for your time.
– Arpith
Oct 8 '12 at 17:19
Apologies the confusion. Great answer. Thank you for your time.
– Arpith
Oct 8 '12 at 17:19
@Arpith Yes, you are right. Fixed.
– Tim
Oct 8 '12 at 17:28
@Arpith Yes, you are right. Fixed.
– Tim
Oct 8 '12 at 17:28
Also, if the script is called every 20 minutes it may start overlapping with the sleep period, probably want to cron run every 30 minutes.
– Efren
Aug 3 at 8:08
Also, if the script is called every 20 minutes it may start overlapping with the sleep period, probably want to cron run every 30 minutes.
– Efren
Aug 3 at 8:08
add a comment |
You would have to build a bash script that checked for the conditional and assigned a sleep to it. Then schedule that bash script to run at a specific time using cron. Cron itself doesn't not have support for conditionals or 'run after a delay'.
add a comment |
You would have to build a bash script that checked for the conditional and assigned a sleep to it. Then schedule that bash script to run at a specific time using cron. Cron itself doesn't not have support for conditionals or 'run after a delay'.
add a comment |
You would have to build a bash script that checked for the conditional and assigned a sleep to it. Then schedule that bash script to run at a specific time using cron. Cron itself doesn't not have support for conditionals or 'run after a delay'.
You would have to build a bash script that checked for the conditional and assigned a sleep to it. Then schedule that bash script to run at a specific time using cron. Cron itself doesn't not have support for conditionals or 'run after a delay'.
answered Oct 4 '12 at 11:23
Drahkar
1213
1213
add a comment |
add a comment |
Okay you updated the question to minutes now.
So you would run every 20 minutes = run at 0 / 20 / 40 / 60 (=0) minute of all hours.
The entry would be:
0,20,40 * * * * /path/to/some_command
And if you want to execute it for limited times, use a state file to store how many times you've launched that script.
Sorry. You're right. Made the appropriate changes in the question.
– Arpith
Oct 4 '12 at 11:35
You have not said anything about the execution of the cron job based on the evaluation of a conditional statement.
– Arpith
Oct 4 '12 at 13:02
add a comment |
Okay you updated the question to minutes now.
So you would run every 20 minutes = run at 0 / 20 / 40 / 60 (=0) minute of all hours.
The entry would be:
0,20,40 * * * * /path/to/some_command
And if you want to execute it for limited times, use a state file to store how many times you've launched that script.
Sorry. You're right. Made the appropriate changes in the question.
– Arpith
Oct 4 '12 at 11:35
You have not said anything about the execution of the cron job based on the evaluation of a conditional statement.
– Arpith
Oct 4 '12 at 13:02
add a comment |
Okay you updated the question to minutes now.
So you would run every 20 minutes = run at 0 / 20 / 40 / 60 (=0) minute of all hours.
The entry would be:
0,20,40 * * * * /path/to/some_command
And if you want to execute it for limited times, use a state file to store how many times you've launched that script.
Okay you updated the question to minutes now.
So you would run every 20 minutes = run at 0 / 20 / 40 / 60 (=0) minute of all hours.
The entry would be:
0,20,40 * * * * /path/to/some_command
And if you want to execute it for limited times, use a state file to store how many times you've launched that script.
edited Dec 17 at 16:30
JakeGould
1698
1698
answered Oct 4 '12 at 11:18
daisy
28.4k48167300
28.4k48167300
Sorry. You're right. Made the appropriate changes in the question.
– Arpith
Oct 4 '12 at 11:35
You have not said anything about the execution of the cron job based on the evaluation of a conditional statement.
– Arpith
Oct 4 '12 at 13:02
add a comment |
Sorry. You're right. Made the appropriate changes in the question.
– Arpith
Oct 4 '12 at 11:35
You have not said anything about the execution of the cron job based on the evaluation of a conditional statement.
– Arpith
Oct 4 '12 at 13:02
Sorry. You're right. Made the appropriate changes in the question.
– Arpith
Oct 4 '12 at 11:35
Sorry. You're right. Made the appropriate changes in the question.
– Arpith
Oct 4 '12 at 11:35
You have not said anything about the execution of the cron job based on the evaluation of a conditional statement.
– Arpith
Oct 4 '12 at 13:02
You have not said anything about the execution of the cron job based on the evaluation of a conditional statement.
– Arpith
Oct 4 '12 at 13:02
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%2f49868%2fconditional-crontab-command-in-bash-script%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