If he had, it would almost certainly have shut down the government at midnight, just as hundreds of thousands of teenagers and adults were scheduled to descend on Washington for a gun control march on Saturday. With Congress on spring recess for two weeks starting Monday, many lawmakers had already departed Washington and some were on their way out of the country as part of official congressional delegations.

The spending measure cleared Congress early Friday morning and, while Mr. Trump had made plain he was unhappy with some aspects of it, his senior advisers spent Thursday telling reporters that he would sign it. Then early Friday, on a morning when he watched a fierce backlash to the measure play out on Fox News, Mr. Trump seemed to hesitate, tweeting angrily about the lack of wall funding.

“I am considering a VETO of the Omnibus Spending Bill based on the fact that the 800,000 plus DACA recipients have been totally abandoned by the Democrats (not even mentioned in Bill) and the BORDER WALL, which is desperately needed for our National Defense, is not fully funded,” Mr. Trump wrote.

He was referring partly to the fact that he failed to reach a deal with Democrats to include provisions in the spending measure that would preserve Deferred Action for Childhood Arrivals, or DACA, an Obama-era program that Mr. Trump rescinded last fall. The program allows undocumented immigrants brought to the United States as children to apply for permits to work legally and avoid deportation.

But the president was most angry about the lack of funding in the bill for an enormous wall across the nation’s southern border that he has billed as the centerpiece of his crackdown on illegal immigration. The measure includes nearly $1.6 billion for border security, including new technology and repairs to existing barriers — but not Mr. Trump’s wall, as he claimed on Twitter on Wednesday.

It provides $641 million for about 33 miles of new fencing, but prohibits building a concrete structure or other prototypes the president has considered.

Mr. Trump’s morning tweet set off a scramble on Capitol Hill and at the White House. Speaker Paul D. Ryan, Republican of Wisconsin, phoned Mr. Trump and encouraged him to sign the measure, according to a source familiar with the call, citing all the “wins” contained in the bill, especially for the military.