LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 02-17-2014, 10:01 AM   #1
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,387

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


Hi,

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, 11:06 AM   #2
jdkaye
LQ Guru
 
Registered: Dec 2008
Location: Westgate-on-Sea, Kent, UK
Distribution: Debian Testing Amd64
Posts: 5,465

Rep: Reputation: Disabled
Look here.
jdk
 
Old 02-17-2014, 11:00 PM   #3
harryhaller
Member
 
Registered: Sep 2004
Distribution: Slackware-14.2
Posts: 472

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, 02:26 AM   #4
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Mint, MX, antiX, SystemRescue
Posts: 2,337

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

Start this running:
Code:
 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:
Code:
 while [ 1 ]; do; sleep 1; ls -l ttt; done;
And also have a third window open running this:
Code:
 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-19-2014, 12:26 AM   #5
RandomTroll
Senior Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 2,156

Rep: Reputation: 275Reputation: 275Reputation: 275
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, 04:35 AM   #6
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,387

Original Poster
Rep: Reputation: 51
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:
Code:
// 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);
    sleep(t);

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

Code:
# 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?
./naps.sh
 
  


Reply

Tags
bash


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 02:03 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 11:18 PM
bash question: how to do "for a in b do..." on the command line Kropotkin Linux - Newbie 7 02-03-2006 09:27 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:26 AM.

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