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.
#!/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.
You need to use the trap command which can enable or disable keyboard keys such as Crtl-C. SIGINT is Crtl-C and it is represented using number 2. Signal names are case insensitive and the SIG prefix is optional. trap -l prints list of signal names and their corresponding numbers.
How is Ctrl-C used in trap utility? The purpose is to disable CTRL+C or CTRL+Z interrupt for the root user or a general user account by the trap command. So basically, when the user tries to interrupt any command or script using CTRL+C or CTRL+Z, he will not be able to do so.
Show the signal numbers with “kill -l”. In the below output the numbers for the signals are shown, and in case of CRTL+C is “SIGINT” signal number 2 and CRTL+Z is “SIGTSTP” signal number 20. To disable ctrl+c or ctrl+z for all users, append the trap command combinations in /etc/profile.
Put it in the background in a subshell to disconnect it from the terminal.
(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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With