This paper describes the optimization of batch processes in the presence of uncertainty and constraints. The optimal solution consists of keeping certain path and terminal constraints active and driving the sensitivities to zero. The case where the terminal constraints have a larger bearing on the cost than the sensitivities is considered, for which a two-time-scale methodology is proposed. The problem of meeting the active terminal constraints is addressed on-line using trajectory tracking, whilst pushing the sensitivities to zero is implemented on a run-to-run basis. The paper also discusses the run-to-run improvement of trajectory tracking via iterative learning control. The proposed methodology is illustrated in simulation on a batch distillation system.