0

I am pretty new to Unix environment.

I am trying to schedule two tasks in Unix server. The second task is dependent on the result of the first task. So, I want to run the first task. If there is no error then I want the second task to run automatically. But if the first task fails, I want to reschedule the first task again after 30 minutes.

I have no idea where to start from.

Jens
  • 61,963
  • 14
  • 104
  • 160
Ishwor Bhatta
  • 119
  • 1
  • 2
  • 12

1 Answers1

0

You don't need cron for this. A simple shell script is all you need:

#!/bin/sh

while :; do             # Loop until the break statement is hit
   if task1; then       # If task1 is successful
      task2             # then run task2
      break             # and we're done.
   else                 # otherwise task1 failed
      sleep 1800        # and we wait 30min
   fi                   # repeat
done

Note that task1 must indicate success with an exit status of 0, and failure with nonzero.

As Wumpus sharply observes, this can be simplified to

#!/bin/sh
until task1; do
   sleep 1800
done
task2
Jens
  • 61,963
  • 14
  • 104
  • 160
  • Could be less nested: `until task1; do sleep 1800; done; task2` –  Mar 29 '17 at 14:33
  • @Jens Thank you. I am sorry my question was not clear. Task two needs to be scheduled to run only at certain time but need to make sure task 1 runs successfully before running task 2. – Ishwor Bhatta Mar 29 '17 at 14:33
  • @WumpusQ.Wumbley Well observed! Your version is more simple and should be preferred. – Jens Mar 29 '17 at 14:36
  • @IshworBhatta Then task1 would somehow need to record whether it ran successfully. That logic is twisted. I'd think about whether I could change the problem so that it can be solved with a serial solution like we've shown. Or could task1 be integrated into task2? – Jens Mar 29 '17 at 14:43
  • @Jens. Thank you for your help. I think I can realign the problem and integrate the two tasks to make it works. – Ishwor Bhatta Mar 29 '17 at 15:00
  • @WumpusQ.Wumbley. Thank you for your help. – Ishwor Bhatta Mar 29 '17 at 15:10