Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable CTRL-C during scp?

Tags:

linux

bash

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.

like image 290
tkoehn Avatar asked Sep 04 '13 16:09

tkoehn


People also ask

What command can disable Ctrl C from stopping a 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?

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.

How do you Ctrl Z trap?

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.


2 Answers

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.

like image 90
Emery Lapinski Avatar answered Nov 15 '22 04:11

Emery Lapinski


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
like image 27
IanM_Matrix1 Avatar answered Nov 15 '22 04:11

IanM_Matrix1