Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 02-17-2014, 09:01 AM   #1
Senior Member
Registered: May 2007
Posts: 1,028

Rep: Reputation: 35
Question bash: is there "ctrl-c" to kill just one command on multi commands line?


Is there shortcut similar to "ctrl-c" that will kill just one command on multi command line?

For example, if we execute the command:

echo a; sleep 1; echo b; sleep 3; echo c; sleep 1; echo d; sleep 1; echo e

And make a ctrl-c before "c" is echoed, the whole line is killed. I want it to kill the "sleep 3" command only, and the others after it would execute normally. Using bash's 'read' isn't really a solution because I would have to add a read between every line of every case... not easy, not pretty, seems like should exist something else.

Any ideias?

Thank you

Tags: bash sh C-c ctrl-c
Old 02-17-2014, 10:06 AM   #2
LQ Guru
Registered: Dec 2008
Location: Westgate-on-Sea, Kent, UK
Distribution: Debian Testing Amd64
Posts: 5,465

Rep: Reputation: Disabled
Look here.
Old 02-17-2014, 10:00 PM   #3
Registered: Sep 2004
Distribution: Slackware-14.0
Posts: 452

Rep: Reputation: Disabled
control-c sends a signal to the program.

It is important to understand how individual programs react to those signals and how your script is going to react to those signals and exit/return codes.

See "man 7 signal" and "man kill"

Try writing some test programs/scripts which capture the signal by using a "trap".

read "man -P 'less +2199' bash" and "man -P 'less +4613' bash" for an explanation.
Old 02-18-2014, 01:26 AM   #4
Senior Member
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD, Raspbian, Arch
Posts: 2,283

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Not sure if this is exactly what you want, but it serves as a fun demo anyway...

Start this running:
 sleep 500; echo hello >> ttt; sleep 600; echo goodbye >> ttt
Then hit ctrl-z. After that, the file "ttt" will contain "hello". Then hit ctrl-z a second time. Now the file "ttt" will contain "hello goodbye". Now run "pkill -9 sleep" to nuke the two sleep commands you suspended with those ctrl-z's.


To see this in action, do all the above in one window. But before starting, have a second window open running this:
 while [ 1 ]; do; sleep 1; ls -l ttt; done;
And also have a third window open running this:
 while [ 1 ]; do; sleep 1; ps -ef | grep sleep; done;
Get those second two windows running and scrolling, then start doing your stuff in the first window. The 2nd and 3rd windows will show you what is happening, and when. You can see the size of the "ttt" file jump when it's written to. I gave the two sleep commands different durations so you can tell them apart when looking at the ps -ef output in the third window. Note: ctrl-z does not kill the sleep commands, it merely suspends them (one suspension for each ctrl-z you hit). Therefore the command following them can execute. You have to kill the sleep's separately with pkill or some other kill variant.
1 members found this post helpful.
Old 02-18-2014, 11:26 PM   #5
Senior Member
Registered: Mar 2010
Distribution: Slackware
Posts: 1,417

Rep: Reputation: 238Reputation: 238Reputation: 238
I'm not exactly sure what you want, but when I have a script executing a string of commands and one hangs up and I want to stop that particular command but not halt the script, I kill its process from another virtual terminal.
Old 02-22-2014, 03:35 AM   #6
Senior Member
Registered: May 2007
Posts: 1,028

Original Poster
Rep: Reputation: 35
Red face

Sorry, people. Now I saw that my question was a bit unclear and misleading.

Thanks for the answer, Haertig.

I wanted a key shortcut that works exactly like the ctrl-z does in your example. And in fact, ctrl-c does! LOL I even wrote a small C program to make sure the behavior with shell's commands (like sleep) wasn't different from normal programs.

But I wasn't crazy when I wrote the question, although I was distracted to one VERY important detail: it was a shell script. My need is to send a "kill the current command" for .sh scripts, but let them continue. Typing a ctrl-c with aliases and functions kills just the current one! The second question: how to fully stop a function/alias? Let it quiet for now.

So, the question is: can I kill the current command being executed inside a script? (a short and simple way, not using ps+kill bruteforce)

Some tests I used now, that anyone can repeat:

The nap.c program:
// Compile with:   gcc -Wall nap.c -o nap
// Execution: ./nap [seconds = 10]
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char **argv)
    int t=10;

    if (argc>1)
        t = atoi( argv[1]);

    printf("Sleeping %d seconds...\n", t);

    printf("Woke up! [%d]\n", t);
    return 0;
The script:
./nap; ./nap 2; ./nap 3; ./nap 4
Testing lines:

# ctrl-c kills a single one, you won't see "woke up" for it
./nap; ./nap 2; ./nap 3; ./nap 4

# Within an alias it also kills a single one
alias naps="./nap; ./nap 2; ./nap 3; ./nap 4"
naps        # hit ctrl anytime to kill each single instance

# Within a function too
function nnnap() { ./nap; ./nap 2; ./nap 3; ./nap 4; }
nnnap        # hit ctrl anytime to kill each single instance

# But within a shell script, is it possible?



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
bash question: how to do "for a in b do..." on the command line Kropotkin Linux - Newbie 7 02-03-2006 08:27 AM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:25 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration