Wait for the completion of previous execution for shell script
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I need to execute a shell script every hour. However I don't want to start the next run till the previous one is complete.
Please advise.
linux scripting
add a comment |Â
up vote
1
down vote
favorite
I need to execute a shell script every hour. However I don't want to start the next run till the previous one is complete.
Please advise.
linux scripting
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I need to execute a shell script every hour. However I don't want to start the next run till the previous one is complete.
Please advise.
linux scripting
I need to execute a shell script every hour. However I don't want to start the next run till the previous one is complete.
Please advise.
linux scripting
asked Nov 4 '17 at 15:06
puneet gupta
211
211
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
2
down vote
Since you tagged the question as Linux, you can use flock
to put a lock on a well-known file:
flock "$TMPDIR:-/tmp"/some_name.lock /path/to/your_command -options
A second instance of the command (run the same way) will wait until the first instance has finished. The problem with this approach is, if the command regularly takes more than an hour you'll just accumulate more and more instances waiting for the lock.
Alternatively, you can just skip running a second instance if the first hasn't finished:
flock -n "$TMPDIR:-/tmp"/some_name.lock /path/to/your_command -options
The problem with this approach is you may run the command fewer that 24 times per day.
add a comment |Â
up vote
1
down vote
You need to define your problem better.
Sato has described how a method where new instances will back up behind a delayed one.
Jaroslav's solution only provides a mechanism for detecting that the previous instance has not yet finished cleanly.
Another option would be to schedule the next instance of the script inside the script itself:
at now + 1 hour $*
You need to define the behaviour if the previous instance has not completed, and whether you want instances to start at hourly intervals or within a defined interval between completion and start.
Thanks, Sato solution is solving my problem. Mostly my jobs will complete in an hour. However if not then I wants to wait for certain time.
â puneet gupta
Nov 4 '17 at 18:11
add a comment |Â
up vote
0
down vote
You can create some temporary file on script start and delete it before script close. The script should check for existence of that file. If it finds it, it'd just close and do nothing. If it doesn't, create it and continue.
Or in the cron job, you can use pgrep for checking the script being executed and continue only if it doesn't.
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
Since you tagged the question as Linux, you can use flock
to put a lock on a well-known file:
flock "$TMPDIR:-/tmp"/some_name.lock /path/to/your_command -options
A second instance of the command (run the same way) will wait until the first instance has finished. The problem with this approach is, if the command regularly takes more than an hour you'll just accumulate more and more instances waiting for the lock.
Alternatively, you can just skip running a second instance if the first hasn't finished:
flock -n "$TMPDIR:-/tmp"/some_name.lock /path/to/your_command -options
The problem with this approach is you may run the command fewer that 24 times per day.
add a comment |Â
up vote
2
down vote
Since you tagged the question as Linux, you can use flock
to put a lock on a well-known file:
flock "$TMPDIR:-/tmp"/some_name.lock /path/to/your_command -options
A second instance of the command (run the same way) will wait until the first instance has finished. The problem with this approach is, if the command regularly takes more than an hour you'll just accumulate more and more instances waiting for the lock.
Alternatively, you can just skip running a second instance if the first hasn't finished:
flock -n "$TMPDIR:-/tmp"/some_name.lock /path/to/your_command -options
The problem with this approach is you may run the command fewer that 24 times per day.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Since you tagged the question as Linux, you can use flock
to put a lock on a well-known file:
flock "$TMPDIR:-/tmp"/some_name.lock /path/to/your_command -options
A second instance of the command (run the same way) will wait until the first instance has finished. The problem with this approach is, if the command regularly takes more than an hour you'll just accumulate more and more instances waiting for the lock.
Alternatively, you can just skip running a second instance if the first hasn't finished:
flock -n "$TMPDIR:-/tmp"/some_name.lock /path/to/your_command -options
The problem with this approach is you may run the command fewer that 24 times per day.
Since you tagged the question as Linux, you can use flock
to put a lock on a well-known file:
flock "$TMPDIR:-/tmp"/some_name.lock /path/to/your_command -options
A second instance of the command (run the same way) will wait until the first instance has finished. The problem with this approach is, if the command regularly takes more than an hour you'll just accumulate more and more instances waiting for the lock.
Alternatively, you can just skip running a second instance if the first hasn't finished:
flock -n "$TMPDIR:-/tmp"/some_name.lock /path/to/your_command -options
The problem with this approach is you may run the command fewer that 24 times per day.
answered Nov 4 '17 at 15:33
Satà  Katsura
10.7k11533
10.7k11533
add a comment |Â
add a comment |Â
up vote
1
down vote
You need to define your problem better.
Sato has described how a method where new instances will back up behind a delayed one.
Jaroslav's solution only provides a mechanism for detecting that the previous instance has not yet finished cleanly.
Another option would be to schedule the next instance of the script inside the script itself:
at now + 1 hour $*
You need to define the behaviour if the previous instance has not completed, and whether you want instances to start at hourly intervals or within a defined interval between completion and start.
Thanks, Sato solution is solving my problem. Mostly my jobs will complete in an hour. However if not then I wants to wait for certain time.
â puneet gupta
Nov 4 '17 at 18:11
add a comment |Â
up vote
1
down vote
You need to define your problem better.
Sato has described how a method where new instances will back up behind a delayed one.
Jaroslav's solution only provides a mechanism for detecting that the previous instance has not yet finished cleanly.
Another option would be to schedule the next instance of the script inside the script itself:
at now + 1 hour $*
You need to define the behaviour if the previous instance has not completed, and whether you want instances to start at hourly intervals or within a defined interval between completion and start.
Thanks, Sato solution is solving my problem. Mostly my jobs will complete in an hour. However if not then I wants to wait for certain time.
â puneet gupta
Nov 4 '17 at 18:11
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You need to define your problem better.
Sato has described how a method where new instances will back up behind a delayed one.
Jaroslav's solution only provides a mechanism for detecting that the previous instance has not yet finished cleanly.
Another option would be to schedule the next instance of the script inside the script itself:
at now + 1 hour $*
You need to define the behaviour if the previous instance has not completed, and whether you want instances to start at hourly intervals or within a defined interval between completion and start.
You need to define your problem better.
Sato has described how a method where new instances will back up behind a delayed one.
Jaroslav's solution only provides a mechanism for detecting that the previous instance has not yet finished cleanly.
Another option would be to schedule the next instance of the script inside the script itself:
at now + 1 hour $*
You need to define the behaviour if the previous instance has not completed, and whether you want instances to start at hourly intervals or within a defined interval between completion and start.
answered Nov 4 '17 at 16:16
symcbean
2,23911121
2,23911121
Thanks, Sato solution is solving my problem. Mostly my jobs will complete in an hour. However if not then I wants to wait for certain time.
â puneet gupta
Nov 4 '17 at 18:11
add a comment |Â
Thanks, Sato solution is solving my problem. Mostly my jobs will complete in an hour. However if not then I wants to wait for certain time.
â puneet gupta
Nov 4 '17 at 18:11
Thanks, Sato solution is solving my problem. Mostly my jobs will complete in an hour. However if not then I wants to wait for certain time.
â puneet gupta
Nov 4 '17 at 18:11
Thanks, Sato solution is solving my problem. Mostly my jobs will complete in an hour. However if not then I wants to wait for certain time.
â puneet gupta
Nov 4 '17 at 18:11
add a comment |Â
up vote
0
down vote
You can create some temporary file on script start and delete it before script close. The script should check for existence of that file. If it finds it, it'd just close and do nothing. If it doesn't, create it and continue.
Or in the cron job, you can use pgrep for checking the script being executed and continue only if it doesn't.
add a comment |Â
up vote
0
down vote
You can create some temporary file on script start and delete it before script close. The script should check for existence of that file. If it finds it, it'd just close and do nothing. If it doesn't, create it and continue.
Or in the cron job, you can use pgrep for checking the script being executed and continue only if it doesn't.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You can create some temporary file on script start and delete it before script close. The script should check for existence of that file. If it finds it, it'd just close and do nothing. If it doesn't, create it and continue.
Or in the cron job, you can use pgrep for checking the script being executed and continue only if it doesn't.
You can create some temporary file on script start and delete it before script close. The script should check for existence of that file. If it finds it, it'd just close and do nothing. If it doesn't, create it and continue.
Or in the cron job, you can use pgrep for checking the script being executed and continue only if it doesn't.
answered Nov 4 '17 at 15:18
Jaroslav Kucera
4,3754621
4,3754621
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f402511%2fwait-for-the-completion-of-previous-execution-for-shell-script%23new-answer', 'question_page');
);
Post as a guest
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
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
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