开发者

Bash Script: What does this mean? "done < /dev/null & disown"

开发者 https://www.devze.com 2023-01-30 11:20 出处:网络
Can you please explain exactly what the last line of this does, and why it is needed? while 开发者_开发百科true; do

Can you please explain exactly what the last line of this does, and why it is needed?

while 开发者_开发百科true; do
    /usr/bin/ssh -R 55555:localhost:22 -i ~/.ssh/tunnel-id user@server.com
    sleep 1
done < /dev/null & disown

That is the entire script, and it's purpose is to create an SSH tunnel to a relay server. I'm new to Bash, but it looks like it will continually try to keep the connection alive, but I don't understand the syntax of the last line.

This script is part of a process to use SSH behind a firewall, or in my case a NAT: http://martin.piware.de/ssh/index.html


The last line redirects /dev/null into the loop as input -- which immediately returns EOF -- and runs the process in the background. It then runs a command disown(1) in the foreground, which detaches the process, preventing HUP signals from stopping it (sort of like nohup does.). the effect is to make the loop into something like a daemon process.

The loop overall runs the ssh command once a second. The command is opening an ssh tunnel, connecting it locally to port 5555 and remotely to port 22 (ssh). If there is something there to connect, it does; otherwise the redirected EOF causes it to terminate. It then tries aagain a second later.

(Or so I believe, I haven't actually tried it.)

In bash, disown is a built-in; use help disown to see some details.


The redirection of /dev/null into the while loop effectively closes its stdin which should be equivalent to exec <&-.

0

精彩评论

暂无评论...
验证码 换一张
取 消