How to continue script execution after tail -f command

I have the following script:

tail -f nohup.out echo 5 

When I press Ctrl + C on tail -f , the script stops working: 5 does not print. How can I run the echo 5 command after stopping the tail command?

+6
source share
5 answers

Ctrl + C sends a SIGINT signal to all processes in the foreground a group of processes . While tail is running, the process group consists of a tail process and a shell using a script.

Use trap to override the default behavior of the signal.

 trap " " INT tail -f nohup.out trap - INT echo 5 

The trap code does nothing, so the shell moves on to the next command ( echo 5 ) if it receives SIGINT. Note that there is a space between quotation marks in the first line; any shell code that does nothing will do, with the exception of an empty line, which would mean ignoring the signal altogether (this cannot be used because it will cause tail also ignore the signal). The second call to trap restores the default behavior, so after the third line a Ctrl + C will abort the script again.

+8
source
 #!/bin/bash trap "echo 5" SIGINT SIGTERM tail -f nohup.out 
+1
source

You can simply run tail in a new shell, for example:

 #!/bin/bash bash -i -c 'tail -f nohup.out' echo 5 
0
source

As rwos mentioned, but adding inline trap.

 bash -i -c "trap ' ' SIGINT; tail -F '$1'; trap - SIGINT" 

This will work without closing the putty session when issuing subsequent SIGINT signals (this was my problem)

0
source

See File: Tail in perl, used it several times for different projects to automate tail logs and perform a specific action to exit the log.

google ftp-upload-mon project I uploaded to sourceforge that uses File: Tail to tail -f xferlogs

-1
source

Source: https://habr.com/ru/post/922824/


All Articles