OT: renice jobs running for more than 5 minutes

Rick Stevens ricks at alldigital.com
Mon Feb 22 18:58:53 UTC 2016


On 02/22/2016 10:50 AM, Ranjan Maitra wrote:
> On Mon, 22 Feb 2016 10:38:30 -0800 Rick Stevens <ricks at alldigital.com> wrote:
>
>> On 02/22/2016 10:19 AM, Ranjan Maitra wrote:
>>> Hi,
>>>
>>> I am running a fully updated F23 box but this question does not have much to do with Fedora itself, hence the designator and the disclaimer.
>>>
>>> I am wanting to run a script which will look at all the jobs that are running and renice all of them which have been on for more than five minutes. (Then I can run the script as a cron job as root and be done with automating the process.)
>>>
>>> Are there any suggestions as to how to go about this task efficiently? Actually, before I reinvent the wheel, are there any standard options that already exist and which would be more suitable for me than just to do everything from scratch.
>>
>> Use the "-o pid,etimes=" options of ps to get the elapsed time of
>> tasks in seconds. To get a full list, for example, as root:
>>
>> 	ps ax -o pid,uid,etimes=
>> 	...
>> 	21412     0     833
>> 	21499     0  631433
>> 	21541     0     773
>> 	21597  1000     769
>> 	21604  1000     769
>> 	21605  1000     769
>> 	21608  1000     769
>> 	21610  1000     769
>> 	21613  1000     769
>> 	21681  1000     769
>> 	21686  1000     769
>> 	21697  1000     769
>> 	21751  1000     742
>> 	...
>>
>> (run it as root so you can see ALL of the processes)
>>
>> As you can see, you get three columns: the first is the PID of the
>> task, the second is the EUID of the user running it, and the third is
>> the elapsed time.
>>
>> So, pull that data into a shell array, look for stuff that has the
>> second column equal to the user ID you're interested in and the third
>> column >= 300 seconds and renice the PID in the first column. Note that
>> I'd avoid renicing any tasks with UIDs < your lowest normal user ID
>> (typically 100) to keep from starving system tasks.
>>
>> Hope that helps!
>
> Yes, this absolutely helps, thanks!! But is there a 2-d array in bash (or do we do array of arrays)? (I am presuming that I need to store this in a 2-d array and then look at columns 2 and 3  and renice the PIDs in column 1.) Also, how does one assign the output of the ps to a 2-d array?

Uhm, no. You can simulate them using associative arrays, but it gets
rather hairy. You may want to try something like awk or PHP or Perl to
do this more easily.

----------------------------------------------------------------------
- Rick Stevens, Systems Engineer, AllDigital    ricks at alldigital.com -
- AIM/Skype: therps2        ICQ: 226437340           Yahoo: origrps2 -
-                                                                    -
-  Memory is the second thing to go, but I can't remember the first! -
----------------------------------------------------------------------


More information about the users mailing list