Can I prioritize Bash scripts in Linux, and do children of these scripts inherit the priority?

StackOverflow https://stackoverflow.com/questions/22748873

  •  24-06-2023
  •  | 
  •  

I have a script playing various movie clips on a little computer using a video player. It's smooth. However, whenever a cron job is run where big files are to be downloaded, the video feed starts flickering. I believe this is due to the read/write needed to be done while downloading and playing at the same time.

My questions are:

Can I prioritize a task in Linux?

Can the prioritized task be a Bash script?

If yes, will scripts and programs that run from that prioritized script inherit the priority?

有帮助吗?

解决方案

you can set process priority with the nice command when launching a process or with the renice command on existing processes

Any normal user process can have a lower priority. Only root owned processes can have a higher priority

To launch a lower priority process use a command like

nice -10 mycommand.sh

In this case the priority is 10 which is a less favourable priority than normal, the process will have less priority on the system than a command not started with nice

Sub processes have the same priority as their parent by default

其他提示

The other answer already talked about scheduling priority, and it surely answers to exact question. That said I think your word prioritize is been used a bit unusual way and you actually mean how can make the batch download not to effect other networking. Answer to that question is traffic controlling.

Here is an example traffic shaping script (from funtoo.org).

modemif=eth4
iptables -t mangle -A POSTROUTING -o $modemif -p tcp -m tos --tos Minimize-Delay -j CLASSIFY --set-class 1:10
iptables -t mangle -A POSTROUTING -o $modemif -p tcp --dport 53 -j CLASSIFY --set-class 1:10
iptables -t mangle -A POSTROUTING -o $modemif -p tcp --dport 80 -j CLASSIFY --set-class 1:10
iptables -t mangle -A POSTROUTING -o $modemif -p tcp --dport 443 -j CLASSIFY --set-class 1:10
tc qdisc add dev $modemif root handle 1: htb default 12
tc class add dev $modemif parent 1: classid 1:1 htb rate 1500kbit ceil 1500kbit burst 10k
tc class add dev $modemif parent 1:1 classid 1:10 htb rate 700kbit ceil 1500kbit prio 1 burst 10k
tc class add dev $modemif parent 1:1 classid 1:12 htb rate 800kbit ceil 800kbit prio 2
tc filter add dev $modemif protocol ip parent 1:0 prio 1 u32 match ip protocol 0x11 0xff flowid 1:10
tc qdisc add dev $modemif parent 1:10 handle 20: sfq perturb 10
tc qdisc add dev $modemif parent 1:12 handle 30: sfq perturb 10

What you need to do is to find out criteria[1] of what sort of traffic you want to de-prioritize by granting it only limited bandwidth. These setting should probably live somewhere in your system start up configurations instead of the script you run.

[1] an address where you download from, or protocol, cgroups which processes are downloading, etc.

See also How to priotize packets using tc and cgroups

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top