A mentally ill man was sentenced to 30 months of probation Thursday after he pleaded guilty to choking an 8-year-old girl unconscious inside a bathroom of a South Loop deli.

Reese Hartstirn’s plea comes after days of testimony in a bench trial that had been stretched over several months at the Leighton Criminal Courthouse.

Days before the girl’s mother found Hartstirn attacking her daughter in May 2016, Hartstirn had been hearing voices and experiencing visions and paranoid delusions, according to Cook County court records.

The 37-year-old, who often attended court hearings accompanied by more than a dozen family members, will be required to undergo continued psychiatric care for the duration of his probation, and court records detail a string of hospitalizations and Hartstirn’s declining mental state leading up to the attack.

Before the attack, Hartstirn, a project manager for a construction company, went to get supplies from a hardware store, bought a crate of bottled water, and sat in the store and spent what he thought might have been several hours contemplating the various “signs” he had encountered in recent days, court records said.

He then drove around, returning to the house he’d been working on after dark, and locked himself inside, fearing “evil spirits” he believed were circling the house.

Hartstirn’s boss found him at the site, and took him to get something to eat at a South Loop sandwich shop.

There, Hartstirn wandered into the women’s restroom, thinking it was a men’s room, to hide in a stall when he saw the girl.

Overcome with fear, Hartstirn grabbed the girl and covered her mouth when she screamed, then began choking her, releasing her only after her eyes closed, according to court records.

Police reports indicate that after the girl’s mother burst into the stall, Harstirn went back and sat next to his boss until officers arrived. At the station, Hartstirn allegedly stripped naked and tried to urinate on officers in the lockup.

Court records indicate that Hartstirn was hospitalized for days after his arrest.