On Fri, Oct 21, 2022 at 03:50:41PM +0200, Solene Rapenne wrote:
> hi
> 
> I found a weird behavior trying to use timeout(1) in a script with
> interactive commands. My use case is to ask for unlocking /home
> from /etc/rc.local but continue if I don't type it in time.
> 
> this is working:
> 
>       timeout 2s less /etc/fstab
> 
> the same command isn't working from a script, running it get stuck and
> can't be stopped with ctrl+C
> 
>       #!/bin/sh
>       timeout 2s less /etc/fstab

Using bash(1) or an interactive ksh(1) runs the pager and terminates it
after two seconds:

        $ bash -c 'timeout 2s less /etc/fstab'
        $ ksh -ic 'timeout 2s less /etc/fstab'

In a non-interactive sh/ksh, GNU's implementation cannot be interrupted,
either, but it does deliver SIGTERM as expected:

        # pkg_add coretutils
        quirks-6.42 signed on 2022-10-22T00:59:54Z
        coreutils-9.1: ok
        $ time sh -c 'gtimeout 2s less /etc/fstab'
        ^C^C
            0m02.02s real     0m00.00s user     0m00.03s system

> However, using timeout with a non interactive command in a script
> doesn't create troubles

I haven't looked into this yet, but two things seem worth noting now:

First, non-interactive sh/ksh receives ^C/SIGINT but does not deliver it
to its child proces like interactive shells do, e.g.

        $ sh -c 'timeout 2s less /etc/fstab'
        ^C^C

does nothing.

Secondly, interrupting our timeout(1) directly does not terminate:

        $ pgrep -fl fstab
        89054 less /etc/fstab
        49950 timeout 100s less /etc/fstab
        54620 sh -c timeout 100s less /etc/fstab
        $ kill -INT 49950
        $ kill -TERM 49950
        $ kill -QUIT 49950
        $ pgrep -fl fstab
        89054 less /etc/fstab
        49950 timeout 100s less /etc/fstab
        54620 sh -c timeout 100s less /etc/fstab

> in the issue case, a ktrace gives the following last lines, but I don't
> know how to interpret them
> 
>  21097 timeout  PSIG  SIGCHLD caught handler=0xb1d715d6b00 
> mask=0x86007<SIGHUP|SIGINT|SIGQUIT|SIGALRM|SIGTERM|SIGCHLD>
>  21097 timeout  RET   sigsuspend -1 errno 4 Interrupted system call
>  21097 timeout  CALL  sigreturn(0x7f7ffffe3540)
>  21097 timeout  RET   sigreturn JUSTRETURN
>  21097 timeout  CALL  kbind(0x7f7ffffe3868,24,0xd30add790970a7bd)
>  21097 timeout  RET   kbind 0
>  21097 timeout  CALL  wait4(WAIT_ANY,0x7f7ffffe39f4,0<>,0)
> 

Reply via email to