how to continue run script after tail -f command

前端 未结 6 828
攒了一身酷
攒了一身酷 2021-01-18 09:55

I have the following script:

tail -f nohup.out
echo 5

When I press Ctrl+C on tail -f, the script stops r

相关标签:
6条回答
  • 2021-01-18 10:04

    As mentioned by rwos , but adding inline trapping.

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

    This would work without closing putty session when issuing subsequent SIGINT signals (that was my problem)

    0 讨论(0)
  • 2021-01-18 10:05

    Look up File:Tail in perl, have used this a few times on different projects to automate tailing logs and carrying out a specific action for log output.

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

    0 讨论(0)
  • 2021-01-18 10:09

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

    Use the trap builtin to override the default behavior of the signal.

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

    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 cause tail to ignore the signal as well). The second call to trap restores the default behavior, so that after the third line a Ctrl+C will interrupt the script again.

    0 讨论(0)
  • 2021-01-18 10:09

    You could just start the tail in a new shell, like so:

    #!/bin/bash
    bash -i -c 'tail -f nohup.out'
    echo 5
    
    0 讨论(0)
  • 2021-01-18 10:21
    #!/bin/bash
    trap "echo 5" SIGINT SIGTERM
    tail -f nohup.out
    
    0 讨论(0)
  • 2021-01-18 10:26

    In bash end the line with a single ampersand to run the command in the background in an async way

    tail -f nohup.out &
    
    0 讨论(0)
提交回复
热议问题