So I have the following command running from the shell

~/for ip in $(cat ip_list);do mkdir $ip; cd $ip; for vuln in $(snmap http);do nmap --script=$vuln $ip -oN $vuln;done cd ../ ; done

and it's been running fine for about a day. It's properly creating directories and navigating through them.

HOWEVER, one of the scripts is hanging now...

So what I'd like to do is use a key command similar to CTR+C that will kill this iteration of nmap but not exit the script that's running. That way I don't have to start all over or modify my ips list.

  • What happens when a mkdir fails? Are you okay with that case, or is there a set -e somewhere?
    – thrig
    Nov 30, 2016 at 17:12
  • Ctrl-Z to suspend the process? You can then background it with bg or resume it in the foreground with fg.
    – DopeGhoti
    Nov 30, 2016 at 18:46

2 Answers 2


This answer is for Bash.

You can use a trap for this. This is an illustrative example. To continue in the loop, use Ctrl+c. There's no way though to stop the script though apart from closing the terminal or killing it from another terminal.


trap ps SIGINT
for i in a b c d;  do
    for j in w x y z; do
        echo ----$i$j------ && ping stackexchange.com -c 10

Your script will look something like this (after some of my improvements), but notice it's not been tested:

trap '' SIGINT
for ip in $(cat ip_list); do
    mkdir $ip && cd $ip &&
    for vuln in $(snmap http);do
        nmap --script=$vuln $ip -oN $vuln
    done &&
    cd ..

The changes I introduced like && are preventing from entering logical paths that can lead to error. If possible it's safer to impose such contstraints. When it's put this way:

mkdir $ip; cd $ip;

if the directory can't be created, then cd leads to error.

When you put it this way:

mkdir $ip && cd $ip

if the directory hasn't been created for some reason, then no cd is attempted. When you keep all the commands from mkdir $dir through cd $dir to cd .. chained with &&, the logic is clear and the structure will not go wrong. When you use ; and fail to create the directory, then you fail to enter it, but you will possibly not fail to change to ...


I know I posted and then answered but I wanted to preserve this answer in the case anyone else came across this problem.

What I did was from another shell, I ran ps -a then I found my nmap process and killed it with kill proc-id. The script continued on to the next iteration.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .