Linux run process command

Landoflinux

Any program that is executing is called a process. A process includes working storage, administrative information such as what files are in use, an environment for variable information and a Process Number. These process numbers are known as a «PID«. It is the job of the operating system’s kernel to create processes, assign CPU time and storage and also to clean up after tasks have completed. Processes can make calls to the operating system kernel to access files, devices or network resources. Process can be classed as a parent process or a child process. A parent process is the name given to a process that spawns a another process known as a child process.

The first process on the system has a «PID» of «1«. This signifies that this was the first process created at boot time. This program is also called the «init process«. The «init» process is responsible for booting the system.

Linux provides us with some very good tools for monitoring processes. The first command we are going to look at is the «ps«. This command reports a snapshot of the current system, displaying information about active processes. In its simplest form:

 $ ps PID TTY TIME CMD 55017 pts/0 00:00:00 bash 68479 pts/0 00:00:00 ps 

In the above output, we can see that I have a «bash» shell that is running and a single «ps» command. The columns «PID» identify the process number. The «TTY» gives the name of the terminal. The «TIME» is the amount of CPU time used by the process. The «CMS» is the name of the executing process. As with many Linux commands, there are lots of other parameters and flags that can be specified with the «ps» command to display specific information in a certain format.

Processes on a Linux system generally fall into one of the following categories:

Runnable (R) — This process may be assigned CPU.
Sleeping (S) — Sleeping, the process is waiting for an event.
Deep Sleep (D) — The process is waiting for an event that can not be disturbed.
Zombie (Z) — This is a process that has finished, however, its exit code has not been picked up by its parent. This means the process can not «die» it remains «undead«.

Читайте также:  Средства контроля целостности astra linux

Basic use of the ps command

Passing the PID to the process will display basic information.

In the example below, we passed a «PID» of «1«. As this is the first process started on a Linux system, we can see this displayed as «/sbin/init«

 $ ps 1 PID TTY STAT TIME COMMAND 1 ? Ss 0:00 /sbin/init splash 

To display information about your current «shell«, the «ps» command can be passed with no parameters.

 $ ps PID TTY TIME CMD 55017 pts/0 00:00:00 bash 115708 pts/0 00:00:00 ps $ ps 55017 PID TTY STAT TIME COMMAND 55017 pts/0 Ss 0:00 bash $ ps $$ PID TTY STAT TIME COMMAND 55017 pts/0 Ss 0:00 bash $ ps l $$ F UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND 0 1000 55017 55010 20 0 10632 4872 do_wai Ss pts/0 0:00 bash 

«$$» is a shell variable that displays the PID of your current shell.

In the above example, the UID represents the numerical «id» of the process owner. PPID is the Parent Process ID. PRI is the Priority of the process. The higher the number, the lower the process. VSZ is the processes size in memory (KiB). RSS is the processes current size in RAM (KiB). All process information is stored within the /proc filesystem. Many programs including «ps» use the information from here to display.

ps —help

By issuing the standard help command for «ps» we are able to see some of the many options that can be passed to the «ps» command:

For more examples of the «ps» command, click on the following link: ps command examples

 ********* simple selection ********* ********* selection by list ********* -A all processes -C by command name -N negate selection -G by real group ID (supports names) -a all w/ tty except session leaders -U by real user ID (supports names) -d all except session leaders -g by session OR by effective group name -e all processes -p by process ID T all processes on this terminal -s processes in the sessions given a all w/ tty, including other users -t by tty g OBSOLETE -- DO NOT USE -u by effective user ID (supports names) r only running processes U processes for specified users x processes w/o controlling ttys t by tty *********** output format ********** *********** long options *********** -o,o user-defined -f full --Group --User --pid --cols --ppid -j,j job control s signal --group --user --sid --rows --info -O,O preloaded -o v virtual memory --cumulative --format --deselect -l,l long u user-oriented --sort --tty --forest --version -F extra full X registers --heading --no-heading --context ********* misc options ********* -V,V show version L list format codes f ASCII art forest -m,m,-L,-T,H threads S children in sum -y change -l format -M,Z security data c true command name -c scheduling class -w,w wide output n numeric WCHAN,UID -H process hierarchy 

Nice command

Every Linux process has an associated priority that is used to determine how CPU time slices are shared amongst other processes. The higher the value of the «nice» number, the lower the priority. By default processes are started with a nice value of «0«. These nice settings can be seen when issuing the «ps» command.

Читайте также:  Use linux enterprise server

To start a process nicely, we could issue a command similar to nice -n 10 program.

By specifying a value of «10«, our program would allow other processes to have a higher access to CPU.

Renice command

As we have seen, the «nice» command is only used when starting a program. To change the nice value of a running process, we have to use a command called «renice«. To «renice» a process, issue the renice command followed by its new nice value and then its PID. Command issued: renice 10 3317.

In the example below, the program «openshot» was initially started with a default nice value of «0«. Once the program was running, the «renice» command was then issued:

 $ ps l 3317 F UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND 0 1000 3317 3229 20 0 379336 61960 poll_s Sl pts/1 0:04 /usr/bin/python /usr/bin/openshot $ renice 10 3317 3317 (process ID) old priority 0, new priority 10 $ ps l 3317 F UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND 0 1000 3317 3229 30 10 379336 61960 poll_s SNl pts/1 0:06 /usr/bin/python /usr/bin/openshot 

Linux «fg» and «bg» command

When you start a program under Linux, the program as a rule is started in the foreground. Normally this means that your console then becomes reserved for communication with that particular program. An easy way to demonstrate this would be to run the «xeyes» program.

Xeyes

As you can see from the screenshot image, the program is running in the foreground.

Читайте также:  Установка настройка linux suse

To run the same program this time in the background, we would have to start xeyes with the «&» ampersand parameter which signifies to run as a background process.

Command issued: xeyes &

Now control has been passed back to the console. We can use the «jobs» command to display the associated job number and process information for the backgrounded process. The «jobs» command can be run to show any jobs we have running.

 $ xeyes & [1] 582158 $ jobs [1]+ Running xeyes & 

If you do start a program from your terminal and forget to pass the «&» ampersand parameter, you can easily interrupt the program and send it into the background. To accomplish this we would
perform a «CTRL + Z» on the keyboard to signal the program to stop.

Now we can type «bg» to place the stopped process back into the background. Now if we issue the «jobs» command again, we can see the difference.

 $ xeyes ^Z [1]+ Stopped xeyes $ bg [1]+ xeyes & $ jobs [1]+ Running xeyes & 

jobs command

If we have more than one process running, we can refer to it by its job number. This is the number in the square brackets.

To bring a process from running in the background, we could specify its job number: fg %1 where «%1» corresponds to the job number. Jobs can also be killed by issuing the command kill %1. Make sure the job number matches the process you are trying to kill.

 $ jobs [1]- Running xeyes & [2]+ Running xeyes & $ kill %2 $ jobs [1]- Running xeyes & [2]+ Terminated xeyes $ fg %1 xeyes ^Z [1]+ Stopped xeyes $ bg [1]+ xeyes & $ jobs [1]+ Running xeyes & $ kill %1 $ jobs [1]+ Terminated xeyes $ jobs 

Источник

Оцените статью
Adblock
detector