Senate passage was a hard-earned but partial victory for President Barack Obama and his Democratic allies.

It caps months of effort by governors of both parties, public employee unions and others seeking to extend programs enacted in last year’s economic stimulus law. That measure provided budget relief to states and local school districts hurt by slumping tax revenues as the U.S. economy has struggled to emerge from recession.

The $26 billion measure would provide $16 billion to help states fund their Medicaid budgets — and therefore free money for other budget priorities — and $10 billion for grants to school districts to forestall layoffs as schools across the country prepare to open.

“This is a huge crisis for us financially,” said Carlos Garcia, superintendent of San Francisco’s schools. “This would be a welcome relief for us so maybe we could hire back as many people as possible. Because if we really want to get out of this recession, people need to be working.”

Garcia said the federal money could help the district rehire some of the roughly 300 employees it laid off this year or reinstate some of the four instructional days cut from the coming school year.

Advocates said the measure would stop the layoffs of perhaps 300,000 teachers, firefighters, police officers, and other public employees. Though scaled back, the bill also would salvage a victory for Democrats who have been unable to deliver most of the jobs help that they and Obama had planned.

“This is about saving jobs that are in immediate danger,” said Sen. Patty Murray, D-Wash. “It will allow us to avoid layoffs, service cuts, or tax increases and it will make sure our children don’t walk through the schoolhouse doors this September to larger class sizes and fewer subjects.”

In New York, approximately $600 million in new education money would help schools avert more than 7,000 layoffs, according to the U.S. Department of education. But to save those jobs, the state will have to allow school districts to reopen their budgets and teachers will have to be rehired quickly. That’s a tight deadline even with jobs at stake.

The state Legislature will need to convene soon in special session because a high court has ruled federal funds must be appropriated by lawmakers, according to the state Education Department.

In Illinois, said Democratic Sen. Dick Durbin, the state had been facing as many as 17,000 teacher layoffs, including 2,700 in Chicago alone. Thursday’s legislation would save almost 5,000 of those jobs.

“I wish it were more, but it is going to help,” Durbin said.

House Speaker Nancy Pelosi, D-Calif., said she would call the House back into session next week to approve the measure to speed it to Obama.

The measure comes on the heels of successful efforts to extend unemployment insurance for the long-term jobless and to provide a payroll tax credit this year to businesses that hire the unemployed.

But the total jobs package has been significantly trimmed from earlier, ambitious designs to boost “green jobs,” provide new funding for roads, bridges and other infrastructure projects, pay for a summer jobs program for disadvantaged young people and renew health insurance subsidies for the jobless.

The measure was financed through cutting other programs and raising taxes on some U.S.-based multinational companies.

Among the ways to pay chosen by Senate Majority Leader Harry Reid, D-Nev., was a $12 billion cut to food stamps that would cost a family of four $59 a month beginning in early 2014.

Associated Press writer Terry Chea in San Francisco and Michael Gormley in Albany, N.Y. contributed to this report.