Linux processes.
Linux processes, a clear article explaining a little on what Linux processes are, the fork/exec technique and commands associated with Linux processes
I want to assume we all have been in a situation whereby we want to run multiple commands in our terminal without opening another terminal window. This seems rather not possible, but in this article, I will be showing you how it is possible, why it is possible and a lot more cool stuff.
First off we need to understand what running a command or a process means in Linux? Google's definition:
A process is simply a running application, command, or any other program
This means that running a simple ls
command is a process in Linux. Each process is known to have a parent process id (PPID) and a process id (PID). The process id is used to identify a running process, but there is an exception to this, there is this one process that doesn't have a parent process id this is known as the init
process.
if you go to your terminal and type in ps -f 1
you should see this as your result
UID PID PPID C STIME TTY STAT TIME CMD
root 1 0 0 Sep18 ? Ss 0:02 /sbin/init splash
As you can see there is no PPID on this process why? Because it was the Linux kernel that started this process after booting up your system.
Now let's discuss what happens when you run a command for example ls
. When you run the ls
command the bash terminal gets forked, thereby creating another bash terminal you can't see, now we have two terminals, the initial terminal where the ls
command was run is the parent process and the second terminal that got created is the child process. The ls
command gets executed on the child process and it exits from there, displaying the output or result on the main terminal you're using. This is known as the fork/exec
technique in Linux.
If you run this command exec ls
it closes the terminal immediately because the bash terminal isn't forked it runs exec directly, the command ran but nowhere to display the output the bash terminal was exited out from.
Managing processes
To check the current processes in a shell run ps
. You should see an output like this
PID TTY TIME CMD
43301 pts/2 00:00:00 bash
48347 pts/2 00:00:00 ps
If you run ps -f
it shows you the hierarchy of the commands.
Now let's see how to run multiple commands in one bash terminal. Let's run sleep 360
for example, when we run it we see we can't really do anything in our terminal again except we kill the process using ctrl + c
, but what if we wanted to avoid this.
First we would have to stop the command from running using ctrl + z
, then run bg
to continue running the process in the background.
Let's test this.
somtochukwu@somtochukwu-HP-Laptop-14-fq0xxx:~$ sleep 360
^Z
[1]+ Stopped sleep 360
somtochukwu@somtochukwu-HP-Laptop-14-fq0xxx:~$ bg
[1]+ sleep 360 &
somtochukwu@somtochukwu-HP-Laptop-14-fq0xxx:~$
We can see that we started a process by running sleep 360
, then we stopped this process by running ctrl + z
, finally we continued running the process in the background using the bg
command.
Some of you might wonder how do we check if the command is still running, well just type in jobs
in your terminal and hit enter. Like this.
somtochukwu@somtochukwu-HP-Laptop-14-fq0xxx:~$ jobs
[1]+ Running sleep 360 &
somtochukwu@somtochukwu-HP-Laptop-14-fq0xxx:~$
It shows that the process is still running. What if we changed our mind to bring the process back to where we can see it running, just type in fg
which stands for foreground and that's all
somtochukwu@somtochukwu-HP-Laptop-14-fq0xxx:~$ fg
sleep 360
This applies to when we have one process running. Let's say we have three different processes running, it's not really difficult it's basically the same thing we have to do only slightly modified. For example, we have sleep 360
, sleep 350
and sleep 340
running. How do we get sleep 350
running in the background?.
All we have to do is run bg {id}
where id
is the id of the sleep 350
job that has been stopped. Here is an example.
somtochukwu@somtochukwu-HP-Laptop-14-fq0xxx:~$ sleep 360
^Z
[1]+ Stopped sleep 360
somtochukwu@somtochukwu-HP-Laptop-14-fq0xxx:~$ sleep 350
^Z
[2]+ Stopped sleep 350
somtochukwu@somtochukwu-HP-Laptop-14-fq0xxx:~$ sleep 340
^Z
[3]+ Stopped sleep 340
somtochukwu@somtochukwu-HP-Laptop-14-fq0xxx:~$ jobs
[1] Stopped sleep 360
[2]- Stopped sleep 350
[3]+ Stopped sleep 340
somtochukwu@somtochukwu-HP-Laptop-14-fq0xxx:~$ bg 2
[2]- sleep 350 &
somtochukwu@somtochukwu-HP-Laptop-14-fq0xxx:~$ jobs
[1]- Stopped sleep 360
[2] Running sleep 350 &
[3]+ Stopped sleep 340
So there you have it, this is only the tip of the iceberg compared to what Linux processes are all about. But this should give a little insight into how Linux processes work and to run Linux processes in the background and foreground in your terminal.
Thank you for taking your time to read. No knowledge is a waste. If you have any opinions or comments do leave them down in the comments section. Cheers!