Tech Junkie Blog - Real World Tutorials, Happy Coding!: March 2022

Monday, March 28, 2022

 In Linux you can control the priority of a process with the commands nice and renice.  There are limitations if you are a non-root user. The nice and renice commands have the values in the range of -20 to +19. The higher the numbers the higher the priority, or the nicer the process is meaning the less CPU it would use, so it's kind of the opposite of what you are thinking.  So it's like nice guys finished last?

So if we run the sleep process again let's see what happens by default

sleep 1500&

[root@cent7 jhuynh]# sleep 1500&
[1] 3401
[root@cent7 jhuynh]# ps -l
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
4 S     0  3358  3315  0  80   0 - 68658 poll_s pts/0    00:00:00 sudo
4 S     0  3365  3358  0  80   0 - 58056 do_wai pts/0    00:00:00 su
4 S     0  3368  3365  0  80   0 - 29107 do_wai pts/0    00:00:00 bash
4 S     0  3401  3368  0  80   0 - 27014 hrtime pts/0    00:00:00 sleep
0 R     0  3402  3368  0  80   0 - 38332 -      pts/0    00:00:00 ps

As you can see the priority(PRI) is set to 80 percent by default

Now let's be a nice guy and assign the sleep process to the nicest value -19

[root@cent7 jhuynh]# nice -n 19 sleep 1500&
[2] 3438
[root@cent7 jhuynh]# ps -l
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
4 S     0  3358  3315  0  80   0 - 68658 poll_s pts/0    00:00:00 sudo
4 S     0  3365  3358  0  80   0 - 58056 do_wai pts/0    00:00:00 su
4 S     0  3368  3365  0  80   0 - 29107 do_wai pts/0    00:00:00 bash
4 S     0  3401  3368  0  80   0 - 27014 hrtime pts/0    00:00:00 sleep
4 S     0  3438  3368  0  99  19 - 27014 hrtime pts/0    00:00:00 sleep
0 R     0  3439  3368  0  80   0 - 38332 -      pts/0    00:00:00 ps

As you can see the new sleep process is set to priority 99 meaning only run the process if processes with the a lower number is ran first.  It's like being the 99th person in line.

If you run it at the highest nice value, not so nice. Let's see what happens

[root@cent7 jhuynh]# nice -n -20 sleep 1500&
[2] 3546
[root@cent7 jhuynh]# ps -l
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
4 S     0  3358  3315  0  80   0 - 68658 poll_s pts/0    00:00:00 sudo
4 S     0  3365  3358  0  80   0 - 58056 do_wai pts/0    00:00:00 su
4 S     0  3368  3365  0  80   0 - 29107 do_wai pts/0    00:00:00 bash
4 S     0  3543  3368  0  99  19 - 27014 hrtime pts/0    00:00:00 sleep
4 S     0  3546  3368  0  60 -20 - 27014 hrtime pts/0    00:00:00 sleep
0 R     0  3547  3368  0  80   0 - 38332 -      pts/0    00:00:00 ps

You can also reassign the priority of an existing process with renice command, with the renice command you have to specify the process id

[root@cent7 jhuynh]# renice -n 5 -p 3543
3543 (process ID) old priority 19, new priority 5
[root@cent7 jhuynh]# ps -l
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
4 S     0  3358  3315  0  80   0 - 68658 poll_s pts/0    00:00:00 sudo
4 S     0  3365  3358  0  80   0 - 58056 do_wai pts/0    00:00:00 su
4 S     0  3368  3365  0  80   0 - 29107 do_wai pts/0    00:00:00 bash
4 S     0  3543  3368  0  85   5 - 27014 hrtime pts/0    00:00:00 sleep
4 S     0  3546  3368  0  60 -20 - 27014 hrtime pts/0    00:00:00 sleep
0 R     0  3589  3368  0  80   0 - 38332 -      pts/0    00:00:00 ps

Linux was nice enough to tell you that the process has been changed from priority of 19 to 5, so now the process is assigned a priority value of 85.  Still the nicest priority!

The caveat on the renice command is that if you are not a user with root privileges, you cannot set a higher priority than the original priority of a process. 

If you are root you can also control other user's priority settings by editing the /etc/security/limits.conf. Priority is the last item or settings that you can set limits on, so move to the end of the line and type in the following for user limit, if you want group limits you just prefix it with the @ sign

#*               soft    core            0
#*               hard    rss             10000
#@student        hard    nproc           20
#@faculty        soft    nproc           20
#@faculty        hard    nproc           50
#ftp             hard    nproc           0
#@student        -       maxlogins       4

# End of file
techjunkie - priority 5

Now the next time techjunkie sets a priority he will be limited to priority number 5 nice value.  Since he is not a root user he cannot renice the process to be anything higher than 5.  Therefore he is a pretty nice guy.



Monday, March 21, 2022

 Linux allows you to run jobs in the background and in the foreground.  It accomplish this by identifying the processes as jobs and assigning the state to the jobs by numbers.

First let's create a process, we are just create a sleep job in the background, with the command sleep 1500& the & automatically put the process in the background.  If you observe the behavior you will noticed that the prompt is in your control right away because it's running in the background.




If you run the jobs command you see the process is running but it's running in the background.  However if you forgot to put the & at the end the job will have to be finished before you get your prompt back, the only way to get your prompt back before then is to type CTRL+Z , but that would also stop the process or job from running.  That's probably not what you wanted




You've gotten your prompt back but you've also stopped the process, that's probably not what you wanted to do.  You want to get your prompt and keep the job running at the same time.  So the solution is to use the bg command to run the job in the background






To bring the job into the foreground you just type fg



As you can see you lose the prompt once the job is put in the foreground because it has to finish running the job before you can get the prompt back. Press CTRL+Z again to interrupt the process

Now there's another way to put a process in the background, that is to specify the job number.  So if we want to put the second job that was stopped in the background again we can type bg 2 







You can do the same thing with the fg command.  Kill the sleep process with the command pkill sleep for cleanup



Monday, March 14, 2022

 Since searching for a process and killing process is such a common task there's are shortcut commands available for these tasks in form of pgrep and pkill.  As you suspect these commands are used for finding and killing processes respectively.

For example we can search for gnome processes like this instead of combining the ps command with the grep command

[jhuynh@cent7 ~]$ pgrep gnome
2292
2303
2521
2570
2822
5338
5345

Or to get more information you can use the command like  command below to get the full listing

[jhuynh@cent7 ~]$ ps -F -p $(pgrep gnome)

UID        PID  PPID  C    SZ   RSS PSR STIME TTY      STAT   TIME CMD
jhuynh    2292     1  0 79358  3836   0 06:53 ?        Sl     0:00 /usr/bin/gnome-keyring-daemon --daemonize --login
jhuynh    2303  2280  0 204743 11336  0 06:53 ?        Ssl    0:00 /usr/libexec/gnome-session-binary --session gnome-classic
jhuynh    2521  2303  1 756875 210812 0 06:53 ?        Sl     0:48 /usr/bin/gnome-shell
jhuynh    2570     1  0 162513 13756  0 06:54 ?        Sl     0:00 /usr/libexec/gnome-shell-calendar-server
jhuynh    2822  2303  0 278015 67620  0 06:54 ?        Sl     0:03 /usr/bin/gnome-software --gapplication-service
jhuynh    5338     1  0 167498 29064  0 07:43 ?        Sl     0:01 /usr/libexec/gnome-terminal-server
jhuynh    5345  5338  0  2134   724   0 07:43 ?        S      0:00 gnome-pty-helper

The pkill command works in similar fashion, so instead of killing the process by the number, you can just kill the bash process with this command pkill PID, one caveat is that it does not work like kill -9 so your usage may be limited.

Another useful command is the top command, this command will sort the processes that uses the most resources first by default. All you have to do is type top in the terminal









You sort it by other attributes as well such as memory, by default it's by CUP usage, to get to the other options type the f key












Select %MEM and type s to select it and press esc to sort the top screen by memory usage



Monday, March 7, 2022

 The kill command in Linux is a powerful command to kill a process. You usually see the kill command accompanied by the process ID however there's other ways you can use the kill command.  To get a list of how you can use the kill command, type kill -l

[root@cent7 jhuynh]# kill -l
 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL       5) SIGTRAP
 6) SIGABRT      7) SIGBUS       8) SIGFPE       9) SIGKILL     10) SIGUSR1
11) SIGSEGV     12) SIGUSR2     13) SIGPIPE     14) SIGALRM     15) SIGTERM
16) SIGSTKFLT   17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP
21) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU     25) SIGXFSZ
26) SIGVTALRM   27) SIGPROF     28) SIGWINCH    29) SIGIO       30) SIGPWR
31) SIGSYS      34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3
38) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7
58) SIGRTMAX-6  59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX    

There's one important thing that you have to remember as a standard user you can only kill are your own processes, so if you have two terminals open and one of them is not yours.  You can only kill the processes in your terminal or session.

Let's say you run the command ps to see the processes that are running, all you need to kill a process is type kill -9 PID

[root@cent7 jhuynh]# ps
  PID TTY          TIME CMD
 3526 pts/0    00:00:00 sudo
 3541 pts/0    00:00:00 su
 3544 pts/0    00:00:00 bash
 4787 pts/0    00:00:00 ps

So it's something like this kill -9 3544 to kill the bash process this will kill the terminal, alternatively you can type out the word that corresponds to -9 with the command kill -sigkill 3544



Search This Blog