Author Topic: [ SOLVED ] Kill a Process ... some help needed  (Read 5422 times)

Offline Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 11060
  • MLUs Forever!
[ SOLVED ] Kill a Process ... some help needed
« on: December 01, 2011, 06:19:11 PM »
I want to put a recording command into a script, and be able to stop the recording when I choose.

This uses Pulse Audio recording command 'parec' to pipe the raw audio to 'sox' which records the audio in the specified format.

This is the command being used


Code: [Select]
parec -d alsa_output.pci-0000_00_10.1.analog-stereo.monitor | sox -t raw -44.1k -sLb 16 -c 2 - ~/record.wav
If I run it from a terminal, sox stops with Ctrl+C .....  it gets Killed.

If I use a second terminal and use
pkill sox
it stops, but not cleanly ....  reporting a 'Broken Pipe' and failure to write to the output file. Similarly if I kill the process from the 'System Activity' window.

So the question is .......  is there any way to stop the sox process from within the script which has called it ......  even by any workaround means? Preferably it should be killed cleanly.

I thought of getting its PID and killing that within the script .......  but once launched, the script is halted until sox is stopped ....  a Catch 22.


Any suggestions welcomed  ;)

regards.

« Last Edit: December 02, 2011, 04:09:14 PM by Just18 »
MLUs rule the roost!

Linux XPS 3.4.48-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Offline Neal ManBear

  • Administrator
  • Super Villain
  • *****
  • Posts: 15829
  • LXDE! Coffee, Bacon and Cheesecake!
Re: Kill a Process ... some help needed
« Reply #1 on: December 01, 2011, 06:46:32 PM »
Not sure on this one. I'm not used to parec or sox. :-\  Try
Code: [Select]
sox --off     
if it isn't too silly of me to suggest it.     

Offline Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 11060
  • MLUs Forever!
Re: Kill a Process ... some help needed
« Reply #2 on: December 02, 2011, 03:23:43 AM »
Not sure on this one. I'm not used to parec or sox. :-\  Try
Code: [Select]
sox --off     
if it isn't too silly of me to suggest it.     

while sox is running nothing else happens within the script ......  so unless something like a separate terminal-type process is run (not explaining this correctly) from where the shut-down/kill of  sox can be run, I see no way to stop it ......
MLUs rule the roost!

Linux XPS 3.4.48-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Offline menotu

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 15515
  • ┌∩┐(◕_◕)┌∩┐
Re: Kill a Process ... some help needed
« Reply #3 on: December 02, 2011, 04:14:52 AM »
Just thinking aloud - can killall sox be used?
PCLinuxOS 32bit KDE 4.10.4; kernel-3.4.11-pclos1.bfs & 64bit 3.4.38bfs; NVidia GeForce 8400GS 1GB 310.19 driver

Sony Vaio SVE1513A4ESI Laptop, Intel Core i5, 2.6GHz, 6GB RAM, 750GB, 15.6" Intel HD Graphics 4000

Offline Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 11060
  • MLUs Forever!
Re: Kill a Process ... some help needed
« Reply #4 on: December 02, 2011, 04:20:16 AM »
Just thinking aloud - can killall sox be used?

The question is where and controlled by what?

Look at it this way ......  the script runs, and eventually calls the recording command .........  and essentially it halts at that point, waiting for the recording to finish .......  but the only way to finish seems to be by killing sox in some manner or form .......  and because the script is waiting for the recording to finish, there is no means to do so ......  Catch 22 ......
MLUs rule the roost!

Linux XPS 3.4.48-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Offline Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 11060
  • MLUs Forever!
Re: Kill a Process ... some help needed
« Reply #5 on: December 02, 2011, 04:27:02 AM »
Trying to take a different approach to this .....

I can wrap the recording command in a Zenity progress scheme, which will display a progress box on screen while the recording is progressing.

That progress box carries a "Cancel" button. This is to cancel the progress window.

Is there perhaps a means of trapping that Cancel, or adding to it, so that sox gets killed also?
BTW .....  is there an equivalent terminal command to typing Ctrl+C ?

In fact that would be a preferred method ......  giving a visual indication of recording, and stopping the recording along with the progress box.

Some more thinking to be done ........

MLUs rule the roost!

Linux XPS 3.4.48-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT


Offline Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 11060
  • MLUs Forever!
MLUs rule the roost!

Linux XPS 3.4.48-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Offline Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 11060
  • MLUs Forever!
Re: Kill a Process ... some help needed
« Reply #8 on: December 02, 2011, 05:07:55 AM »
Sorry, Neal, nothing there that I could determine that would be of any use in this case .....  but there might be something I did not understand.

Everything so far needs to be able to determine the PID of the process and be able to enter a command.

The only means I can imagine to do this is by using something like Zenity, as mentioned above ....  because once the recording command is issued no further commands are executed until it is stopped. Also of course its PID is not available until after it is started ........

So I think I will try to figure out if something like Zenity 'Cancel', if not Zenity itself, could be used ....

Is there some means to 'wrap' the record command in another process, so that the parent process could be killed along with its daughter?

Thanks for the responses so far ....... ;)
MLUs rule the roost!

Linux XPS 3.4.48-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Offline Neal ManBear

  • Administrator
  • Super Villain
  • *****
  • Posts: 15829
  • LXDE! Coffee, Bacon and Cheesecake!
Re: Kill a Process ... some help needed
« Reply #9 on: December 02, 2011, 05:28:53 AM »
Hmm...... maybe try YAD instead of zenity......... hmm...... got my thinking cap on, but it ain't working too well..... sorry. I will think on this for a while. Maybe something will occur to me.     

Offline AS

  • Hero Member
  • *****
  • Posts: 4098
  • Have a nice ... night!
Re: Kill a Process ... some help needed
« Reply #10 on: December 02, 2011, 06:02:02 AM »
To kill a process, a signal is sent to the process itself, some signals can be caught from the process, some other can't, typically SIGKILL (9) and SIGTERM (15) are used to terminate a process.

see man 7 signal for furher info

You can instruct your 'script' to intercept some signal and execute some subroutine when receiving the signal, by using the the shell built-in command trap: (man 1 bash)
Quote
trap [-lp] [[arg] sigspec ...]
              The  command arg is to be read and executed when the shell receives signal(s) sigspec.  If arg is absent (and there is
              a single sigspec) or -, each specified signal is reset to its original disposition (the value it had upon entrance  to
              the  shell).   If  arg is the null string the signal specified by each sigspec is ignored by the shell and by the com‐
              mands it invokes.  If arg is not present and -p has been supplied, then the trap commands associated with each sigspec
              are  displayed.  If no arguments are supplied or if only -p is given, trap prints the list of commands associated with
              each signal.  The -l option causes the shell to print a list of signal names and their  corresponding  numbers.   Each
              sigspec  is either a signal name defined in <signal.h>, or a signal number.  Signal names are case insensitive and the
              SIG prefix is optional.

what you may want is something like:

Quote
#!/bin/bash
# example trapping signals
#
function_terminate()
{
   echo "this code will be executed upon receinving SIGUSR1 (signal 16)"
}

# script start here
#
trap function_terminate 16
#
# your script here
#
# echo "PID="$$
#
while true
do
    sleep(1)
done

run the above script, you will find the following output while the script will remain in loop execution:
Quote
./signal
PID=8101


then, from a second terminal execute the following command:
kill -16 8101
signal 16 is SIGUSR1, you will obtain the next output:
Quote
./signal
PID=8101
this code will be executed upon receiving SIGUSR1 (signal 16)

therefore you can put your commands to be executed upon receiving a signal (signal SIGUSR1), including an exit command to terminate the script.

HTH  ;)

AS


EDIT: the kill command took it's name because of it's original use, but should be more appropriate a name like send_signal   ;)
« Last Edit: December 02, 2011, 06:10:31 AM by as »

Offline AS

  • Hero Member
  • *****
  • Posts: 4098
  • Have a nice ... night!
Re: Kill a Process ... some help needed
« Reply #11 on: December 02, 2011, 06:38:10 AM »
...
This is the command being used

Code: [Select]
parec -d alsa_output.pci-0000_00_10.1.analog-stereo.monitor | sox -t raw -44.1k -sLb 16 -c 2 - ~/record.wav
If I run it from a terminal, sox stops with Ctrl+C .....  it gets Killed.

If I use a second terminal and use
pkill sox
it stops, but not cleanly ....  reporting a 'Broken Pipe' and failure to write to the output file. Similarly if I kill the process from the 'System Activity' window.


Please, ignore my previous post  ;D I missed one important thing.  :D

When you start your command, first sox is executed then the command parec is started.
if you press CTRL-C, you will kill the parec command, when parec is terminated, the pipe is closed (at the parec side), and sox terminate cleanly. Therefore you should try to kill parec, not sox. 

AS

Offline Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 11060
  • MLUs Forever!
Re: Kill a Process ... some help needed
« Reply #12 on: December 02, 2011, 07:34:46 AM »
Thanks AS ......  particularly for that last part about parec. ;)
I haven't read all you wrote, but will  :D

I came back to say I have it sorted .....  stupid me! I was making elementary errors with Zenity  :(

Here is the draft script I have been working on in case it is of any interest ......  and thanks again to all for your help ........

Code: [Select]
#!/bin/bash

# To record from the Pulse Audio Monitors of the sound system.
set -x

TITLE="Sound Recording"
SoundFile=~/record.wav
rm -f "$SoundFile"              # sox will create a new one.

# Get sink monitor:
MONITOR=$(pactl list | egrep -A2 '^(\*\*\* )?Source #' | grep 'Name: .*\.monitor$' | awk '{print $NF}' | tail -n1)
echo "set-source-mute ${MONITOR} false" | pacmd >/dev/null

# Zenity progress wrapper
(
echo
parec -d "$MONITOR" | sox -t raw -r 44.1k -sLb 16 -c 2 - "$SoundFile" &
) |
zenity --window-icon=$ICON --progress --pulsate --title="   $TITLE   " --text $"  
<b>Recording</b>  

 Sound is being recorded to $SoundFile file.

                  Press "Cancel" to stop recording." ;

PARECPID=$(pgrep parec); kill $PARECPID

exit 0

The source of the recording can be altered to another sink by using the drop down menu item in pavucontrol.

regards.
« Last Edit: December 02, 2011, 10:59:49 AM by Just18 »
MLUs rule the roost!

Linux XPS 3.4.48-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Offline pags

  • Hero Member
  • *****
  • Posts: 2606
  • Keep it clean.
Re: Kill a Process ... some help needed
« Reply #13 on: December 02, 2011, 09:12:43 AM »
Looks like you might be heading towards a solution...I was thinking you might want to initiate the process(es) within a subshell.  That should release control back to the script, so you can send a signal, if desired.  A subshell (in bash, anyway) is usually done with parenthesis.  See http://tldp.org/LDP/abs/html/subshells.html.

Offline Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 11060
  • MLUs Forever!
Re: Kill a Process ... some help needed
« Reply #14 on: December 02, 2011, 10:43:09 AM »
Thanks pags .....  I am glad I did not have that earlier  :D  the solution was simple .....  once found  ;D  ;D


Thanks to everyone for their assistance.  ;)


I now have my own wee recording application ......  and my itch is scratched  ;D

regards
« Last Edit: December 03, 2011, 06:57:05 AM by Just18 »
MLUs rule the roost!

Linux XPS 3.4.48-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT