Keep processes open within zellij session after disconnecting from SSH ?
Hello,
I wanted to know if thats a zellij bug or thats something I don't understand, here what I did :
- I connect to my server using SSH
- I attach my existing zellij session using : zellij a "main"
- I start my process using ./server.sh or something like that
- I detach the zellij session using CTRL+D
- I disconnect from my server using "exit"
But : When I log back, the process has stopped automatically and zellij asks me to re run it
This is before I detach the session (the process I want to keep open)
This is after I reconnect and re-attach the zellij session, the process has stopped ...
Is it normal ? Is there a way to keep that process open within zellij even after I disconnect from SSH, and to have it back when I re-attach the session?
Thanks for your help.
It's very likely that something on the server is killing the detached Zellij process (eg. systemd is notorious for doing this to processes that are not owned by any connected user). I'd try looking in that direction.
@romaric-siri I'm guessing your server is running systemd? By default, systemd kills all user processes on logout. This is a departure from the old initrc setups. It's nothing to do with zellij per se.
Why the tmux can hold on to its process on the same Ubuntu?
I often use tmux as nohup to run some job to background. But zellij can't work to this case.
That's what i was wondering, I have no problem when running Screen for similar use case ...
I'm wondering if there's any news with this as I have the same problem and it happens even when running locally. Whenever i resurrect a session my progress isn't actually saved since the process is not persisted as zellij just runs it again from the top.
@chubi-x @wllenyj @romaric-siri I have written some documentation to help: https://github.com/adaschma/zellij-org.github.io/blob/systemd-integration/docs/src/integration.md#keep-session-running-after-logout-on-systemd
Please tell me, if it is clear and works for you or any suggested changes. I will then send a pull request to make it part of the official documentation.
It has now been a week and I haven't heard from anybody. @chubi-x @wllenyj @romaric-siri did you have a chance to read it? Or do you still plan on doing so?
Sorry for the late reply @adaschma. The suggested command didn't work for me on linux ubuntu 24.04. I had to use systemd-run instead of systemd-bin. Also i'm running zellij as the default shell for alacritty so I don't know how the suggested command will integrate with that since zellij is started by alacritty.
Thanks @chubi-x for catching that. Don't know how I missed that. Fixed it. In alacritty.toml you can add something like:
[shell] program = "systemd-run" args = ["--scope", "--user", "zellij"]
Sorry for late @adaschma, now I can't reproduce the problem anymore. I upgraded zell to 0.41.1
Thanks @adaschma, your solution worked. I was running into this problem lately since I transferred my workspace into a remote machine and I keep getting prompted to run the previous command every time I disconnect via SSH.
To anyone who put their zellij attach command in their .bashrc or .bash_profile (in case you are not using alacritty as above), what I did was swap out the command as below:
# before
zellij attach --create <default session name>
#after
systemd-run --scope --user zellij attach --create <default session name>
EDIT: Tested this more but this only works when you detach from the zellij session properly. If the SSH session get disconnected while still attached to zellij, when you reconnect it would still stop the process
Somebody might find this useful: https://openforums.wordpress.com/2016/05/31/systemd-defaults-killuserprocesses-to-yes-in-logind-conf-with-v230/
systemd-logind will now by default terminate user processes that are part of the user session scope unit (session-XX.scope) when the user logs out. This behaviour is controlled by the KillUserProcesses=yes|no setting in logind.conf, and previous default of "no" is now changed to "yes". This means that user sessions will be properly cleaned up after, but additional steps are necessary to allow intentionally long-running processes to survive logout.
While the user is logged in at least once, [email protected] is running, and any service that should survive the end of any individual login session can be started at a user service or scope using systemd-run. systemd-run(1) man page has been extended with an example which shows how to run screen in a scope unit underneath [email protected]. The same command works for tmux.
After the user logs out of all sessions, [email protected] will be terminated too, by default, unless the user has "lingering" enabled. To effectively allow users to run long-term tasks even if they are logged out, lingering must be enabled for them. See loginctl(1) for details.