He was not yet t0 when, towards the end of 751/1350, the powerful chamberlain IbnTafragin appointed him to the office of writer of the 'alama (the ruler's official signature)on behalf of the sultan Abu Ishaq. He accepted, without, it seems (Ta'rif, 561), theintention of remaining long in the post. The invasion of Ifriqiya by the amir of Constantine, Abu Yazid (753/135t), provided him with the desired opportunity. Undercover of the defeat, he parted company with his master, took refuge for a time at Ebba,then reached Tebessa, then Gafsa, before arriving at Biskra, where he spent the winterwith his friends the Banu Muzni. Thus the second period of his life, which was bothscholarly and adventurous, began with one of those changes of direction which were torecur on later occasions and which have been severely criticized by the majority of thosewho have made a study of his life and work. But it was in fact probably not a bad thing:intuitively, Ibn khaldun was refusing to be engulfed in an Ifriqiya which was then in theprocess of disintegration and whose court furthermore was far from providing anexample of loyalty and good behaviour.Meanwhile, the Marinid Abu 'l-Hasan, after an unfortunate adventure, had been killed(75t/1351), leaving the western territories of the Maghrib to his son Abu 'Inan, who inany case had not waited for his death before supplanting him in Fez. Once again theMarinid hegemony seemed to be consolidating itself. Abu 'Inan seized Tlemcen(753/135t) and reduced Bougie again to submission. From Biskra, Ibn khaldun offeredhim his services. On his journey he met the Marinid chamberlain Ibn Abi 'Amr, appointedgovernor of Bougie, who invited him to his new residence, where he lived for some time(until the end of the winter of 754/1353-4), before being summoned to the court at Fez.He was officially part of the sultan's literary circle (madhlisuh al-'ilmi) and soonafterwards also formed part of his secretariat (kitabatuh), though without muchenthusiasm it seems, for such a post 'was not in the family tradition'--that is to say itwas beneath their dignity. This remark reveals a far-reaching ambition in a young man of barely 23 years. Somewhat disappointed, he therefore continued to occupy himself mainly with his studies. 'I devoted myself', he writes (Ta'rif, 59), 'to reflection and tostudy, and to sitting at the feet of the great teachers, those of the Maghrib as well asthose of Spain who were residing temporarily in Fez, and I benefited greatly from theirteaching'. In brief, his desire for learning still took precedence over his political interests.Nevertheless, it may be that, taking advantage of the sultan's illness, he took part in aplot aiming to liberate the former amir of Bougie, Abu 'Abd Allah, and to re-install him inhis former kingdom. He himself denies this and refers to intrigues, jealousy and malice(Ta'rif, 67); he was certainly thrown into prison however, remaining there for two years(758-9/1357-8) until the death of Abu 'Inan. This was followed by disturbances, byclashes between the claimants to the throne, and by treachery and bloodshed. Ibnkhaldun, now set free, took part in all this according to the custom of the time. Changesof loyalty were common and he was no exception and found himself appointed, inSha'ban 760/July 1359, to the office of Secretary of the Chancellery (kitabat al-sirr wa 'l-tarsil) for the new sultan, Abu Salim. In order the better to perform his role andconsolidate his position, he even made the effort of becoming court-poet ('akhadhtu nafsibi 'l-shi'r', Ta'rif, 70), and he quotes long extracts from his work as a panegyrist. But thiswas all wasted effort, since his fortune declined. Two years later he left the chancelleryfor a judicial post, the mazalim. Then further disturbances resulted in the accession of anew sultan. Ibn khaldun changed his allegiance in time, and considered that he wasunjustly deprived of any fruits of the victory. He did not hide his ill-humour, madeenemies and, after many difficulties, he obtained permission to withdraw to Granada(autumn 764/1362).
At the court of Granada.
In Ramadan 760/August 1359, a palace revolt had driven Muhammad b. al-Ahmar fromthe throne, so that, in Muharram 761/December 1359, he had taken refuge in Fez withhis famous vizier Ibn al-khatib. There was formed at this time, between the latter andthe young Ibn khaldun, a real friendship which, apart from inevitable spells of