LPI 101-500 – 103.5: Create, monitor and terminate processes Part 2
July 24, 2023

3. pkill, killall

Another way of sending signals to processes is the pkill command. It works in a similar way to kill. However, no process IDs have to be passed, but only the names of the corresponding processes. So theoretically pkill signal and than the name and not the process ID. The standard signal from pkill is also sick term. In order to stop the NDX processes we have to choose the following command pseudo pkill and then Andrew nix. However, I advise you to include the option x means that it only kills the program with the exact name. Without x, processes could possibly be terminated that were not actually intended. So with pgrab and the option l we get the exact name and here we see the name and it is just nginx.

So with pseudo pkill and the option x we can kill all endonix processes. Let’s try it out pgrap, l and Linux. And there is nothing to be found here. Now that means pkill killed two processes with a transfer of the name, which we called Anginax. The last possibility in our list is the command kill all. And here too the SIG term signal is standard or it is only called term. Here a list of signals known to kill ORC can also be found with kill all and L. And these are the corresponding signals that kill all understands kill all sends signals to all processes that have a certain property. For example a specific name or processes started by a specific user. Unfortunately it is difficult to simulate that.

But an example from practice suppose we have installed a program from a manufacturer x. When installing this program, a user was created who is, so to speak, the owner of this program, and only this user. And root of course has access to the corresponding directories. And only this user can start and stop the program. Suppose this program stops working. What to do? First you stop the program completely and in an orderly manner of course, and then restart it cleanly. So stop the program. So to stop the program there are often scripts, or you can simply use systemctl. Of course, if the program has now shut down, we check whether any processes of the corresponding user are still running.

We do this with PS and the auction f and u followed by the username. For example monitor. In this case we already discussed f in the PS video it stands for full format listing and u stands for user. So the command shows us all processes that are running by this user manual here. And these are all the processes that are running by manual. If the user manual were the corresponding user who was created especially to be responsible for this one program and only for this program and nothing else, then it could be that further processes are running here after the program has been shut down.

Processes that should no longer run after the software has been shut down, in this case, we still have to close all of these processes. Either we use Kill and then enter maybe 20 process IDs, or we use Pkill and enter maybe 20 different names one after the other. Or we use kill all with the u option for users. So pseudo kill all. You and than manual. And this command would ensure that all these processes belonging to the user manual are shut down.

4. nohup, screen, tmux

We have already learned how to list processes, how to find out the process ID or the status of the process also how to start, stop or end the process but you can do a little more with processes. All the processes that we have just listed with ps run in the background. That is, they run. While we can continue working in Bash at the same time, the Sleep command is ideal for clarifying what this means. Sleep simply lets a program run for a certain period of time. The program itself doesn’t actually do anything except pause. So let’s enter the following sleep 30 the program now runs for 30 seconds in the foreground which means that the program is running and we cannot do anything in the meantime and have to wait until the program has ended.

The program proves our bash. We don’t see a prompt here, we cannot do anything here. We have to wait 30 seconds and now the 30 seconds are over and I can now work with the Bash again. If I now enter Sleep 100, I have to wait 100 seconds and if I want to do something on the Bash, then I cannot because Sleep is currently occupying the foreground of the Bash we can also start the program in such a way that it is moved to the background. That means the program runs in the background and our Bash is not used so we can continue to work normally with the Jobs command we can check if we have any programs running in the background and no result. That means we don’t currently have a program in the background.

We now enter the following Sleep 1000 actually, we wanted to move the command together with an end sign in the background. So the end sign means that the Sleep 1000 command should take place in the background and not in the foreground but here we have forgotten the command is now running and we cannot work on our terminal for 1000 seconds. So how can we push Sleep 1000 into the background? Afterwards we simply send a signal to the process, namely Six Stop, which we have already got to know as a reminder. Six Stop ensures that a process is paused. Since we cannot make any entries in Bash at the moment, we can simply use the key combination control and that this is the equivalent to kill Six Stop.

So I press Control and the key that and we see stopped here sleep 1000 stopped just like we actually want it to be. The Sleep process was stopped and we can now make entries in the Bash again. If we want to run the program again without packing it into the background we simply enter fg for Foreground and then the program continues as before. So the program is running again and the Bash is blocked again for me so I cannot use it with Control that I stop it again. So I send the signal six stop again and instead fg for foreground I use now BG which stands for background BG and now the job is running in the background and we can continue to use Bash as normal.

So let’s start another sleep, maybe another sleep, maybe Sleep 2000. You can also stop this job with a key combination of control and C instead of that, which then sends the signal int which interrupts the terminal connection of the process and thus the entire process is stopped. Now the process is stopped since the process has now been completely stopped and not just paused, BG or fg, of course no longer have any effect. We can use the command jobs again and see which processes are still running in the background and we see there is only Sleep 1000 and not Sleep 2000.

To make this clear again, if we open another sleep, for example Sleep 500 sorry, I just cancel it again. I wanted to show you Sleep 500 with the end symbol at the end, which causes Sleep to be pushed into the background immediately and we can continue working with Bash and with Jobs. We see that we have now two sleep jobs in the background sleep 1000 and Sleep 500. These processes can also be seen via ps. So Psef grab Sleep and we can see here Sleep 1000, Sleep 500 and we have here there regarding process ids and that is working fine. So what happens to the processes that are in the background when you log out of the system? They are closed because they receive the sick Hub signal when you log out and thus end their work.

The sycup is an interruption or separation between the terminal and the program. The program then terminates in the administration world there are processes that you have to let run even if you lock yourself off the system. The ladder often happens automatically if you are locked in for a long time but otherwise no longer move. As an example, you want to run a data synchronization from one server to another overnight. Since it is several terabytes, the synchronization process has to run for several hours. And of course we don’t want to abort everything just because we want to go home after work. In such a case we use no Hub. The name might already say it no Hub. So no hub or no sycup? Command is accepted.

We have to execute the corresponding command with no Hub. We can simulate that with the Sleep command again. So no Hub, Sleep 1000 and the end symbol for using the background. So with the difference that we are now logging out of this running server and logging in again tomorrow, the process would be still alive. No ignoring input, pending output to know about this is a lock file and if you use Enter you can still do your work. So if we had forgotten the no hup the synchronization would not have continued. When we locked off from the system and we would have to start it again over the next day. But if we use nohab for reputation now if we use nohab, then we can log out of this system of a running server and the job still would be done.

Instead of no hub, you can also use the program screen. It may have to be installed first with Pseudoapt install Screen you can simply start the program with Screen. And here we have a small overview of licensing and copyrights and so on. And now it looks like nothing happened, but Screen is already running in the background. We can check that with ef grab Screen. And here you see the program is running. So Screen has the same function as no hub, but the syntax is different. You sign up for example via ssh to Server. ssh is the protocol with which I can log in from my computer via remote. So I’m logged into the server with my terminal. Now I start a program here and disconnect the ssh connection again this means that the program is also terminated afterwards.

This behavior can be prevented with no hub as just mentioned, or with Screen. In this case you log onto the server and first enter the word Screen as I’ve done now and with control A and then the hotkey C another window is opened we couldn’t see that here but trust me, another window is open. Now control A ensures that it accepts hot keys and in that case a hot key would be C which I have now pressed and which opens another window. In the new window we can for example run sleep again in the foreground so sleep 1000 you can see it is running in the foreground and I can no longer use bash here and with control a and then the space key, we can switch back and forth between the individual windows. So I switch back to the other window, where I can continue my work. If I want to switch back to the sleep window, I use control.

A and then space. Maybe there is another one. Yeah. Now I have three windows open so I can switch between them with Control A and D. D stands for detach, so disconnect. You can disconnect the virtual console, so control a and then D. And here you see detached from a similar tool like screen would be tmux, which can also be used to manage virtual terminals with multiple windows. tmux must also be installed first with pseudo apt. Install tmx, disturbing tmx. And you see another shell is started with the tmx command. We see that this shell looks a little different. We have this bar here below, and my name at the top is no longer colored. So tmax can also work with hot keys like Screen. Instead of using Screen with Control and A these are initiated with Control and B followed by the respective hot keys.

The hotkey C also opens a new window here. So I press control B and now C. And again it looks like nothing has happened, but a new window has opened in which we are right now. I’m entering here again Sleep 1000 and to switch back and forth between the windows, you use the Hotkey S for switch, so control B and then S. And we see here that we have two windows. We have window zero here, where nothing happens, and we have window one here. And we see we have executed an order. So we have sleep order here. And yes, there would certainly be a lot more to say about Sleep and tmux, but I wouldn’t expect any detailed question in the exam. Still, it doesn’t hurt to deal with it a little more extensively.

5. watch

This is going to be a very short lesson because I have forgotten one command in the last few lessons, and that’s the watch command. The watch command can periodically execute a particular command over and over. This is often done to review changes in expenses. The standard period the watch command works would be every 2 seconds. So if we now enter, for example, watch up time, then you can see now that something changes every 2 seconds. Here, see here 15 1618 sometimes here are changes too. You saw it here now. Yeah.

And that’s it. You can leave watch with control C. And if we now, for example, want to change the period, you can use the N option. So for example, for every 5 seconds, as an example, watch and five and then uptime again. And then you can see here that it changes every 5 seconds. That’s it. With control C, you can leave watch again. M yeah, I think there is actually nothing more to say about watch. And accordingly, the video would actually have ended.

Leave a Reply

How It Works

img
Step 1. Choose Exam
on ExamLabs
Download IT Exams Questions & Answers
img
Step 2. Open Exam with
Avanset Exam Simulator
Press here to download VCE Exam Simulator that simulates real exam environment
img
Step 3. Study
& Pass
IT Exams Anywhere, Anytime!