as he needed reference works that were not available at his far outpost. He used the occasion of Abul Abbas’s conquest of Tunisia to go to Tunis. This was the first time he had returned to the town of his birth since leaving it more than twenty-seven years previously.
There were political forces at work against him once more and this time, before he fell out of favor, he used a convenient occasion (in 1382 A.C.E / A.H.) to leave North Africa behind, never to return. 
Ibn Khaldun was granted permission from Sultan Abul Abbas to go on hajj. He arrived in Alexandria in October 1382 A.C.E. (Shabaan 784 A.H.), at the ripe age of fifty. He spent a month preparing to leave for hajj, but was unable to join the caravan bound for the Holy Lands. He turned toward Cairo instead. Here he was warmly welcomed by scholars and students, and it was in Cairo that he lived out his final days. His fame for his writings had already preceded him. He lectured at al-Azhar and other fine schools. He had the chance to meet with Sultan al-Zahir Barquq who appointed him to a teaching post at the Kamhiah school. 
He again enjoyed the favors of the sultan. He was appointed a Maliki Judge at the sultan’s whim, and anger. He fared well and tried to fight corruption and favoritism, but again, conspiracies worked against him and he was relieved of duty, just in time to coincide with his family’s disaster. The ship carrying his family and belongings sank in a storm. 
Ibn Khaldun again took permission to go on hajj to the Holy Lands. He returned and was well received, and appointed to a teaching position in the newly built school, Bein al-Qasrein. He lectured in hadith, particularly Imam Malik’s Muwatta. He was then appointed to the Sufi Institute of Beibers with a generous salary. But soon, the state of affairs of Egypt was disturbed, as a rival of Sultan Barquq, Yulbugha, organized a successful revolt. Sultan Barquq staged a counter-revolt and was restored to his former throne. During this period, Ibn Khaldun suffered and then had his position restored with the return to power of the victorious Sultan Barquq.
During this period, Ibn Khaldun devoted his time to lecturing and studying, as well as to completing his Universal History. After Yulbugha’s revolt, he wrote about asabiyah and its role in the rise and fall of states. He applied his theory to the Egyptian theater since the time of Salah ad-Din. 
Fourteen years after leaving the position of Chief Maliki Judge, Ibn Khaldun was reassigned to the post upon the death of the presiding judge. The state again fell into disarray upon Sultan Barquq’s death and his son’s ascension. Ibn Khaldun was not a party to these revolts and asked permission to visit Jerusalem. He joined the Sultan Faraj’s caravan on its way back from Damascus, and was relieved of his duties as judge for the second time, again due to political intrigue. However, this did not matter because he was called upon to accompany the sultan on a perilous journey with fate to Damascus. 
During Ibn Khaldun’s stay in Egypt he was asked by Sultan Faraj of Egypt to accompany him on his expedition to Damascus. News reports had confirmed the movement of Tamerlane’s war party toward Damascus. Sultan Faraj and his army were on their way to Damascus, and it seems that Ibn Khaldun was asked, firmly, to accompany the sultan to Damascus. 
The sultan stayed in Damascus just two weeks, as he had to leave because of rumors that a revolt was in the works back in Cairo. Ibn Khaldun and several notables were left behind in Damascus. It was up to the leaders of Damascus to deal with Tamerlane. Ibn Khaldun had suggested that they consider Tamerlane’s terms. It was the task of another judge, Ibn Muflih, to discuss the terms with Tamerlane. When Ibn Muflih returned from Tamerlane’s camp, the terms were not agreeable to the residents of Damascus.
Since it was Ibn Khaldun’s suggestion to come to terms with Tamerlane, Ibn Khaldun felt obliged to meet with Tamerlane personally, and so he left Damascus and went to Tamerlane’s camp. It is not clear whether he went on his own o