Process management essentials for Devops
What you will Learn:
Difference between service and process
Exploring Processes (ps -a, ps -au, ps -aux)
kill the process
netstat (for listening services)
0.0.0.0 (listen on all interfaces)
Edit nginx configuration file
Difference between service and process
In the previous tutorial, we saw that nginx can be run as a background and foreground 'service'.
Now, what is the difference between service and process?
All services running will have one or more processes associated with them. So, irrespective of, whether service is running in the foreground or background, it will have a process.
Exploring Processes
Let us get back to our nginx webserver. Make sure that nginx service is running. Now, to look for all the processes that are running, we can use ‘ps aux’ command. You can look at the manual of ‘ps’ and see what a/u/x switches mean
a -> used to list all processes with a terminal
So notice the below output, ‘ps a’ shows all terminal processes currently running for a user. TTY column shows to which terminal the process is attached to. PID column shows the process id (every process will have a process id associated with it)
The output of ‘ps au’ shows some additional details. You can see USER information (under which the process is running), %CPU utilized, %Memory utilized, Virtual memory utilized (VSZ), Actual RAM usage or physical memory usage (RSS) etc
Next ‘ps aux’ will show all the processes that are currently running (for all the users including root user), see below.
All the services are running without a terminal (TTY) viz the services are running as background processes
Now ‘bash’ is a process and you can see that the ‘bash’ process is running on a terminal pts/0
Similarly, the command ‘ps aux’ that we executed just now too is running on a terminal
Let us grep the nginx service, notice that the 2 PIDs are running on background (without a terminal), the 3rd PID (grep nginx command) is running on a terminal. Please note that even ‘grep’ is a process and hence the command ‘grep nginx’ too contains ‘nginx’ and hence got printed in the output
If you want to exclude the grep process from output, you can pipe another command ‘grep -v grep’. Now if you notice below, we no more see the grep process in output
Also, PID 4409 is child process of 4408. Now, since the nginx processes are running, we can see the webserver running
kill the process
Now, if you try to kill the parent process id, you will get below error
The reason is, the owner of parent process is root user
So let us run it as sudo
Refresh the age, notice that service is now down
Also see below, none of the nginx processes are running
Let us start the service again
netstat
We can use ‘netstat -tulnp’ command to find out the ports the various services are listening to. Nginx listens on port number 80 because it’s an http web server
Similarly ‘ssh’ process is listening on port 22, see below
0.0.0.0 (listen on all interfaces)
See below, 0.0.0.0 means the nginx service is listening on all network interfaces
When we execute ‘ip a’ command, we can see 2 network interfaces:
loopback interface 127.0.0.1 (ubuntu localhost)
Ethernet network interface 192.168.0.106 (bridged from our host computer)
So 0.0.0.0 means, whatever networking interface the system has, this nginx service should listen on all of them. So the service should respond if you try to access it using 127.0.0.1 ip address or 192.168.0.106 ip address.
The loopback interface 127.0.0.1 is valid ONLY on same system (here, ubuntu). It has no value outside of the system. So as you notice below, we are trying to access the nginx service on ubuntu localhost using 127.0.0.1 ip address and we are getting the response
However see below. As expected, we are NOT getting a response when we try to access the service on 127.0.0.1 outside ubuntu system (from our windows host machine)
However, Ethernet n/w interface ip address 192.168.0.106 can be accessed on ubuntu as well as on windows host, see below
Edit nginx configuration file
To understand the above concept better, let us open the nginx config file in edit mode. The config file location is:
/etc/nginx/sites-enabled/default
Hit Enter, the editor window opens. The highlighted line seen below means that the nginx service is listening at default port number 80 (for all the network interfaces)
Bring the cursor to the line
Press key ‘i’ to insert the text, type # to comment the above line
Press escape key to enter into read-only mode.
Bring the cursor to the below line
Next using the right arrow key, move the cursor and bring it on 80
Press key ‘i’ to enter into write mode. Type 127.0.0.1:
So we are telling nginx to listen to only loopback interface 127.0.0.1
Save the file by performing below-
Esc key followed by colon : followed by wq and Hit Enter
The file contents are saved as you can see below
We can execute ‘sudo nginx -t’ to check if there is any syntax error
Let us restart nginx ‘sudo systemctl restart nginx’
Now when we execute ‘sudo netstat -tulnp | grep nginx’, we see that nginx service is listening at loopback interface 127.0.0.1 instead of 0.0.0.0
So what it means is that, nginx will no more listen on Ethernet ip address 192.168.0.106. It will only listen to loopback
See below
Also see below that nginx is not listening on ethernet
So this is how we can change the config file if needed. Let us edit the file and bring it back to original form.
Open the file again in editor
To uncomment the below line, using down arrow key, bring the cursor on # and type x to delete #
Using arrow key, bring the cursor one by one on 127.0.0.1: and type x to delete the numbers
Save the file and make sure that changes are saved
Run below command to ensure syntax is perfect
Restart nginx
The below command now shows that nginx is again listening on all n/w interfaces
Refresh browser, see below
Similarly
Thank you for reading!