For more than a week, the president has been publicly open about his view of the case against his longtime friend and adviser Roger J. Stone Jr. And the notion that Mr. Barr might leave his post over Mr. Trump’s commentary did not appear to quiet the president.

Mr. Stone was convicted in November of seven felonies for obstructing a congressional investigation into whether the Trump campaign had ties to Russia. At the time, Mr. Trump said his friend’s conviction was evidence of a double standard in the justice system.

Last week, a day after prosecutors filed a routine recommendation for Mr. Stone’s sentencing, Mr. Trump called it “horrible and very unfair.” Hours after that, Mr. Barr intervened to lower the sentencing recommendation, drawing public praise from the president while spurring fears that the Justice Department was bowing to White House influence.

This led four prosecutors to quit the Stone case, drawing a lashing from Mr. Trump who said they “cut and run” and were part of the special counsel team’s “investigation that was illegal.”

Amid outrage over the prosecutors’ departures, Mr. Barr took the extraordinary step of going on national television to send a message to the president: “It’s time to stop the tweeting about Department of Justice criminal cases.”