A good means of meriting a good death is devotion to St. Joseph, patron of the dying. He had the grace of dying in the arms of Jesus and Mary.
O St. Joseph, guide of those in their last agony, grant me through your intercession to have the ineffable consolation to breathe forth my soul in peace in the presence of Jesus and Mary. St. Joseph, patron of the dying, intercede for me. Amen