Karen Elaine Smith, 53. (left) was as one of the people shot by Cedric Anderson (right), identified as her estranged husband, as she taught a special education class at North Park Elementary School in San Bernardino, Calif., Monday, April 10, 2017. (San Bernardino Police Department via AP)

SAN BERNARDINO, Calif. (AP) — A man walked into his estranged wife’s elementary school classroom in San Bernardino and opened fire without saying a word, killing her and an 8-year-old student before shooting himself in a murder-suicide that spread panic across a city still recovering emotionally from a terror attack just 15 months ago.

A 9-year-old student also was critically wounded. He and the boy who died were behind their special-education teacher, Karen Elaine Smith, 53, the target of the man she had married months earlier, police said.

The shooting left hundreds of distraught parents waiting for hours to reunite with their children.

Staffers knew Cedric Anderson, who had been estranged from his wife for about a month, and he got into the school by saying he had to drop something off for Smith, officials said.

Anderson had a history of weapons, domestic violence and possible drug charges that predated the short marriage, authorities said.

He frequently wrote social media posts about his wife over the past month. On what appeared to be his Facebook page, Anderson said he “loved being married to Karen Smith-Anderson!” and posted a photo of the two of them on March 4 in what he described as a date night. He posted several photos of his wedding to Smith early this year and their honeymoon in Sedona, Arizona.

Smith’s mother, Irma Sykes, said her daughter had been friends with Anderson for about four years before they got married, but separated from him after about a month because she saw “the real Cedric.” She did not elaborate. Sykes told the Los Angeles Times her daughter was a mother of four who had been a teacher for 10 years.

Fifteen students ranging from first to fourth grade were in the special-education classroom at North Park Elementary School, along with two adult aides and Smith, when Anderson emptied a large-caliber revolver and reloaded. Then, he turned the gun on himself.

Marissa Perez, age 9, was in the classroom hiding under a table.

“A boy just walked in with a gun,” she said shortly after she and her mother, Elizabeth Barajas, were reunited. The two cried, hugged and trembled. Barajas held the sweater her daughter had been wearing. It was speckled with blood.

Jonathan Martinez, the 8-year-old, was airlifted to a hospital and died soon after arriving, Burguan said. The 9-year-old boy, whose name was not released, has been stabilized at a hospital.

The 600 other students at the school were bused to safety at California State University’s San Bernardino campus, several miles away, after many walked off campus hand-in-hand, escorted by police.

Panicked parents had to wait hours before being reunited with them at a nearby high school.

Holly Penalber, whose 9-year-old son and 7-year-old daughter attend the school, called it “every parent’s worst nightmare.” She said the long wait was “frustrating but also understandable.”

When the buses first pulled away from the elementary school, some parents ran alongside, waving and trying to recognize their children inside. Many said their children were too young to have cellphones. Others said the phones rang unanswered.

When students got to the high school, many carrying glow sticks they had been given to pass the time, they got hugs from emotional parents, many in tears. Police officers applauded and high-fived them.

Alberta Terrell said she cried with relief when she was told that a family friend saw her 9-year-old granddaughter getting safely onto a bus.

“I was really elated. But I won’t be truly happy until I see her and can give her a big hug,” Terrell said as she sat in the bleachers near Cajon High School’s baseball diamond, waiting for her granddaughter to arrive.

San Bernardino, a city of 216,000 people about 60 miles east of Los Angeles, was the site of a December 2015 terror attack that killed 14 people and wounded 22 others at a meeting of San Bernardino County employees. Husband-and-wife shooters Syed Rizwan Farook and Tashfeen Malik were later killed in a gunbattle with authorities.

“Tragedy has again befallen our city,” Police Lt. Mike Madden said Monday of a community that has struggled in recent years.

Once a major rail hub and citrus producer, San Bernardino filed for bankruptcy in 2012 after struggling to pay its employees despite steep cuts to the budget.

An outlying suburb of Los Angeles, it was hit hard when the Great Recession sent housing prices tumbling. As the city struggled with economic problems that forced layoffs of police and other government workers, violent crime, particularly homicides, began to rise.

In the past year, however, the city seemed to be making a recovery. Burguan, who won national praise for the way his department responded to the 2015 attack, announced last year he was hiring additional officers.