Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8023317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:31:54+00:00 2026-06-04T22:31:54+00:00

Context: I have a bash script that contains a subshell and a trap for

  • 0

Context:

I have a bash script that contains a subshell and a trap for the EXIT pseudosignal, and it’s not properly trapping interrupts during an rsync. Here’s an example:

#!/bin/bash
logfile=/path/to/file;
directory1=/path/to/dir
directory2=/path/to/dir

cleanup () {
     echo "Cleaning up!"
     #do stuff
     trap - EXIT 
}

trap '{
    (cleanup;) | 2>&1 tee -a $logfile
}' EXIT

(
    #main script logic, including the following lines:
    (exec sleep 10;);        
    (exec rsync --progress -av --delete $directory1 /var/tmp/$directory2;);

)  | 2>&1 tee -a $logfile
trap - EXIT #just in case cleanup isn't called for some reason

The idea of the script is this: most of the important logic runs in a subshell which is piped through tee and to a logfile, so I don’t have to tee every single line of the main logic to get it all logged. Whenever the subshell ends, or the script is stopped for any reason (the EXIT pseudosignal should capture all of these cases), the trap will intercept it and run the cleanup() function, and then remove the trap. The rsync and sleep commands (the sleep is just an example) are run through exec to prevent the creation of zombie processes if I kill the parent script while they’re running, and each potentially-long-running command is wrapped in its own subshell so that when exec finishes, it won’t terminate the whole script.

The problem:

If I interrupt the script (via kill or CTRL+C) during the exec/subshell wrapped sleep command, the trap works properly, and I see “Cleaning up!” echoed and logged. If I interrupt the script during the rsync command, I see rsync end, and write rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(544) [sender=3.0.6] to the screen, and then the script just dies; no cleanup, no trapping. Why doesn’t an interrupting/killing of rsync trigger the trap?

I’ve tried using the --no-detach switch with rsync, but it didn’t change anything.
I have bash 4.1.2, rsync 3.0.6, centOS 6.2.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T22:31:56+00:00Added an answer on June 4, 2026 at 10:31 pm

    How about just having all the output from point X be redirected to tee without having to repeat it everywhere and mess with all the sub-shells and execs … (hope I didn’t miss something)

    #!/bin/bash
    logfile=/path/to/file;
    directory1=/path/to/dir
    directory2=/path/to/dir
    
    exec > >(exec tee -a $logfile) 2>&1
    
    cleanup () {
         echo "Cleaning up!"
         #do stuff
         trap - EXIT 
    }
    trap cleanup EXIT
    
    sleep 10
    rsync --progress -av --delete $directory1 /var/tmp/$directory2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a bash script that goes through a file that contains a
I have a Unix bash function that executes a script that parses custom environment
I'm developing a little script using ash shell (not bash). Now i have a
I have a bash script that takes the date, month and year as separate
I have a bash shell script that unzips a zip file, and manipulates the
(context)I have information from a bunch of elements that I'm collecting into a JSON
Context: I have a text field in my form that is meant for entering
Context: I have a class that implements a Session, maintaining a TCP connection to
I currently have an existing code in bash that greps a keyword from a
I have a bash script which use curl: url=https://example.com/api.cgi message=<?xml version=\1.0\ encoding=\utf-8\?> <request> <encoding>utf-8</encoding>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.