A sheriff's deputy was in critical condition Thursday after being shot in the head while attempting to serve a warrant at an apartment building in Texas, authorities said. A suspect was in custody.

The deputy and another officer had entered a Dollar General store in La Grange to serve a felony warrant to a man who failed to register as a sex offender, the La Grange Police Department said.

Deputies used a stun gun on the suspect after he resisted arrest, La Grange Police Chief David Gilbreath said. During the confrontation, the suspect cut one officer’s hand with a buck knife, then escaped to his apartment and barricaded himself inside.

Deputies followed the suspect to the apartment to try serving the warrant again, but were immediately fired upon, FOX 7 Austin reported. One deputy was shot in the head.

The unidentified Fayetteville County deputy was alert and talking as he was rushed to a hospital in Austin, the Austin American-Statesman reported. He remained in critical condition.

Texas Department of Public Safety, SWAT and La Grange police officers responded to the scene of the shooting, Austin's KXAN-TV reported. The suspect was barricaded for about four hours before he was taken into custody, Gilbreath said.

La Grange, a town of about 4,600 residents, is about 70 miles southeast of Austin, along state Highway 71.