0

i have a unix script in which a command needs to be executed repeatedly but the command can be run next time only when the previous one have completed successfully. I searched for the command that can tell that the script has finished execution but i couldnt find it. I am new in the unix scripting and started to love unix scripting.

3
  • If you don't explicitly run a command in the background, the next line of the script only executes when the command finishes. Commented Oct 7, 2013 at 14:54
  • earlier i used like: #!/bin/sh command1; command2; ....but the problem was that all the given commands executed simultaneously. then after each command i gave sleep 20 after estimating the time for the execution of each command. it worked really well :) Commented Oct 8, 2013 at 7:56
  • So command1 starts a background job even if you run it in the foreground? Is there any way to prevent that? For example, some daemons have an option like --interactive to run in the foreground. Commented Oct 8, 2013 at 8:39

2 Answers 2

1

In order for a command to execute only after the previous one has succeeded, you need to write the two as:

command1 && command2

To have this in a loop with a single command, you will need to check the return status of each invocation and exit the loop if it's not successful; the shortest form should be something like:

while your_command; do :; done

You could also insert a sleep instead of the NOOP :.

Sign up to request clarification or add additional context in comments.

4 Comments

yes :D the sleep option. it would look really dumb but i tried with the sleep commmand after each command i give the sleep option. it worked fine but it is only workable thing .
@10hero: I'm not sure I got that... To use sleep, you need to write sleep <seconds>; but most likely I misunderstood your comment...
sorry!! actually i used sleep <seconds> after each command estimating the time of execution of the each command as the same command needs to be used everytime.
No need to estimate anything. The wile loop will wait for each invocation to complete before proceeding with the next one. Sleep is only meant to protect you in case your commands exit too fast and you don't want to run the command 100 times a second. Otherwise, the noop : should be fine.
0

You need to store PID (process id) of your background command in some file, so that your script can check if its still running next time it is started. For example:

if [ ! "kill -0 $(</var/run/myscript-command.pid) 2>/dev/null 1>&2" ]; then
    somecommand&
    $CMD_PID = $!
    echo $CMD_PID >/var/run/myscript-command.pid
fi

For this to work somecommand needs to daemonize itself. If it does not, then call it like:

nohup ./somecommand 0<&- &>/dev/null &

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.