logo
down
shadow

Interrupt sleep in bash with a signal trap


Interrupt sleep in bash with a signal trap

By : Mohammad Azaze
Date : November 17 2020, 04:28 AM
should help you out I'm trying to catch the SIGUSR1 signal in a bash script that is sleeping via the sleep command:
code :
#!/bin/bash

trap 'echo "Caught SIGUSR1"' SIGUSR1

echo "Sleeping.  Pid=$$"
while :
do
   sleep 10 &
   wait $!
   echo "Sleep over"
done


Share : facebook icon twitter icon
Is it possible to detect *which* trap signal in bash?

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


By : user1728478
Date : March 29 2020, 07:55 AM
Hope that helps No documentation hints of any argument or variable holding the signal that was trapped, so you'll have to write a function/trap statement for each trap you want to behave differently.
Python sleep function does not catch interrupt signal

Python sleep function does not catch interrupt signal


By : harshul.mittal.se
Date : March 29 2020, 07:55 AM
To fix this issue I cannot catch the INT signal in the main thread, please advise me how to fix the problem. I wish that the sleep method could be interrupted by CTRL+C, but it waits till the timer is ended.. , Believe it or not, this will work.
code :
from Queue import Queue, Empty
def signal_safe_sleep(delay):
  q = Queue()
  try: q.get(True, delay)
  except Empty: pass
bash trap interrupt command but should exit on end of loop

bash trap interrupt command but should exit on end of loop


By : Marvinrey A. Dorado
Date : March 29 2020, 07:55 AM
may help you . Try to simplify the discussion above, I wrap up an easier understandable version of show-case script below. This script also HANDLES the "double control-C problem": (Double control-C problem: If you hit control C twice, or three times, depending on how many wait $PID you used, those clean up can not be done properly.)
code :
#!/bin/bash

mp3convert () {
  echo "mp3convert..."; sleep 5; echo "mp3convert done..."
}

PreTrap() {
  echo "in trap"
  QUIT=1
  echo "exiting trap..."
}

CleanUp() {
  ### Since 'wait $PID' can be interrupted by ^C, we need to protected it
  ### by the 'kill' loop  ==> double/triple control-C problem.
  while kill -0 $PID >& /dev/null; do wait $PID; echo "check again"; done

  ### This won't work (A simple wait $PID is vulnerable to double control C)
  # wait $PID

  if [ ! -z $QUIT ]; then
     echo "clean up..."
     exit
 fi
}

trap PreTrap SIGINT SIGTERM SIGTSTP
#trap CleanUp EXIT

for loop in 1 2 3; do
    (
      echo "loop #$loop"
      mp3convert
      echo magic 1
      echo magic 2
      echo magic 3
    ) &
    PID=$!
    CleanUp
    echo "done loop #$loop"
done
bash trap won't ignore signal

bash trap won't ignore signal


By : Tommy Chrysostomou
Date : March 29 2020, 07:55 AM
it fixes the issue Have you tried using rsync with --no-detach as advised in the SO question bash not trapping interrupts during rsync subshell/exec statements?
bash signal trap will override the signal of its nohup sub commands?

bash signal trap will override the signal of its nohup sub commands?


By : Maxime
Date : March 29 2020, 07:55 AM
I hope this helps . The bash manual says:
Related Posts Related Posts :
  • Security of su root in bash script
  • how to replace a string at a specific position in a csv file using bash
  • how to delete duplicate lines in a text file in unix bash?
  • Compare a variables in bash
  • How do I use sed on a string?
  • Using bash variables for range in sed
  • convert a DURATION HH:MM:SS to seconds in bash
  • Shell make system paths click-able when printed out by my shell script
  • I want to use parallel-ssh to run a bash script on multiple servers, but it simple prints the echo statements
  • AWK between 2 patterns - first occurence
  • Nginx on docker - daemonize and attach to bash
  • Delete or Replace nth line in a file after a pattern match using shell scripts
  • Actual return code for SCP
  • How to exit a bash function that expects a return
  • Bash script to split file into two files while changing output file during processing
  • Pass command line arguments via sbatch
  • variable substitution (what do you call/name this method)?
  • Call function from another shell script
  • How to Grep and Replace With Exclusions
  • shell command to remove characters after a special character in bash/shell
  • How do I run a script for 1st working day of every month in cron?
  • Bash Unexpected End of File Error After Installing RVM
  • Nested Loop with increment on inner loop?
  • video orientation detection in bash
  • Want file rename script to add number at end with fixed length
  • What's the DRYest way to replace a file with a symlink in bash
  • How to run Bitcoin's autogen.sh file?
  • Renaming files in folder and subfolders using a list- Bash Script/Unix
  • Bash command to compare more than two files
  • If It is a directory append text
  • What's wrong with my WHILE-DO loop, or why my script can't find the file I'm giving it on a plate?
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org