Skip to main content

Stopping a script process with a Control + C or something [Resolved]

I am trying to make an AV bug raspberry pi for a class.

I am using sox to record sound. Which is working fine.

the issue is sox needs to be stopped by a control+C to stop and create the new file. If killall is sent from a different ssh session it will drop the other session and sox will not create the file.

#! /bin/bash
NOW=$( date '+%F_%H:%M:%S' )


sox -t alsa plughw:1  $NOW.wav;

sleep 6;

echo $filename

I have tried making a separate script for stopping it; pretty much

#! /bin/bash
sleep 5;
ps | grep sox | kill 0;

Then run a

#! /bin/bash

Any advice on how to stop sox in a way that would still produce an output file would be great. This will ideally be set to run at set times so avoiding human interaction is essential.

Question Credit: Reckless Liberty
Question Reference
Asked September 17, 2019
Posted Under: Unix Linux
4 Answers

kill -SIGINT <pid> is the usual equivalent to ^C.

credit: L29Ah
Answered September 17, 2019

use trapping:


# this is a trap for ctrl + c 
trap ctrl_c INT

function ctrl_c() 
  echo "Trap: CTRL+C received, exit"

credit: karmax
Answered September 17, 2019

This is working fine :

# This script will do exactly the same job of Ctrl^C for background process
# find the PID of running script

pid=$(ps -ef| grep <scriptname>)

echo ".... PSR Stats collection script PID:"+$pid +"is getting exited abruptly"

# exit the process 
kill -INT $pid

credit: Thomas
Answered September 17, 2019
Your Answer