7

I have sshd with PID of 1957:

mohsen@debian:~$ ps ax -o pid,nice,pri,cmd |grep 1957
   1957  -2  21 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups

According to above, my nice number is -2 and pri number is 21.
According to /proc/1957, I can't find my nice number and pri number.

root@debian:~# cd /proc/1957
root@debian:/proc/1957# cat sched
sshd (1957, #threads: 1)
-------------------------------------------------------------------
se.exec_start                                :     211942985.934983
se.vruntime                                  :            31.031644
se.sum_exec_runtime                          :            23.385935
se.nr_migrations                             :                   14
nr_switches                                  :                   57
nr_voluntary_switches                        :                   18
nr_involuntary_switches                      :                   39
se.load.weight                               :              1624064
se.avg.load_sum                              :                 4366
se.avg.runnable_sum                          :              4470824
se.avg.util_sum                              :              1557504
se.avg.load_avg                              :                  147
se.avg.runnable_avg                          :                   95
se.avg.util_avg                              :                   33
se.avg.last_update_time                      :      211909716609024
se.avg.util_est                              :                   38
policy                                       :                    0
prio                                         :                  118
clock-delta                                  :                   89
mm->numa_scan_seq                            :                    0
numa_pages_migrated                          :                    0
numa_preferred_nid                           :                   -1
total_numa_faults                            :                    0
current_node=0, numa_group_id=0
numa_faults node=0 task_private=0 task_shared=0 group_private=0 group_shared=0

Where in /proc are the number pri and nice stored?

2 Answers 2

8

The priority and niceness values are given by fields 18 and 19 in /proc/<pid>/stat. See man 5 proc or man 5 proc_pid_stat for details.

awk '{print $18 " " $19}' /proc/1957/stat

or more robustly, assuming a 3.5 kernel or later (but not a future kernel adding fields…):

awk 'NF > 49 { print $(NF - 34) " " $(NF - 32) }' /proc/$$/stat

(on 3.5 kernels and later, stat has 52 fields; comm is truncated so no value of comm can result in that many fields).

2
  • 1
    (It is also settable by the process by writing to /proc/self/comm, so e.g. grep ' ' /proc/*/comm says I have ~25 processes – mostly parts of Firefox – that have a space in their comm name. The newline I got by accident, from an echo foo > /proc/self/comm, but I wouldn't rule it out. I don't know how other tools manage to parse the stat file, but it's probably best to parse it from the end, or something like that. Or if you're invoking a command anyway – reuse the same ps -o pri= $pid instead.) Commented Jun 5 at 7:38
  • Yes, the usual technique is to parse from the end but even that is tricky since comm can contain newlines. Mind you the length limit does reduce the amount of harm that can be done, so counting the number of fields should make things better. Commented Jun 5 at 7:42
4

While the raw values come from /proc/<pid>/stat, ps may change the values that you see.

For nice, if you use a different sort of scheduler, then the output may be '-' because the nice value is not used, see set_scheduler(2) for details on that.

Priority is problematic because there are different definitions on what the ranges mean. The pri field uses 39 - raw value. So you won't see 21 in the stat file but 18 because 39-18=21. You can see the raw figure with the priority field. This calculation changes when you are using real-time schedulers, but the field doesn't change.

Scheduler class for a process can be found with the cls field. TS means time-share, but the kernel calls it SCHED_NORMAL or SCHED_OTHER. Its the usual default class.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.