One should ensure that he performs a good deed before sleeping, so that if death is written for one at that time, one’s last actions will be righteous and pious. One should ensure that he does not carry out any evil deed before sleeping.
عن جابر، رضي الله عنه أن رسول الله صلى الله عليه وسلم قال: إن الرجل إذا أوى إلى فراشه ابتدره ملك وشيطان، فقال الملك: اللهم اختم بخير، وقال الشيطان: اختم بشر، فإن ذكر الله عز وجل ثم نام، بات الملك يكلؤه (الترغيب والترهيب رقم 892)
Hadhrat Jaabir (Radhiyallahu Anhu) reports that Rasulullah (Sallallahu Alaihi Wasallam) said: “When a person retires to bed, an angel and a shaytaan both come to him. Then the angel makes dua for him saying: “May your end be upon good.” And the shaytaan says: “May your end be upon evil.” If he remembers Allah Ta’ala and thereafter falls off to sleep, the angel spends the night protecting him and safeguarding him from any type of evil.”