In Linux when a console session is closed most background jobs (^Z and bg %n) will stop running when the parent ( the ssh session ) is closed because the parent sends a SIGHUP to all its children when closing (properly). Some programs can catch and ignore the SIGHUP or not handle it at all hence passing to the root init parent. The disown command in a shell removes a background job from the list to send SIGHUPs to.
In ESXi there is no disown command. However there is a way to close a shell immediately without issuing the SIGHUPs :
exec </dev/null >/dev/null 2>/dev/null
The exec command will run a command and switch it out for the current shell. Also this command will make sure the stdio and stderr are piped properly.
nJoy 😉
Hello,
I came across your page while researching a similar issue I am having. I need to SSH into ESXi, execute a command, exit SSH session. All while not killing the process associated with the executed command. I am trying to figure out how to use the above example in your article. Is this the way you can exit the SSH session without the process being terminated as well? I have tried using it, but am getting the same results. Any help would be appreciated.
Sorry it was not clear..
The idea is to run the commands in the job list, say
Then when you need to log off the session you run the
When leaving the session the SIGHUPs to child processes will (or should , ESX is bound to change userspace without notice) NOT be sent.
Hope this clarifies a bit..
The piped output is lost as this is but you could replace the >/dev/null 2>/dev/null to point to log files instead of discarding. Ijust hate doing that on ESX as it is not clear what the effects are if the storage location / path where you point the pipes is not writable or unavailable.
😉
After a lot of google research, I stumbled across this https://www.tecmint.com/keep-remote-ssh-sessions-running-after-disconnection/. ESXi does not have screen, tmux, or disown. An nohup was not working…I gave setsid a shot, and it worked.
setsid /etc/run_command &
Awesome hint, thanks! Did not know about setsid.
Thanks for posting up the whole article, but also to Thehack904! The nohup didn’t work for me when using JuiceSSH (Android App) to remote execute controlled shutdown commands. ‘setsid’ worked like a charm!