0

I have a radio.sh file. I start it from Java with:

Process ls=null;
BufferedReader input=null;
String line=null; 
try {
    ls= Runtime.getRuntime().exec(new String[]{"/bin/bash", "/home/djeki22/Dokumente/radio.sh"});
    input = new BufferedReader(new InputStreamReader(ls.getInputStream()));

    } catch (IOException e1) {
        e1.printStackTrace();  
        System.exit(1);
      }

in the radio.sh:

#!/bin/bash

rtl_fm -f 103.78M:103.82M:25K -M wbfm -s 159K -r 48K -l 2 -E deemp -F 9 - | aplay -r 48k -f S16_LE

I would like to kill this process because I want to change the frequency. I'd like to run a new script radio1.sh with the same script, but with other frequency.

I can kill rtl_fm and aplay with killall rtl_fm and killall aplay, but rtl_fm isn't 100% killed. But I cannot hear anything. If I start java and start this radio.sh, I cannot hear anything. It's not working.

If I start radio.sh in a terminal, it tells me that it cant open the device. I think the device is in use.

How can I kill this process with a script?

2
  • Is the process definitely killed? Try "ps -ef | grep rtl_f[m]" and see if anything is listed. If it is, then yes, the process was not killed. If it is not, then process was indeed killed, but there is a device driver problem leaving the rtl device inaccessable. I have seen this happen before, although on other platforms. My only resolution then was to unplug the RTL dongle and plug it back in again.
    – Paul
    Oct 14, 2015 at 19:45
  • Ty for this Command. I can see the rtl_fm with the -f ... options. If i use the Terminal with the radio.sh script, then i can stop this with strg+c. Then i can start another script. This works, but how i use the strg+c in scripts? Maybe its a result for my problem.
    – DjEKI
    Oct 14, 2015 at 21:48

1 Answer 1

1

The best way to do it is to have the process (rtl_fm) write it's own PID to a PID file, then use the contents of that file to kill the process.

However, since rtl_fm does not have a way to write it's PID to a file, we can use a workaround.

#!/bin/bash
RTL_PID=`ps -e | grep rtl_f[m] | awk '{print $1}'`
kill ${RTL_PID}

First, we set a variable RTL_PID with the processes PID. We get this by listing all processes, looking for rtl_fm. The [m] is a trick that keeps grep rtl_fm from showing up in the list of processes. Finally, take the line output and get the first field from it, which is the PID.

Note that this will send a TERM signal to the process. If that doesn't work and you need to send a KILL, add a -9, e.g. kill -9 ${RTL_PID}.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .