atuinsh / atuin

✨ Magical shell history
https://atuin.sh
MIT License
19.15k stars 527 forks source link

Trigger sync on daemon stop #2230

Open Programie opened 2 weeks ago

Programie commented 2 weeks ago

I'm using the daemon in a socket activated systemd user service to periodically sync the history even if I close my terminal after executing a few commands.

I'm also using atuin on my servers where the systemd user service is also running. Unfortunately, the daemon doesn't seem to sync on stop (i.e. on SSH disconnection when the systemd user service gets stopped) resulting in not including the recent commands in my history on the other hosts.

Right now, I'm using a small workaround in my .zshrc on all my hosts to trigger a manual sync on logout:

function shellexit() {
    atuin sync
}
trap shellexit EXIT

But I could get rid of that if the daemon would just sync once it gets stopped.

ellie commented 2 weeks ago

Hey!

I'd stick with the workaround tbh - the daemon is designed to run persistently, and not just while a session is in place.

I'd also prefer that the daemon does not try and do more work once it's been asked to exit, and instead exits immediately.

Is there any reason you're not happy to just leave the daemon running?

Programie commented 2 weeks ago

The daemon is designed to run persistently, and not just while a session is in place.

In a system using systemd, that could be done using a system-wide systemd service (i.e. not a systemd user service). But that won't work in case of multiple users on the same system. It's especially not suitable for systems where multiple users should have different Atuin accounts (not my case, but probably others also having the same issue). A single Atuin daemon running as systemd service would also just use a single Atuin account.

Is there any reason you're not happy to just leave the daemon running?

That's just by design of systemd user services. Those services are deactivated once the last session of the user is closed. In case of an SSH connection to a server, the session ends with the SSH connection being closed. And if that's the only SSH connection to the server, the systemd service gets stopped.

But as already mentioned, this results in Atuin not being able to sync the recent history entries added since the last sync.

Example:

The commands executed in the 3rd step of my example won't be synced until I'm connecting again.