V. Doe
V. Doe

Reputation: 91

Bash script run with timeout won't exit on SIGINT

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!

Upvotes: 6

Views: 1463

Answers (1)

Inian
Inian

Reputation: 85780

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

Upvotes: 7

Related Questions