6

I have a bash script which calls another bash script within a for loop (under a timeout condition) in the following format:

#!/bin/bash

trap 'trap - SIGTERM && kill 0' SIGINT SIGTERM EXIT
INNER_SCRIPT_PATH="./inner_script.sh"

for file in "$SAMPLEDIR"/*
do
  if [[ "${file: -4}" == ".csv"  ]]; then 
    CSVPATH="$file"
    CSVNAME=${CSVPATH##*/} # extract file name
    CSVNAME=${CSVNAME%.*} # remove extension
    timeout -k 10s 30m bash "$INNER_SCRIPT_PATH" 
  fi 
done
wait

Pressing Ctrl-C does not quit out of all the processes, and I have a feeling there is probably something wrong with the way I'm calling the inner bash script here (especially with timeout). Would appreciate feedback on how to make this better!

1 Answer 1

7

The issue is with the timeout command, that makes your script immune to Ctrl+C invocation. Since by default timeout runs in its own process group and not in the foreground process group, it is immune to the signals invoked from an interactive terminal.

You can run it with --foreground to accept signals from an interactive shell. See timeout Man page

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! To clarify, will simply adding --foreground after timeout be sufficient?
@V.Doe: Should be! You can test it for your case
This will make timeout susceptible to Ctrl+C, but won't kill any children process. Would be great to have both features
@PabloBianchi are you sure it doesn't kill children processes? I'm reading the docs, and it says, that children of COMMAND will not be timed out - What exactly does that mean? It feels like if children are not killed that could cause SERIOUS issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.