This paper proposes a distributed consensus algorithm in predefined time of time‐varying optimization with inequality constrains for multi‐agent systems (MASs) with external disturbances over an undirected graph. Firstly, by utilizing exponential function, a novel criterion with respect to the stability in predefined time is developed for the nonlinear systems. Secondly, a new distributed switching control protocol, which can inhibit the external unbounded disturbances, is piecewise designed to drive the states of agents to the sliding mode surface via the predefined‐time set by users. Subsequently, under the designed event‐triggered controller (ETC), the states of agents achieve consensus in predefined time on the sliding mode surface. Meanwhile, with the help of log barrier function, the consensus states of agents reach the feasible region and converge to the optimal solution in predefined time. Finally, the correctness and availability of the proposed optimization control algorithm are verified by a simulation example.