Alex Rivera | Logout

Executing subsequent command/script, after the current one was ^C

Asked 2012-08-29T09:50:30.410
10

I'm failing to find answer to my problem. I need to be able to execute a shell command/bash script after the current one was exited with ^C.

In other words: I've got a script which is running and just after I ^C from it I want another script or command to launch immediately after. How to achieve this?

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

I could try to run simultaneous scripts where one would be waiting/listening for the other to finish and then do the cleaning-up commands and also exit. But then I'd need a way of launching them in one command.

What would be the most appropriate approach to this problem?

Thanks

EDIT: I think I've got it... From the first script I can launch the other one with & at the end so it goes to the background. Seems to work with ^C exit from the first one (second continuous to work). Any suggestions on better solutions?

Edit
Report

1 Answer

9

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

./script1.sh || ./script2 i.e. execute 2nd if 1st was NOT successful.

or in script1 you could trap the exit signal and spawn the second process. Something like this:

s1:

#!/bin/bash

trap "./s2.sh" SIGINT

echo "hello"
sleep 100

s2:

#!/bin/bash

echo "goodby"

session:

$ ./s1.sh 
hello
^Cgoodby
answered 2012-08-29T09:59:11.300

Your Answer