1

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?

Paolo
  • 2,161
  • 5
  • 25
  • 32

2 Answers2

5

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

Vorsprung
  • 32,923
  • 5
  • 39
  • 63
  • Thank you for your clear answer. Followup question: What exactly does a low priority mean, in practice? Does it mean it takes longer to perform or does it mean that it will simply be ignored? Say you have a simply little Raspberry Pi, and set up a cron job that mails "Hello world" once a week, but the task is set to nice -100. Does this mean it will never mail anything if it has other things it need to do? – Paolo Mar 31 '14 at 09:48
  • this page explains it better than I could http://www.ibm.com/developerworks/library/l-lpic1-v3-103-6/ – Vorsprung Mar 31 '14 at 10:07
2

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

Community
  • 1
  • 1
kerolasa
  • 401
  • 2
  • 5