This paper develops an optimal control policy for unreliable manufacturing systems evolving in a stochastic and dynamic environment and producing perishable products with limited and random shelf lives. The developed optimal control policy aims to minimize the total cost, composed of backlog, inventory holding and disposal costs. To do this, we develop a stochastic dynamic programming model and the obtained optimality conditions are solved using a numerical approach to determine the parametrized structure of the resulting optimal control policy. A simulation-based optimization approach is used to optimize the parameters of the developed control policy. This policy is subsequently analyzed using multiple sensitivity analyses and compared to other policies from the literature. We also study shelf life variability and its influence on the system performance. Numerical examples show that in the case of a random shelf life, the developed control policy presents a lower total cost in comparison with other policies from the literature.