bash-trap

bash restart sub-process using trap SIGCHLD?

旧城冷巷雨未停 提交于 2019-12-01 17:01:00
问题 I've seen monitoring programs either in scripts that check process status using 'ps' or 'service status(on Linux)' periodically, or in C/C++ that forks and wait on the process... I wonder if it is possible to use bash with trap and restart the sub-process when SIGCLD received? I have tested a basic suite on RedHat Linux with following idea (and certainly it didn't work...) #!/bin/bash set -o monitor # can someone explain this? discussion on Internet say this is needed trap startProcess

Bash not trapping interrupts during rsync/subshell exec statements

喜欢而已 提交于 2019-11-30 19:20:53
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

multiple bash traps for the same signal

半城伤御伤魂 提交于 2019-11-29 22:48:06
When I use the "trap" command in bash, the previous trap for the given signal is replaced. Is there a way of making more than one trap fire for the same signal? Edit: It appears that I misread the question. The answer is simple: handler1 () { do_something; } handler2 () { do_something_else; } handler3 () { handler1; handler2; } trap handler3 SIGNAL1 SIGNAL2 ... Original: Just list multiple signals at the end of the command: trap function-name SIGNAL1 SIGNAL2 SIGNAL3 ... You can find the function associated with a particular signal using trap -p : trap -p SIGINT Note that it lists each signal

Shell Script get CTRL+Z with Trap

社会主义新天地 提交于 2019-11-29 22:31:44
问题 I am trying to get the SIGSTOP CTRL + Z signal in my script's trap . When my script is executing, if I temporarily suspend from execution, send a SIGSTOP signal CTRL + Z , it needs to remove the files I create in it and to kill the execution. I don't understand why the following script doesn't work. But, more important, what is the correct way to do it? #!/bin/bash DIR="temp_folder" trap "rm -r $DIR; kill -SIGINT $$" SIGSTP if [ -d $DIR ] then rm -r $DIR else mkdir $DIR fi sleep 5 EDIT :

How to propagate a signal through a collection of scripts?

99封情书 提交于 2019-11-29 01:13:26
I have an collection of scripts which are controlled by a main one. I want to trap the signal ctrl + c in the main script and propagate it to the others. The other scripts should trap this signal as well ( from the main script ) and do some clean-up ... I have tried to send kill -s SIGINT to the children, but they seem they are unable to catch the signal( even if trap 'Cleanup' SIGINT being defined on the children scripts ) Any clues how to realize this? The following example demonstrates a parent script that does something ( sleep 5 ) after it starts two children that do their own thing (also

multiple bash traps for the same signal

 ̄綄美尐妖づ 提交于 2019-11-28 20:36:55
问题 When I use the "trap" command in bash, the previous trap for the given signal is replaced. Is there a way of making more than one trap fire for the same signal? 回答1: Edit: It appears that I misread the question. The answer is simple: handler1 () { do_something; } handler2 () { do_something_else; } handler3 () { handler1; handler2; } trap handler3 SIGNAL1 SIGNAL2 ... Original: Just list multiple signals at the end of the command: trap function-name SIGNAL1 SIGNAL2 SIGNAL3 ... You can find the

Is it possible to detect *which* trap signal in bash? [duplicate]

£可爱£侵袭症+ 提交于 2019-11-28 09:40:38
Possible Duplicate: Identifying received signal name in bash shell script When using something like trap func_trap INT TERM EXIT with: func_trap () { ...some commands... } Is there a way in the function block to detect which trap has called it? Something like: func_trap () { if signal = INT; then # do this else # do that fi } Or do I need to write a separate function for each trap type that does something different? Is there a bash variable that holds the latest received signal? Thanks in advance! No documentation hints of any argument or variable holding the signal that was trapped, so you'll

How to propagate a signal through a collection of scripts?

我的梦境 提交于 2019-11-27 15:45:38
问题 I have an collection of scripts which are controlled by a main one. I want to trap the signal ctrl + c in the main script and propagate it to the others. The other scripts should trap this signal as well ( from the main script ) and do some clean-up ... I have tried to send kill -s SIGINT to the children, but they seem they are unable to catch the signal( even if trap 'Cleanup' SIGINT being defined on the children scripts ) Any clues how to realize this? 回答1: The following example

How to send a signal SIGINT from script to script?

你说的曾经没有我的故事 提交于 2019-11-26 22:00:49
问题 I want to trap a signal send from Script-A.sh to Script-B.sh so in Script-A.sh i use the command: (Send SIGINT to Script-B.sh) kill -2 $PID_Script-B.sh And in Script-B.sh i catch the signal and call function Clean trap 'Clean' 2 It does not work, instead the Script-B.sh is killed right away without performing the Clean !! What i notice also is that if i want to send SIGINT from terminal to any script that traps it, a ctrl-c will be caught correctly, but not if i specify the signal via the