Interrupt or Suspend a Command Execution in Linux

An accidentally executed sequence of command and system files is in danger now. Any time they will wipe out, What should I do now?

The situation is familiar; most Linux beginners face this situation and do something unintentionally which risks their files.

How to copy file from local to remote server in linux

Most of the time, you executed the rm command or transfer files using the mv or cp commands. To interrupt the command further from execution, you might want to stop them immediately.

In Linux, there are different ways to stop the execution of a process or commands softly and forcefully. There are many commands to stop the command execution in Linux but, you don’t need to be aware of those commands if you know the below shortcut key.

CTRL+C

It is one of the most used shortcut keys to prevent or interrupt command execution, while other commands sometimes fail to prevent executions like kill, killall, etc. CTRL+C can easily do it as its shortcut key. You don’t need to type any command between a running command.

Prevent or Interrupt a Command Execution in Linux

You can view below I have executed the ping command on “trendoceans.com”, and once two ICMP packets are sent, I aborted or interrupted the command execution using the CTRL+C shortcut key.

abort ping command execution in linux
Abort ping command execution in Linux

Suspend a Command Execution in Linux

Besides this, we have another shortcut key, CTRL+Z, which can pause or suspend executed commands. The only difference between CTRL+C and CTRL+Z is CTRL+C sends a killing signal and stops the process immediately. At the same time, the CTRL+Z command sends the SIGTSTP signal to the process.

Below same ping command is executed again. Instead of CTRL+C, we used the CTRL+Z shortcut key to suspend the command execution.

Suspend ping command execution in linux
Suspend ping command execution in Linux

The suspended command can be spotted if you execute ps or jobs command in your terminal.

$ ps
$ jobs

The picture below shows the behavior of this command.

Viewing suspended process
Viewing suspended process

Suspending is good if you want to pause the execution of the existing command for a while and take a look for something else. Once you have done it, you can go back to the suspended command and re-execute it.

To re-execute the suspend command use fg command, it will execute the latest background command and place it in the foreground.

$ fg

Below is the behavior of this command.

Placing background command in foreground for execution
Placing background command in the foreground for execution

If you put multiple commands in the background, you can specify their process ID to run the specific command in the foreground.

Below I have placed multiple commands ping and nano in the background using the CTRL+Z shortcut key. To execute the specific command in the foreground, I will use the fg command and specify the ID of that specific command, as shown below.

$ fg 2

Below is the behavior of this command.

Specific command in foreground
Specific command in the foreground

Kill command

Another option to prevent command execution is using the kill command; it will send the kill signal to the command in an attempt to close everything.

To kill any command execution, first, find the process ID using the below command.

$ ps aux | grep -i nano

In my case, nano is the command to be terminated. Replace nano with your command to find the process ID.

The picture below shows the behavior of this command.

Finding the process ID of the command
Seeing the process ID of the command

Once you find the process ID, use the below command and replace the process ID with yours.

$ kill -9 4926

Below is the behavior of this command.

Kill the process using process ID
Kill the process using the process ID
What will happen to the CP & MV command when interrupted?

Suppose you copy files from one directory to another. Then when you interrupt the process, the already copied files will be in the destination, and the leftover will be in the previous location.

Conclusion

I hope the mentioned method can help achieve process killing or suspension. Let me know if you have anything to ask in the comment section.

Leave a Reply