I want my script to be able to finish an scp
, even if CTRL+C is entered. I have tried using a trap to disable CTRL+C, but it does not work when the scp
is in progress. The scp
terminates immediately. Is there any way to do this? Code is below. Pretty simple.
我希望我的脚本能够完成一个scp,即使输入了CTRL + C.我已经尝试使用陷阱来禁用CTRL + C,但是当scp正在进行时它不起作用。 scp立即终止。有没有办法做到这一点?代码如下。很简单。
#!/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”。
3 个解决方案
#1
12
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.
编辑:您可能希望将任何错误重定向到文件。
#2
4
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
#3
0
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
.
因此,您可以使用&发送后台进度,并查看所有后台任务和作业,您可以使用%1访问它并使用kill将其终止并使用fg将其移至前台。
#1
12
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.
编辑:您可能希望将任何错误重定向到文件。
#2
4
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
#3
0
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
.
因此,您可以使用&发送后台进度,并查看所有后台任务和作业,您可以使用%1访问它并使用kill将其终止并使用fg将其移至前台。