The court, by injunction, on good cause shown, may restrain the party in possession from doing any act to the injury of real property during the existence of the lien or foreclosure of a mortgage thereon and until the expiration of the time allowed for redemption. If before the sheriff's sale the mortgagee or after the sheriff's sale the holder of the sheriff's certificate of sale reasonably believes that the property is abandoned, the mortgagee or holder of the sheriff's certificate may petition the court to determine abandonment. A notice of hearing must be sent by mail to the last-known address of the mortgagor or the party entitled to possession of the real property at least ten days prior to the date of the hearing to determine abandonment. Service by mail is complete upon mailing. If the court determines that the real property is abandoned, the court may grant the mortgagee or holder of the sheriff's certificate immediate possession and use of the property and all benefit and rents from the property until expiration of the redemption period. The court may proceed at the hearing to consider remedies to prevent waste. The provisions of this section concerning abandoned real property do not apply to agricultural property as defined by section 57-02-01.