Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
baxiry opened this issue Jan 28, 2022 · 6 comments
Open

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

baxiry opened this issue Jan 28, 2022 · 6 comments

Comments

@baxiry
Copy link

baxiry commented Jan 28, 2022

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 baxiry changed the title How can I escape the ***client.Run(cmd)*** loop? How can I escape client.Run(cmd) loop? Jan 29, 2022
@melbahja
Copy link
Owner

Hi,

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

@baxiry
Copy link
Author

baxiry commented Feb 5, 2022

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.

@george-zakharov
Copy link
Contributor

george-zakharov commented Jul 18, 2022

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?

@melbahja
Copy link
Owner

I think that nohup it's gonna help you.

@george-zakharov
Copy link
Contributor

george-zakharov commented Jul 20, 2022

I think that nohup it's gonna help you.

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

@george-zakharov
Copy link
Contributor

george-zakharov commented Jul 26, 2022

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 &")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants