goph icon indicating copy to clipboard operation
goph copied to clipboard

How can I escape ```client.Run(cmd)``` loop?

Open baxiry opened this issue 3 years ago • 6 comments

Question: How can I escape the client.Run(cmd) loop? When I call client.Run(cmd) or cmd.Execute(myCmd) my program can't go to the next tasks. I am trying to run the command in the background of the remote host. then Close connection and allow the app to do other things.

defer sshClient.Close()         // The app cannot close this 
 _, _:= sshClient.Run(myCmd + "&")     // Infinite loop.
            
someEsleTasks()                // The app cannot execute this line 

Currently I'm skipping Runc(cmd) and jumping to the next code via goroutine. like this

       go func() {
           _, _:= sshClient.Run(myCmd + "&")     // Infinite loop.

           sshClient.Close()             // The app cannot execute this line 
       }()

I'm looking for a logical solution to my problem. thank you so much

baxiry avatar Jan 28 '22 13:01 baxiry

Hi,

go routines is a good way to do it, without "&" at the end of cmd.

melbahja avatar Jan 30 '22 02:01 melbahja

Yes. This is a temporary solution. The remote application will remain running as long as my application is running. When my app stops the remote app will stop. I'm still looking for a good solution to the problem. Another solution is to use SystemD It is a good solution. But I avoid it for technical reasons.

baxiry avatar Feb 05 '22 19:02 baxiry

Yes. This is a temporary solution. The remote application will remain running as long as my application is running. When my app stops the remote app will stop. I'm still looking for a good solution to the problem. Another solution is to use SystemD It is a good solution. But I avoid it for technical reasons.

Hi! Have you found any good solution of this problem?

george-zakharov avatar Jul 18 '22 15:07 george-zakharov

I think that nohup it's gonna help you.

melbahja avatar Jul 18 '22 21:07 melbahja

I think that nohup it's gonna help you.

Thank you for this idea. I'll check it on my case.

george-zakharov avatar Jul 20 '22 02:07 george-zakharov

I think that nohup it's gonna help you.

I've checked. Not working in my case. App still can't go to the next step after Run() method. Looking for another solution. If you have some more advices, I will be pleased to check them :)

Update. What really helped me is running with redirect to dev/null: test, err := client.Run("some_cmd > /dev/null 2>&1 &")

george-zakharov avatar Jul 26 '22 16:07 george-zakharov