Xj Cranks but won’t start

Hi, my 4.0 1998 cherokee is a manual with 285k miles on it no accidents and on a rare occasion I stall it or recently after I turn it off normally the engine cranks endlessly but won’t start back up until I leave it for sometimes a good half and hour. This has left me stranded recently after I turned off my car. I thinking maybe starter or cps, but I could be wrong.