Linux is amazing, but only when you know what you are doing, as well as by learning about the Stone of Miles you could achieve along. As compared to Windows users, they are significantly more established in terms of technical knowledge. Why is that? Thanks to Mr. Terminal. Using the command line for tasks might seem boring to many when clicking buttons with the mouse is widespread. But once you get the hang of it, you’d hardly wish to use the mouse for everything else you cannot do with just the keyboard inside a terminal.
Meanwhile for Linux users, there is a keyboard shortcut I’d like to share a few words for. It’s Ctrl + Z, used in Linux terminals. If you have ever used TTY terminals, or even if it’s your home for all things computing (if you’re that server guy), it could very much be one of the coolest tricks you learn. So hang in there and don’t fall off your chair – as I tell you about Ctrl + Z!
(Haha, I seemed to have exaggerated a bit too much right there 🙂 Nevertheless, whether or not you need Ctrl + Z yourself, it’s best you know what it is and why you may even use it.
What are signals used for?
Signals are commonly used in Linux terminals to suspend, terminate and interfere in terminal processes with a limited form of communication between a process and the terminal. Basically, while you’re running a program or a command in your Linux terminal, a signal will either stop the process, entirely kill it or pause it for a certain time. This type of communication is also known as Job Control. This is carried out by means of using keyboard shortcuts, and or external commands from adjacent terminals to one another, to trigger the desired event. Signals can also be executed by terminal programs themselves to carry out specific jobs on demand.
There is quite a vast array of signals available in Linux, some of the common ones being SIGINT, SIGSTP and SIGQUIT.
Purpose of TSTP Signal, for Ctrl + Z
TSTP (“terminal stop”) comes from the SIGSTP signal in UNIX systems. When you press Ctrl + z inside of a linux terminal while a program or a process is running, you will find that it shows you a message that includes the STOPPED text:
What’s happening here is that when you press Ctrl + Z, it sends a TSTP signal to the terminal, asking it to temporarily pause the running process. This is widely helpful when you TTY terminals (Virtual Terminals), and don’t have a terminal multiplexer, or more so if you’re using an SSH server. Let’s say, you’re running a terminal program on a remote SSH server, and don’t have a terminal multiplexer installed. In such cases, you’ll be able to pause the running process that is in the foreground, and send it to the background to be able to use another command in the meantime. You can later unpause the program by means of using a terminal command, which will be discussed in the sections that follow.
SIGCONT to resume process after Ctrl + Z
So you’ve just sent a program to the background, and you’ve done some wizardry mixing all types of potions and casting all sorts of terminal mantras to your heart’s content. But once you’re done with all that mess, how do you get back to the process which you had paused? Will it never come out of the forgotten abyss of background? Well this is exactly where the SIGCONT terminal spell (signal) comes into play.
To bring the program from the background to the foreground once again, you’d type fg in your terminal as a command to tell your gaga staff (terminal) that you want your forgotten process back in front of your eyes. The SIGCONT signal stands for “Signal: Continue”, which essentially tells your terminal to resume the process you had paused using Ctrl + z.
Ctrl + Z in action inside a Linux Terminal
To further help you clarify the the overall process of using Ctrl + z as mentioned in the previous two sections, try looking at the following example:
Here, I’m running a terminal program called asciiquarium, which is quite relaxing and you should install that too. While I was rocking on my chair looking at those adorable fish going places, I suddenly wanted to see how long my computer has been turned on, by checking out the uptime. To achieve that, I’ll first need to quit the program, then type neofetch to look at the uptime, and then once again type asciiquarium to start the program once again.
How inconvenient is that! Typing the whole command once again is pure pain, and on the other hand, the fish I was previously looking at may have gone! This is why, well if not the same it is for you, you should at least get the idea why someone might wish to use Ctrl + Z. Back in the early days, there were nothing like Terminal Multiplexers or even the choice of several virtual terminals at a time to execute different commands together. This is why such commands were created. Even though the use of Ctrl + Z is quite limited as of now, it used to mean the only way of multitasking some time ago.
Also, there are other signals that are still widely used today, which we may not consciously know about.
Ctrl + C vs Ctrl + Z | What to use and when
Many people tend to confuse the two shortcuts doing the same thing in the Terminal. Trust me, it does not. Ctrl + Z is used to pause the program temporarily, which can again be called upon by using the fg command. The process will resume from the very same state where it left off.
On the other side, Ctrl + C terminates the process entirely, which won’t allow you to resume the process once you’ve quit from it. You’ll need to start the program from the beginning, and there is no command like fg for Ctrl + C to follow for restarting the program, or do anything of that sort.
Linux Terminal Wizardry is quite fun indeed, but sometimes too much can lead to disaster. Moreover, if you don’t really know what you are doing in the first place, you can either face one of these – a terrible heart attack if the task you were carrying out determined your entire career, or the breakage of your system if you ran a command that removes everything from your disk which you didn’t even know of, and had pasted it from your friend’s message who suggested that it’ll make your system faster (such mean friends…). Yes, that is possible in Linux, if not elsewhere. However, if all this time you thought Ctrl + C and Ctrl + Z both terminated a program, and now are mourning the death of an important terminal process you were running, breathe peace. You can bring your process back by simply pressing fg.