Managing processes on Linux

DON'T post new tutorials here! Please use the "Pending Submissions" board so the staff can review them first.
Post Reply
User avatar
bad_brain
Site Owner
Site Owner
Posts: 11636
Joined: 06 Apr 2005, 16:00
18
Location: In your eye floaters.
Contact:

Managing processes on Linux

Post by bad_brain »

In this little tutorial I will explain how to manage processes on Linux, how to start and stop them, how to end a process if the desktop is frozen, and of course how to get info about running processes.


---Start a process---
Different from Windows an executable don't need a special file extension (like .exe) on Linux/Unix, so the extension can be anything or even no extension at all. But it is a good practice to give Bash scripts a .sh extension for example, so executables can be easier identified, especially on a multi-user system and/or if colors are not enabled in the terminal (executables are usually displayed in a yellow-ish color).
So, how to start a process? Simple, you just have to enter the name of the executable, if you are not in the same irectory as the executable you have to use the full or relative path like:

Code: Select all

/usr/local/myprocess
If you are in the same directory as the executable you don't need to enter the path, but you have to add ./ before the executable name, like:

Code: Select all

./myprocess
Why ./ ? The intention behind this is to make sure a user REALLY wants to run an executable, if no ./ would be needed it would be easy to trick a user, so it is a security feature.

But of course there is a way to start executables just by entering the name, for this the executable must be located inside one of the paths where the shell interpreter looks for them.
The paths are stored in the $PATH variable:

Code: Select all

serv:~# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11
So if you place the executable named "myprocess" in /usr/local/bin for example you can start it with:

Code: Select all

myprocess
Ok, all the above not only applies to processes, it applies generally to all executables, so let's get a little more specific:
First, let's start your process again, asuming it's in $PATH:

Code: Select all

myprocess
Ok, runs fine, BUT the screen is blocked now because the process runs in the foreground. Of course you can open a 2nd terminal now, but this is not really comfy.
So, let's start the process again, but this time as background process:

Code: Select all

myprocess &
Simple, isn't it? Adding a & does the trick!
Hm, wait, when I close the terminal the process ends too....
Ok, also no problem, let's start the process again as background process that will keep running even when you log out:

Code: Select all

nohup myprocess &
"nohup" means "no hangup", so the process will not stop when the user that started it logs out.
Another problem that might appear is that the process prints its output on the screen, if the output is kinda useful and you want to log it for later checks you can easily write it to a file:

Code: Select all

myprocess > /mylogfile &
But if the output is kinda useless you can send it to "nowhereland":

Code: Select all

myprocess > /dev/null &

---Stop a process---
Well, how to stop a process kinda depends on you, I am not talking about violently kill a process now, I mean how to stop it regularly. The best is of course when you have implemented this option in your code already or you use an external start/stop script (this applies to permanent processes like servers for example). But a process of course also simply end by itself when it is done with its job (like a backup script for example).


---To stop a process that "hangs"---
If there is an error in the code for example it can happen that the process doesn't end like it should, this can lead to various problems from a high load on a server system to freezing the complete desktop on a desktop system.
What you NEVER should do is to press the "reset" button, this is really only the LAST option because it can lead to a loss of the data that was processed at that moment or it can even corrupt a whole database.
Let's start with the most simple case when you run a process in the shell and it hangs so the terminal is blocked, pressing CTRL+C should end the process.
Another way is the "kill" command, if the terminal is not blocked and/or you can open a 2nd one you can stop the process with:

Code: Select all

kill <PID>
How do I know the PID of the process?
There are different ways, you can use the "top" command or "ps -A", if you know the name of the process you can also use the "pdiof" command:

Code: Select all

pidof myprocess
Notice that there are different kinds of the "kill" command, or better said the "kill" command can send different signals to the process. The default signal is 15 which "asks the process to please stop" (so "kill 725" equals "kill -15 725"), this way the process shuts down regularly and there is no danger of a data loss. If this doesn't work you can be a little more brutal by sending the 9 signal:

Code: Select all

kill -9 725
This ends the processe with the PID 725 without taking care of the data that is maybe processed by that process at the moment, so this can result in a data loss (of that process of course, NOT of your whole system).
Now let's imagine you started your process on your desktop system and "uh-oh...desktop frozen!"...still no need to press "reset", even if your mouse and keyboard seem to have no effect.
By pressing CTRL+Alt+F1 you have a good chance to switch into textmode where you can end the process that caused the hang with the methods mentioned above.



---Getting info about processes---
As mentioned about (process PID) there are different ways to get informations about processes, the most basic one is:

Code: Select all

ps -A
You can see the PID, in how many terminals the process is visible/used (TTY), for how long the process is running, and of course the process name.
If you want to have more detailed info "ps aux" is a very useful command, it display information like the process owner, PID, CPU and RAM usage, etc., a very valuable info is the actual command that started the process.
If you want to have info in realtime the "top" command is very useful, it also displays CPU and RAM usage, so it is very good to monitor the system if you suspect a process to be a resource hog.
Both ps and top have numberous options, so I recommend to check the manual pages.


---Be nice!---
Um, what? No joke, you can tell a process to be nice! Ok, actually you can give a process a priority, which means a processe with higher priority gets time (better said CPU runtime) to finish first before the process with lower priority is allowed to use the resources.
And the command for this is "nice". the highest priority is -20, the lowest priority is 19.
So let's start out process with the highest priority:

Code: Select all

nice -20 myprocess
It is better not to be "too nice" to processes, and I wouldn't use a higher priority than -10 for a regular process, because higher priorities are only used for REALLY important kernel processes for example that are always more important than a service or a script.
The "nice" command is only used to start a new process, if the process is already running and you want to give it a different priority you will have to use the "renice" command.
So let's give our already running process (with the PID 725) a different priority (-5) because -20 was a little too much:

Code: Select all

renice -5 725
Notice that you have to use the PID for renice, and not the process name.



Alright, that's all for now, I hope you find this little tut useful...:)

Hex00010
forum buddy
forum buddy
Posts: 17
Joined: 22 Nov 2010, 23:43
13

Re: Managing processes on Linux

Post by Hex00010 »

As per your statement
Different from Windows an executable don't need a special file extension (like .exe) on Linux/Unix,

I forgot the word that defines that so just bare with me im bore so thought id post this maybe someone might find it interesting


The reason why Linux reads from files instead of a .exe is because the whole entire system is files LINUX = FILES FILES = LINUX lol


You can name any file in linux anything you want for example

Code: Select all

root@mountainDew~Touch  HexLOL.superman
root@mountainDew~pico HexLOL.superman
         echo ' This is Hex00010 '
However if you will be programing in lets say bash , ksh , sh it is always good to name the files the appropriate way


Linux will always read from files no matter what which to me is a cool thing i guess rather than it having to a MS where as it is a .exe


I know this may not be helpful or it is a completely off topic but im bored so i thought id post this lol

User avatar
John_W
forum buddy
forum buddy
Posts: 24
Joined: 02 May 2010, 16:00
13
Contact:

Re: Managing processes on Linux

Post by John_W »

Hex00010, you aren't using Linux terminals often, are you?
1) Don't login as root if not necessary.
2) File names are case sensitive. "Touch" doesn't exist in PATH, however "touch" does.
3) You cannot execute the file named HexLOL.superman you created with touch (it isn't even necessary to touch before pico/nano/vi/vim/emacs it) without giving permissions: chmod +x HexLOL.superman
4) Be careful with spaces, they can be misleading if they are quoted they will be used in the file name, if not (as in the beginning of your "Touch" line) they are ignored.

Another point:
The reason why Linux reads from files instead of a .exe is because the whole entire system is files LINUX = FILES FILES = LINUX lol
On Windows you have files too (e.g. the registry can be found in C:\Windows\system32\config), the Windows kernel is also a file...
File extensions can be used on Linux as well, but are not necessary due to the fact that Linux uses magic bytes and other means of file type detection.
Windows doesn't have this feature, it doesn't even have a well designed file tree...

Post Reply