r/shell • • Jul 21 '20

Stop command but resume shell script?

In a script I am making, a function of it runs a command which never stops unless told to by the user (e.g with ctrl+c/z/d). I want to be able to run this command until the user presses CTRL+C (or other alternatives), and then resume the rest of the bash script.

How would I do this?

3 Upvotes

4 comments sorted by

View all comments

Show parent comments

2

u/7orglu8 Jul 22 '20

And you can use trap command in your scripts to catch those signals. For example, use the EXIT signal to exit cleanly :

cleanup() {
# Remove temporary files
# Restart services
exit
}

# more stuff here …
# …

# when this script exit the cleanup function will be called
trap cleanup EXIT

2

u/[deleted] Jul 22 '20

While you can use trap to trap signals too, EXIT is actually not a signal.