I have the following script:
tail -f nohup.out
echo 5
When I press Ctrl+C on tail -f, the script stops running: the 5 is not printed. How can I run the command echo 5 after I stop the tail command?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Ctrl+C sends the SIGINT signal to all the processes in the foreground process group. While
tailis running, the process group consists of thetailprocess and the shell running the script.Use the
trapbuiltin to override the default behavior of the signal.The code of the trap does nothing, so that the shell progresses to the next command (
echo 5) if it receives a SIGINT. Note that there is a space between the quotes in the first line; any shell code that does nothing will do, except an empty string which would mean to ignore the signal altogether (this cannot be used because it would causetailto ignore the signal as well). The second call totraprestores the default behavior, so that after the third line a Ctrl+C will interrupt the script again.