Substitute king ritual
The substitute king ritual was an ancient Mesopotamian religious rite of human sacrifice, documented most clearly in Assyria, which was performed to safeguard the king from danger perceived to come from evil omens. These omens were generally thought to arise from certain astronomical events such as eclipses. During the course of the ritual the king symbolically abdicates his throne and a substitute is placed there in his stead for a period of up to 100 days. Though he possessed no real power, the substitute king would live and hold court at the palace and enjoy the wealth and prestige of the king. Simultaneously the real king would go into hiding, accessible only to his closest advisors. Throughout the ritual many exorcistic rites were performed in order to transfer the danger arising from the evil omens from the real king onto the substitute. At the end of this time the substitute is put to death and the real king, having successfully transferred his doomed fate onto the scapegoat, returns to his throne. Reasons for performance 
The ritual was performed because of evil omens foretelling the death of the king. These omens were derived from eclipses, both lunar and solar. While every attested instance of the ritual's performance is traceable to an eclipse it is possible that other astronomical events such as planetary occultations could also trigger the ritual. A tablet describing the ritual, discovered in the library of Ashurbanipal, states the reasons for its performance as  "the evil portent of evil and unlucky signs, eclipses of the moon, the sun, Jupiter, Venus, Mercury and Saturn, [and Mars]." Not every eclipse would provoke the ritual. Certain circumstances had to be fulfilled. Tablet 20 of the Enuma Anu Enlil, a record collection of astrological omens, states: "if an eclipse (of the moon) takes place and the planet Jupiter is present in that eclipse, the king is safe; a noble dignitary will die in his stead." Another source states, in the case of a solar eclipse, "If an eclipse (of the sun) takes place and Venus and Jupiter are visible, the king is safe, but the country will be attacked by an enemy." Excepting the above examples, every eclipse in essence signified that a powerful king would die, but the identity and location of the doomed monarch had to be determined separately. This was done by analyzing the features of the eclipse such as the date and time, the direction of its transit, and the obscured region of the moon or sun's disc. The obscured area of the moon or sun's disc was of primary importance in determining the identity of the endangered king and country. The disc was separated into four parts corresponding to the four quarters of the Assyrian's known world. These were Amurru to the west, Elam to the east, Subartu to the north, and Akkad in the south. The darkening of a particular quadrant during an eclipse signified a danger to the relevant country and a total eclipse represented a universal danger. Assyrian kings were in danger when the lowest quadrant (corresponding to Subartu) was eclipsed. For kings such as Esarhaddon who ruled both Assyria and Babylonia, a darkened right-side quadrant (corresponding to Akkad) also signified danger. Thus in Assyria the substitute king ritual was necessary when the eclipse fulfilled these conditions and Jupiter was not simultaneously visible. Ritual procedure

Announcement and recommendation to the king 
When it had been determined that the ritual was necessary, scholars from the king's inner circle would inform him. Sometimes this was done solely by the chief exorcist, though generally it seems that a group of high-level scholars such as the chief exorcist, chief scribe, and chief chanter would collectively inform the king of the eclipse's bad omens. Normally this was only done after the eclipse had already occurred, but in a few unusual cases it seems to have happened beforehand. With two exceptions, the suggestion of performing the substitute king ritual always came from scholars in the king's inner circle, and they were the only ones who participated in the rites pertaining to the enthronement of the substitute. While the king himself technically had the final say in whether or not to proceed with the ritual, it was always assumed that he would consent to its performance. Choosing a substitute 
After receiving permission from the king, the chief exorcist would begin searching for a man to serve as the substitute. Generally this was a condemned criminal, a prisoner of war, a political adversary, or a menial laborer. An ideal substitute was someone whose death would not cause a great deal of unrest amongst the people. Often when writing letters to the king, officials would symbolically declare their willingness to serve as substitute with the phrase "ana dinān šarri bēlīya lullik", meaning " "I would go as a substitute for the king, my lord,"

Rites of enthronement and substitution 
Once a substitute was chosen he would be taken to the palace where he was washed, anointed, and given wine and food. He was dressed in the king's robes and decorated with the royal diadem and other crown jewels then placed upon the throne. Extant ritual tablets also state that a "girl" or "virgin" was chosen to serve as his "queen" and seated by his side. The substitute was also given a statuette to present to the gods of the underworld. This was the point in the ritual where the real king and the substitute formally switched roles. This is further indicated by the fact that the real king, after the enthronement of the substitute, was addressed as "the peasant" or "the farmer" by the few advisors who could still access him. Though not specified in the texts, it seems likely that the real king also did not wear any outward signifiers of kingship nor occupy the throne during the reign of the substitute. To make sure that they were properly transferred to the substitute, all bad omens pertaining to the eclipse along with any other ominous signs were written down and proclaimed to the substitute king and queen after their placement on the throne. The substitutes then had to recite the omens in front of the god Shamash, the cosmic judge. This was a very important part of ritual, overseen by the chief scribe and accompanied by exorcistic rites, which served to officially transfer the omen of death from the real king onto the substitute. To make sure this exchange of fate was irreversible, the record where the omens were written was physically attached to the clothes of the substitute king. Certain eclipses called for the enthronement rites to be repeated. Since the substitute had to be crowned and enthroned in the residence of the king, this meant that Assyrian kings who also ruled Babylon (such as Esarhaddon) had to enthrone substitute kings there when its corresponding lunar quadrant was eclipsed. In cases where the quadrants of both Assyria (Subartu) and Babylon (Akkad) were eclipsed, the rites had to be performed with the same substitute in both Nineveh and Babylon. This happened three times during the reign of Esarhaddon. Since Babylon was in ruins during his reign and did not possess a suitable royal palace, Esarhaddon chose to have the enthronement rites performed in the ancient city of Akkad, which once served as the capital of the Sargonic empire. The substitute king maintained a sizable court, possibly between one-third to one-half the size of the real king's court. Most of these were professionals meant to keep the substitute entertained, such as cooks and musicians. Some scholars suggest that the substitute's retinue consisted of a disproportionate amount of bodyguards in order to keep him under control and surveillance but other scholars dispute this. The doom foretold by the eclipse was believed to occur within 100 days after the eclipse. Thus the reign of the substitute could and often did last this long. It was also possible for the ritual to conclude considerably earlier than this. There are records of substitute king rituals respectively lasting 47 days, 20 days, 7 days, and even as short as 3 days. This disparity shows that the length of the substitute's reign was not of primary importance and was likely influenced by factors such as additional eclipses or other astrological observations. While the substitute occupied the throne the real king and royal family were advised to stay within the palace and not travel abroad until the ritual had concluded. Aside from this the king seemed to carry on as usual, receiving letters and supervising administrative business. It appears that scholars and priests were the only officials with access to the king while he was in hiding, as there are no known letters from governors or palace officials addressed to the king's ritual alias "the Farmer." There are also no records which suggest any of these officials knew that a substitute was enthroned. Killing 
To conclude the ritual, after the allotted time the substitute king and queen were put to death. The method of execution is unclear but some sources point to an overdose of "soporific." Other sources suggest that harsher and more violent means were the norm. Based on existing evidence it does not appear that the manner of death was of primary importance. When referring to the death of the substitute, records use the phrase "to go to one's fate." This is not because of an aversion to talking openly about it, but rather because the substitute's death was seen as predestined and inevitable. Burial rites 
There is only one known inscription describing the burial of a substitute king. It describes how the substitute king and queen's bodies were dressed, anointed, and buried with full royal honors replete with public display and mourning. They were placed in a mausoleum built specifically for them. It's unclear whether this publicity was exceptional or standard practice in the ritual. During the burial many exorcistic rites were performed. The substitute was interred with the figurine which had been given to him representing "everything that is evil" so he could present it to the gods of the underworld. The royal palace was surrounded with protective figurines meant to ward off any evil threatening the king. After the death of the substitute the real king was ritually purified. The method of this is unknown but it likely involved a shaving ritual, bathing in the Tigris, incense burning, and dressing in new clothes. It can be presumed that these rituals were meant to pacify the king's nervous psyche as much as cleanse his body. Frequency 
It is unclear generally how often the substitute king ritual was performed in Assyria. Esarhaddon had it performed seven times during his reign. If each of these rituals extended to its full term of one hundred days, this would mean that Esarhaddon spent almost one-fifth of his time during those years in hiding with a substitute on the throne. There is record of one performance early during the reign of Ashurbanipal but it seems he used an inanimate statue as a substitute instead of a real person. The frequency with which Esarhaddon had the ritual performed along with the high level of influence possessed by scholars in his inner circle seems to suggest that the substitute king ritual was utilized by those scholars as a means by which to control the king's movement and limit access to him. This theory is supported by the reluctance of his successor Ashurbanipal to perform the ritual despite the many suitable eclipses which occurred during his reign. Aside from these instances the only Neo-Assyrian records of the ritual's performance date from between the years 786 and 783 BC, during the reign of Adad-nirari III. Other historical mentions 
The earliest mention of the substitute king ritual is from the Chronicle of Early Kings and mentions a (likely fabricated) story from the reign of king Erra-Immitti of Isin(1860-1837 BC):

" King Erra-imitti had the gardener Illil-bani seated on his throne as a substitute statue and placed his royal tiara upon his head. Erra-imitti died in his palace while swallowing hot porridge. Illil-bani who was sitting on the throne did not leave it, and was accepted as the king." Herodotus seems to mention- in very general terms- the ritual in his Histories:

"Xerxes then, being very greatly disturbed by fear of the vision, started up from his bed and sent a messenger to summon Artabanos; to whom when he came Xerxes spoke thus:

"Artabanos, at the first I was not discreet, when I spoke to thee foolish words on account of thy good counsel; but after no long time I changed my mind and perceived that I ought to do these things which thou didst suggest to me. I am not able however to do them, although I desire it; for indeed, now that I have turned about and changed my mind, a dream appears haunting me and by no means approving that I should do so; and just now it has left me even with a threat. If therefore it is God who sends it to me, and it is his absolute will and pleasure that an army should go against Hellas, this same dream will fly to thee also, laying upon thee a charge such as it has laid upon me; and it occurs to my mind that this might happen thus, namely if thou shouldst take all my attire and put it on, and then seat thyself on my throne, and after that lie down to sleep in my bed." " Having thus said Artabanos, expecting that he would prove that Xerxes was speaking folly, did that which was commanded him; and having put on the garments of Xerxes and seated himself in the royal throne, he afterwards went to bed: and when he had fallen asleep, the same dream came to him which used to come to Xerxes, and standing over Artabanos spoke these words: "Art thou indeed he who endeavours to dissuade Xerxes from making a march against Hellas, pretending to have a care of him? However, neither in the future nor now at the present shalt thou escape unpunished for trying to turn away that which is destined to come to pass: and as for Xerxes, that which he must suffer if he disobeys, hath been shown already to the man himself." - (Herodotus, Histories Book 7, chapters 15 & 17)

Plutarch(d. 150 AD) in chapters 73-74 of his work Lives, describes that Alexander the Great was recommended to perform the ritual by "Chaldean diviners" in an attempt to ward off the illness which would later kill him. Suetonius (d. 150 AD), in book 5 chapter 29 of his work The Twelve Caesars, describes an event in the time of Claudius where the empress Messalina was made to marry her lover Gaius Silius whereafter they both were executed. Suetonius states that the marriage contract was signed by Claudius himself in order to  "avert and turn upon another the danger which was inferred from certain portents to threaten the emperor himself." It has been suggested by scholar C. J. Gadd that this could be a muddled account of a sort of substitute king ritual with Silius fulfilling the role of substitute. In his work The History of Persia, John Malcolm describes a substitute king ritual being performed in the year 1591 AD  by the Safavid Shah Abbas the Great:

"While engaged in preventing the inroads of the Usbegs...[Abbas the Great] was suddenly called from all considerations of foreign or domestic policy, by a prediction of his astrologers; who, from the aspect of the heavenly bodies, had discovered that a most serious danger impended over the sovereign of Persia. Abbas was not exempt from the superstition of the age in which he lived, and he did not hesitate to adopt the strange expedient by which his counsellors proposed to avert the dreaded omen. He abdicated the throne; and a person of the name of Yusoofee, whom Persian authors take care to tell us was an unbeliever (probably a Christian), was crowned; and for three days, if we are to believe these historians [Zubd-ul-Tuarikh] he enjoyed not only the name and state, but the power of the king. The cruel farce ended as was to be expected. Yusoofee was put to death; the decree of the stars was fulfilled by this sacrifice; and Abbas, who reascended his throne in a most propitious hour, was promised by his astrologers a long and glorious reign. -(John Malcolm, The History of Persia, p. 346)

A tale from the One Thousand and One Nights, The story of Abu-l-Hasan, or the Sleeper Awakened, though concluding happily, is very likely inspired by the substitute king ritual. References 

Assyrian culture
State ritual and ceremonies
Human sacrifice