Beginner’s Guide to the Linux `chrt` Command: 5 Practical Examples

Processes form the core of a running Linux system. When a program is executed and becomes a process, multiple attributes are set. At times, you might need to adjust these attributes at runtime. The command line tool chrt allows you to achieve this.

In this tutorial, we will discuss this utility with some easy-to-understand examples. Note that all examples have been tested on an Ubuntu 24.04 LTS machine.

Linux chrt Command

The chrt command enables you to manipulate the real-time attributes of a process. Here’s the syntax:

chrt [options] -p [prio] pid

The tool’s man page describes it as:

chrt sets or retrieves the real-time scheduling attributes of an existing pid, or runs a command with the given attributes. Both policy (one of SCHED_OTHER, SCHED_FIFO, SCHED_RR, SCHED_BATCH, or SCHED_IDLE) and priority can be set and retrieved. 
  The SCHED_BATCH policy is supported since Linux 2.6.16. The SCHED_IDLE policy is supported since Linux 2.6.23. The SCHED_RESET_ON_FORK flag for policies SCHED_RR and SCHED_FIFO is supported since Linux 2.6.31.

Below are some Q&A-styled examples that illustrate how to use the chrt command effectively.

Q1. How to Use the chrt Command?

If you’re interested in manipulating the scheduling policy or priority of the gedit process, the first step is to find the process ID using the pidof command.

pidof gedit

For instance, on my system, the command returned the PID ‘6063’. To retrieve the current scheduling policy and priority for the gedit process, execute:

chrt -p 6063

The output on my system was:

pid 6063's current scheduling policy: SCHED_OTHER
  pid 6063's current scheduling priority: 0

Q2. How to Display Minimum/Maximum Valid Priorities with chrt?

Use the -m command line option to display valid priorities.

chrt -m

On our system, the command produced:

SCHED_OTHER min/max priority: 0/0
  SCHED_FIFO min/max priority: 1/99
  SCHED_RR min/max priority: 1/99
  SCHED_BATCH min/max priority: 0/0
  SCHED_IDLE min/max priority: 0/0

Refer to these values when setting scheduling priorities.

Q3. How to Set a Scheduling Policy to SCHED_BATCH?

The -b command line option is used to set the scheduling policy to SCHED_BATCH.

chrt -b -p 0 6063

Check the scheduling policy for gedit again:

chrt -p 6063

The expected output should indicate the applied change:

pid 6063's current scheduling policy: SCHED_BATCH
  pid 6063's current scheduling priority: 0

Q4. How to Set a Scheduling Policy to SCHED_FIFO?

The -f command line option sets the policy to SCHED_FIFO.

chrt -f -p 20 6063

As before, you can verify using chrt -p. Note that this option may require root privileges.

Q5. How to Set a Scheduling Policy to SCHED_OTHER?

The -o command line option is employed for SCHED_OTHER.

chrt -o -p 0 6063

To confirm:

chrt -p 6063

The output will show:

pid 6063's current scheduling policy: SCHED_OTHER
  pid 6063's current scheduling priority: 0

Conclusion

While the chrt command might not be used daily, it is a valuable tool for system management or kernel scheduling tasks. Understanding its existence and functionality is beneficial. For more detailed information, refer to its man page.

FAQ Section

What is the main function of the chrt command?

The chrt command is used to set or retrieve the real-time scheduling attributes of a process or to execute a command with specific scheduling attributes.

Can I use chrt without root privileges?

Some options, such as setting the policy to SCHED_FIFO, might require root privileges.

Is chrt supported on all Linux distributions?

While chrt is generally available on most Linux distributions, the specific options available may vary depending on the kernel version.

Why would someone need to change a process’s scheduling policy?

Adjusting a process’s scheduling policy can be crucial for tasks requiring specific timing or prioritization over other processes.