The High Court in Glasgow heard that Peter Boyd, 27, went to his local Spar shop in Annan, Dumfrieshire, with two knives after posting on Facebook he was going to carry out the raid.
He held up two female workers and sparked a major incident with a firearms unit and a police negotiator on August 10 last year.
Yesterday judge Lord Turnbull told Boyd that, but for his early plea of guilty to a charge of abduction and robbery, he would have jailed him for eight years.
Lord Turnbull added: "You pleaded guilty to a most serious offence, one that is fortunately only seen rarely. The impact on the victims was significant and serious as was the disruption in the local area."
Boyd said he carried out the offence as he was upset because it was near the anniversary of his mother's death.