Donald Trump survived another apparent assassination attempt Sunday when Secret Service agents opened fire on a gunman at his golf club in West Palm Beach, Fla., where the former president was golfing just a few holes away. Trump was unhurt and the gunman fled but was arrested shortly later.

A police officer gestures as they investigate reports of shots fired outside Republican presidential nominee and former U.S. President Donald Trump’s Trump International Golf Course in West Palm Beach, Florida, U.S. September 15, 2024. REUTERS/Marco Bello

The agents, who were a few holes ahead of Trump on the course, fired when they spotted the man in some bushes pointing a rifle through a fence, law-enforcement officials said. The man then took off in a vehicle. An AK-style firearm was recovered at the scene.

The FBI said it was investigating “what appears to be an attempted assassination of former President Trump.”

A Trump campaign spokesman confirmed the former president is safe but provided no other details.

The Trump International Golf Club West Palm Beach is about 5 miles from the former’s president Mar-a-Lago residence.

The Palm Beach shooting comes amid a tense moment for the Secret Service, which is facing questions over how it failed to prevent a July 13 assassination attempt against Trump at a Pennsylvania rally where the former president was shot and wounded in the ear.

Law enforcement officers stand after reports of shots fired outside Republican presidential nominee and former U.S. President Donald Trump’s Trump International Golf Course in West Palm Beach, Florida, U.S. September 15, 2024. REUTERS/Marco Bello

In that case, law enforcement failed to stop a 20-year-old gunman, Thomas Matthew Crooks , from opening fire from a rooftop about 400 feet away from where Trump spoke, killing a spectator and injuring two others. Secret Service agents shot back, killing Crooks.

Law-enforcement officials have warned that threats of politically motivated violence, already elevated, have spiked online since the assassination attempt, expressing concern about the possibility of copycats.

The sheriff’s office said it would provide more details at a press conference later in the afternoon. A White House official said President Biden and Vice President Kamala Harris were briefed about the shooting.

“I am glad he is safe. Violence has no place in America,” Harris said in a post on X.

The Secret Service has long viewed golf as a vulnerability, with courses offering a precarious combination of open ground and the predictability of a protectee proceeding from hole to hole. But that risk has been pronounced with Trump, current and former agents said, because of the frequency of his trips and his habit of golfing at courses he owns.

Agents walk ahead of and behind the former president when he is golfing; they were checking for threats a few holes away from Trump when they noticed the gunman, the officials said.

In a fundraising campaign email sent shortly after the incident, Trump said, “ I AM SAFE AND WELL!”

He added “Nothing will slow me down. I WILL NEVER SURRENDER!”

Write to Sadie Gurman at sadie.gurman@wsj.com , Jacob Gershman at jacob.gershman@wsj.com and Vivian Salama at vivian.salama@wsj.com