简体   繁体   中英

Terminate a forked process (forwarding a port)

I installed an Oracle thin client on my local machine to execute SQL commands on a remote host. Also, I have a Ruby script that contains SQL commands (using the OCI8 gem) and the script is working correctly.

However, to run the SQL commands I need to open a tunnel, forwarding to the port the remote Oracle is listening to:

ssh -L 1521:localhost:1521 <user>@<host>

Running this command logs me into the remote host, which I do not want.

I found the following command to run which creates a tunnel, forwarding the specific port, and I can run the Ruby script, containing the SQL commands, successfully.

tunnel = fork do
    exec "ssh -f <user>@<host> -L 1521:localhost:1521 -N"
end

So, the tunnel is created in the background, however, I also need to terminate the tunnel, disconnect from the remote instance. I tried the following:

 Process.kill('HUP', tunnel)

However, the tunnel is not killed.

Is there a way that I can kill/terminate the tunnel from within the Ruby script?

A HUP signal doesn't kill a process, that just sends it a signal and that process is free to ignore it. HUP is often used to signal that a configuration file should be reloaded.

What you want is:

Process.kill('INT', tunnel)

A more aggressive version for situations where asking politely doesn't work:

Process.kill('KILL', tunnel)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM