By executing the following / script command, the current one was ^ C

I can not find the answer to my problem. I need to be able to execute a shell / bash script command after the current one has been output using ^ C.

In other words: I have a script that runs, and right after I ^ C from it, I want another script or command to run right after. How to achieve this?

I tried ./first.sh; echo something ./first.sh; echo something with no luck.

I could try running simultaneous scripts where I could wait / listen for another to finish, and then execute the cleanup commands and exit. But then I would need to run them in one team.

What would be the most appropriate approach to this problem?

Thanks,
EDIT: I think I have it ... From the first script, I can run another with and at the end so that it goes to the background. It seems to work with ^ C output from the first (second continuous to work). Any suggestions for better solutions?

+4
source share
3 answers

./script1.sh && ./script2 i.e. complete 2nd if 1st was successful.

./script1.sh || ./script2 ./script1.sh || ./script2 i.e. complete 2nd if 1st was not successful.

or in script 1, you can capture the output and start the second process. Something like that:

s1

 #!/bin/bash trap "./s2.sh" SIGINT echo "hello" sleep 100 

s2

 #!/bin/bash echo "goodby" 

session:

 $ ./s1.sh hello ^Cgoodby 
+3
source

You need:

  • You need to capture sigint (ctrl-c) so that the script is not finished when you press ctrl-c
  • After completing the command, you need to check the reason why it was completed. If the command was interrupted using SIGINT (signal 2), the exit code for this command will be 130 (128 + 2).

You do it like this:

 trap "echo ctrl c was pressed" SIGINT sleep 100 [ "$?" = 130 ] && echo "you've interrupted the previous command with ctrl-c" 

Example:

 $ bash 1.sh ^Cctrl c was pressed you've interrupted the previous command with ctrl-c 
+2
source

Bash Beginner's Guide Chapter 12. Trapping Signals

Here is a very simple example: intercepting Ctrl + C from the user after which he prints the message. When you try to kill this program without specifying a KILL signal, nothing will happen:

 #!/bin/bash # traptest.sh trap "echo Booh!" SIGINT SIGTERM echo "pid is $$" while : # This is the same as "while true". do sleep 60 # This script is not really doing anything. done 
0
source

All Articles