简体   繁体   English

如何在scp期间禁用CTRL-C?

[英]How to disable CTRL-C during scp?

I want my script to be able to finish an scp , even if CTRL + C is entered. 我希望我的脚本能够完成一个scp ,即使输入了CTRL + C. I have tried using a trap to disable CTRL + C , but it does not work when the scp is in progress. 我已经尝试使用陷阱来禁用CTRL + C ,但是当scp正在进行时它不起作用。 The scp terminates immediately. scp立即终止。 Is there any way to do this? 有没有办法做到这一点? Code is below. 代码如下。 Pretty simple. 很简单。

#!/bin/bash
trap '' SIGINT SIGTERM
scp -q user@server:/backup/large_file.txt /local/dir/

UPDATE: Also make sure you have "set -m" at the top of your script. 更新:还要确保脚本顶部有“set -m”。

Put it in the background in a subshell to disconnect it from the terminal. 将它放在子shell中的后台,以断开它与终端的连接。

(scp ... &) (scp ...&)

EDIT: You'll probably want to redirect any errors to a file. 编辑:您可能希望将任何错误重定向到文件。

Another method is to disable the key interrupt completely until the transfer is done: 另一种方法是完全禁用密钥中断,直到传输完成:

#!/bin/bash
STTY=$(stty -g)                        # Save settings
stty intr undef                        # Disable interrupt
echo -n "Press ENTER to continue: "    # Do your file transfer here
read response
stty ${STTY}                           # Restore settings

If you want to send it to background and be able to get the process in foreground again use this: 如果您想将其发送到后台并且能够再次在前台获取该过程,请使用以下命令:

$ ( sleep 10 ; echo hello ) &
[1] 1323
$ jobs
[1]  + running    ( sleep 10; echo hello; )
$ kill %1
$
[1]  + 1323 terminated  ( sleep 10; echo hello; )
$ $
$ ( sleep 10 ; echo hello ) &
[1] 1325
$ jobs
[1]  + running    ( sleep 10; echo hello; )
$ fg %1
[1]  + 1325 running    ( sleep 10; echo hello; )
^C
$

So you can send the progress in the background with & and see all background tasks with jobs and you can access it with %1 and kill it with kill and get it to the foreground with fg . 因此,您可以使用&发送后台进度并查看jobs所有后台任务,您可以使用%1访问它并使用kill将其终止并使用fg将其置于前台。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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