Richard Gregg was a pretty good sleeper, but in the summer of 2011, he was waking up in the middle of the night and staying up.

A career civil servant at the Treasury Department who helps oversee the tens of billions of dollars that flow out of and into federal coffers, he was facing down a scary possibility: The government could soon run out of money.

Then there was the missing 57 cents. As he wondered whether Congress would hike the debt ceiling in time to avoid missed payments, Gregg noticed a debate among government bean counters about the missing money. He relayed it to the then-Treasury secretary, Timothy Geithner, who for a moment thought he was saying that Treasury was a little more than two quarters and a nickel short of missing payments.

“No, no, we’re just reconciling 57 cents,” Gregg, who turns 70 on Sunday, recalled replying. It was one of the more surreal moments in Gregg’s 41-year career at Treasury. On Friday, he retires for a second time, after being called back into action by Geithner in 2009.

Gregg has the dry title of fiscal assistant secretary, but he has played an important role in shaping how government works since he arrived at Treasury during the Nixon administration. He has since served under 16 Treasury secretaries, starting with David M. Kennedy and ending with Jack Lew under President Obama.

“Dick’s dedication to the calling of public service was never more apparent than when he came out of retirement in 2009, at a critical time for our nation’s economy, to assume the position of Fiscal Assistant Secretary,” Jack Lew said in an e-mail. “His leadership, depth of knowledge, and tireless efforts toward a more efficient, less costly government are a model for those who serve in federal government.”

In addition to managing federal cash flow during two debt ceiling crises, Gregg also played a central role in pushing the government to make nearly all of its benefits payments electronically, saving what the department estimates is $1 billion over a decade. He helped clean up federal debt collection, which was struggling under sharp criticism in the 1990s, and he helped modernize how active duty military personnel are paid.

From his first-floor Treasury office, Gregg also has been working to design the “myRA” retirement savings accounts program that President Obama outlined as a new policy in this year’s State of the Union address.

“The pace is so much faster than when I started,” Gregg said in an interview. “The number and complexity of the issues have grown exponentially.” He said he’s leaving partly because he wants to give his successors time to master the ropes before the arrival of a new administration, which always brings with it considerable turnover.

Gregg grew up in South Dakota. A then-newlywed with a graduate degree in public administration, he came to Treasury in 1970 — mainly to make a buck. In his early years, he worked in the office that raises funds for the government through bond auctions.

Compared with today, when an auction takes three minutes, it felt like the Stone Age. In New York, bankers would show up and put their bids in a box. Then an official would call Treasury in Washington with the bids. The process took three hours.

In 1997, Gregg helped create a new way for military personnel deployed overseas, especially in conflict zones, to be paid. Historically, soldiers had to stand in long lines on payday to collect their checks. Now, they received “stored value cards,” and saw their salaries automatically added to their card values.

By 2006, Gregg was ready to retire to his home in Springfield, in which he had lived in since the mid-1970s, when “blue laws” prohibited restaurants from serving alcohol. He and his wife Conie have two grown sons — Ramsey and Matt, living in California — and four grandchildren.

Retired, he enjoyed writing his memoirs, reading and traveling, but still had an itch to return to work.

“My wife says she was happy I came back to work because I had rearranged her spice racks three times,” he said. Then, when an illness struck the fiscal assistant secretary, Geithner called.

Gregg returned to face two of the biggest tasks of his career. First, he embarked on a big push to convert more government payments to electronic. He had seen the trouble in getting money to victims of Hurricane Katrina, when one deputy had to ride from Austin to Houston every day to distribute checks. And still many other seniors receiving Social Security checks had to turn to expensive check cashers to get their money.

Today, 97.7 percent of federal benefits payments are electronic, up from 85.2 percent in 2010.

Then, he faced the debt ceiling battles, a “very challenging and very stressful” period.

As he has wrapped up his tenure, he has worked toward making sure more agencies share more services and reduce duplication.

“We think we have an opportunity to transform the financial management of the government,” said Gregg, who has already gone back to sleeping through the night.