If the officer or person to whom the writ is directed refuses, after service thereof, to obey it, the court or judge shall, upon affidavit, issue an attachment against such person, directed to the sheriff, commanding him forthwith to apprehend the person, and bring him immediately before such court or judge. Upon being brought before the court or judge, such officer or person shall be committed to jail until he makes due return to the writ, or is otherwise legally discharged.