chrt command in Linux with examples
'chrt' command in Linux is known for manipulating the real-time attributes of a process. It sets or retrieves the real-time scheduling attributes of an existing PID, or runs the command with the given attributes. 'chrt' can help optimize process management in a Linux system, especially for applications that require real-time performance.
Syntax
$ chrt [options] priority command [argument ...]
$ chrt [options] -p [priority] pid
where,
- priority: The scheduling priority for the process (if applicable).
- command: The command to be executed with the specified scheduling attributes.
- pid: The process ID of the existing process you want to manipulate.
Note: The 'chrt' command typically requires root privileges to modify scheduling policies for existing processes.
Common Options with 'chrt' command
Option | Description |
---|---|
-b, --batch | Sets the policy to SCHED_BATCH. |
-d, --deadline | Sets the policy to SCHED_DEADLINE. |
-f, --fifo | Sets the policy to SCHED_FIFO. |
-i, --idle | Sets the policy to SCHED_IDLE. |
-o, --other | Sets the policy to SCHED_OTHER (default time-sharing). |
-r, --rr | Sets the policy to SCHED_RR (default real-time policy). |
-p, --pid | Operates on an existing process with the specified pid. |
-a, --all-tasks | Operates on all tasks (threads) for a given pid. |
-m, --max | Displays the minimum and maximum valid priorities for the policies. |
-v, --verbose | Displays status information. |
-h, --help | Displays help information and exits. |
--version | Displays version information and exits |
Scheduling Options with 'chrt' Command
- SCHED_BATCH : Use Scheduling batch processes algorithm.
- SCHED_FIFO : Uses First In-First Out scheduling algorithm. This scheduling method is used on Batch-Systems, it is NON-PREEMPTIVE. It implements just one queue which holds the tasks in the order they come in.
- SCHED_IDLE: Used for running very low priority background jobs.
- SCHED_OTHER: Uses Default Linux time-sharing scheduling algorithm or simply the standard round-robin time-sharing policy.
- SCHED_RR Uses Round Robin scheduling algorithm and is used as the default algorithm if not specified. It is an algorithm used for PREEMPTIVE scheduling.
chrt Command Examples in Linux
Here are some examples of how to use chrt to manage real-time scheduling:
1. To see the current scheduling policy:
First we have to make a process. Let's take a example, Firefox is running and to find its's pid we run the following command:
$ pidof -s firefox
In my case the pid is 5794, here is the image

Now to retrieve the current scheduling policy and priority for the firefox process, use chrt in the following way:
$ chrt -p 5794

2. To change the scheduling policy to SCHED_FIFO:
From the above example, the scheduling policy of firefox process is set as SCHED_OTHER. Now to change the policy to SCHED_FIFO we can use the following command:
$ sudo chrt -f -p 5794

3. To change the scheduling policy SCHED_BATCH:
From the above example, the scheduling policy of the firefox process is set as SCHED_FIFO. Now to change the policy to SCHED_BATCH we can use the following command:
$ sudo chrt -b -p 5794

4. To see the maximum and minimum valid priorities:
This can be done using the -m command line option mentioned in the policy of chrt.
$ chrt -m

Conclusion
The 'chrt' command in Linux is a powerful tool for controlling the scheduling attributes of processes. By understanding how to use it effectively, you can optimize system performance for real-time tasks or background jobs, depending on your specific needs. 'chrt' allows you to fine-tune how these processes are scheduled, ensuring that critical tasks are prioritized appropriately while preventing less important tasks from consuming excessive system resources.