LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to stop a bash script by pressing a key? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-stop-a-bash-script-by-pressing-a-key-4175488363/)

Juicy 12-17-2013 08:51 PM

how to stop a bash script by pressing a key?
 
Hi i want to know how to stop this loop by pressing a key, and if possible how to start it again with a keystroke.

Code:

#!/bin/bash

PORT=$(($RANDOM))


WID=`xdotool search --name "xxxxxxxxx" | head -1`
xdotool windowactivate $WID

while :

 do

 xdotool sleep .$PORT
 xdotool sleep 4
 xdotool type 7


done


caladran 12-17-2013 09:19 PM

Ctrl+Z will stop the process. You can resume it by using the fg command.

In general, a process can be stopped and resumed by sending the SIGSTOP and SIGCONT signals to the process.

Juicy 12-17-2013 10:30 PM

naw dude. it doesnt work

btmiller 12-17-2013 11:18 PM

Yes it does. Unless the xdotool is intercepting signals and ignoring them, which is always possible. IIRC, Ctrl+Z sends SIGTSTP which can be caught by a process and thus ignored, whereas SIGSTOP cannot be caught by a process.

Juicy 12-18-2013 01:14 AM

okay so how to i code for it to send sigstop by pressing a key?

pan64 12-18-2013 01:48 AM

you can use the command stty -a to check how it is set.
you will get something like this:
Code:

intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = M-^?; eol2 = M-^?; swtch = M-^?; start = ^Q;
stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd cs8 hupcl -cstopb cread -clocal -crtscts
-ignbrk brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc ixany imaxbel iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke

see man stty and you will see susp defines that event:
Code:

susp CHAR
              CHAR will send a terminal stop signal

therefore you need to execute stty susp <your char> if you want to modify it.

frankbell 12-18-2013 08:41 PM

Try CTRL-C.


All times are GMT -5. The time now is 10:36 PM.