Whenever a peremptory mandamus is directed to a public officer or body commanding the performance of any public duty specially enjoined by law, if it appears to the court or judge thereof that the officer or any member of the body has without just excuse refused or neglected to perform the duty so enjoined, the court or judge may impose a fine, not exceeding $500, upon every such officer or member of such body; and the payment thereof is a bar to any action for any penalty incurred by the officer or member by reason of the refusal or neglect of the officer or member to perform the duty so enjoined.