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

  • Home
  • SEARCH
  • 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 4265548
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:38:21+00:00 2026-05-21T06:38:21+00:00

Use trap to capture signals like this: i=-1;while((++i<33)); do trap echo $i >> log.txt

  • 0

Use trap to capture signals like this:

i=-1;while((++i<33));
do
    trap "echo $i >> log.txt" $i;
done

And close the terminal by force.

The content in log.txt is then (under redhat linux):

1

18

1

17

0

Where these signals from?

  • 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-05-21T06:38:22+00:00Added an answer on May 21, 2026 at 6:38 am

    The first signal is SIGHUP; that gets sent to all processes in the process group when the terminal disconnects (hangs up – hence HUP).

    The second signal is SIGCONT (thanks, SiegeX, for the numbers). This is slightly surprising; it suggests you had a job stopped in the background which had to be allowed to run again.

    The third signal is another SIGHUP. This was likely sent to ensure that the continued process got its turn to exit, but was sent to the whole process group. (See the POSIX standard for information on process groups, etc.).

    The fourth signals is a SIGCHLD, indicating that a child process died and the corpse is available (well, the status is available).

    The final signal, 0, is the shells internal pseudo-signal indicating that it is exiting.

    You can do:

    trap 'echo Bye' 0
    

    to echo ‘Bye’ when the shell exits under control for any reason. You chose to echo the signal number to the file instead. Since the shell exits at this point, that is the last signal message that is seen. Its parent process should get a SIGCHLD signal because the shell died.


    FWIW, on MacOS X 10.6.7, I ran your test. There isn’t a signal 32 on MacOS X, and some of the mappings are different, and the sequence of signals sent is also different:

    $ i=-1;while((++i<33));
    > do
    >     trap "echo $i >> log.txt" $i;
    > done
    -sh: trap: 32: invalid signal specification
    $ trap
    trap -- 'echo 0 >> log.txt' EXIT
    trap -- 'echo 1 >> log.txt' HUP
    trap -- 'echo 2 >> log.txt' INT
    trap -- 'echo 3 >> log.txt' QUIT
    trap -- 'echo 4 >> log.txt' ILL
    trap -- 'echo 5 >> log.txt' TRAP
    trap -- 'echo 6 >> log.txt' ABRT
    trap -- 'echo 7 >> log.txt' EMT
    trap -- 'echo 8 >> log.txt' FPE
    trap -- 'echo 9 >> log.txt' KILL
    trap -- 'echo 10 >> log.txt' BUS
    trap -- 'echo 11 >> log.txt' SEGV
    trap -- 'echo 12 >> log.txt' SYS
    trap -- 'echo 13 >> log.txt' PIPE
    trap -- 'echo 14 >> log.txt' ALRM
    trap -- 'echo 15 >> log.txt' TERM
    trap -- 'echo 16 >> log.txt' URG
    trap -- 'echo 17 >> log.txt' STOP
    trap -- 'echo 19 >> log.txt' CONT
    trap -- 'echo 20 >> log.txt' CHLD
    trap -- 'echo 23 >> log.txt' IO
    trap -- 'echo 24 >> log.txt' XCPU
    trap -- 'echo 25 >> log.txt' XFSZ
    trap -- 'echo 26 >> log.txt' VTALRM
    trap -- 'echo 27 >> log.txt' PROF
    trap -- 'echo 28 >> log.txt' WINCH
    trap -- 'echo 29 >> log.txt' INFO
    trap -- 'echo 30 >> log.txt' USR1
    trap -- 'echo 31 >> log.txt' USR2
    $
    

    The signals captured in one run were:

    2
    1
    20
    0
    

    In a second run, I got:

    20
    1
    20
    0
    

    The SIGINT first is surprising — I don’t think I can explain that unless it simply means an incomplete write of some sort (it should have read 20 but the SIGHUP caused a problem). I’m not sure that I can explain the SIGCHLD signals either; the SIGHUP and ‘exit’ trap are as before.

    To some extent, though, the signals are system specific – or so it seems. The SIGHUP is common and constant, though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use trap to write errors to file, and want write line number where
When I use the trap command in Bash, the previous trap for the given
'''use Jython''' import shutil print dir(shutil) There is no, shutil.move, how does one move
Use case: A does something on his box and gots stuck. He asks B
Use case: 3rd party application wants to programatically monitor a text file being generated
We use a data acquisition card to take readings from a device that increases
I use Firebug and the Mozilla JS console heavily, but every now and then
I use rsync to synchronize files to Windows clients in a server agnostic way.
I use emacs to edit my xml files (nxml-mode) and the files were generated
I use the VS2008 command prompt for builds, TFS access etc. and the cygwin

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.