ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hey guys! How are you? Im a new member to Linux Questions, but not new to Linux, but.. new to bash scripting, lol.
I run a small hosting company, with a few clients, but the few clients that I do have, use all my CPU and MEM, thus I wanted to find a way to suspend them when they use too much. I made a custom script out of PHP, and its running as a cron on root permissions, but I just dont feel comfortable doing this with PHP. Its just... weird to me. Linux administration shouldn't be done with PHP.
Here is my PHP code to do what I am trying to do.
PHP Code:
function checkPs() //================================================================================== { global $cpu_column; global $usr_column; global $mem_column; global $pid_column; global $cmd_column;
echo "Populating User List .."; $usr = shell_exec("ps aux |awk {'print $1'}"); $usr_column = split("\n", $usr); echo " \033[32m[ OK ]\033[30m\r\n";
echo "Populating CPU List .."; $cpu = shell_exec("ps aux |awk {'print $3'}"); $cpu_column = split("\n", $cpu); echo " \033[32m[ OK ]\033[30m\r\n";
echo "Populating MEM List .."; $mem = shell_exec("ps aux |awk {'print $4'}"); $mem_column = split("\n", $mem); echo " \033[32m[ OK ]\033[30m\r\n";
echo "Populating CMD List .."; $cmd = shell_exec("ps aux |awk {'print $11'}"); $cmd_column = split("\n", $cmd); echo " \033[32m[ OK ]\033[30m\r\n";
echo "Populating PID List .."; $pid = shell_exec("ps aux |awk {'print $2'}"); $pid_column = split("\n", $pid); echo " \033[32m[ OK ]\033[30m\r\n";
That way I can simply match the array key, and check the value of memory and cpu, if its over the limit, I get $user[$key] and run the suspend script.
However, How would I do such a thing in bash? I know I can split
ps aux |awk {'print $2'} into rows, but how would I grab the array key and make it suspend the user?
OR! Do you think that PHP Is a perfectly fine way to run this, and that I shouldnt fix what isnt broken. I have ran this script on my server for some time, and have not had a single issue with it yet, and it has saved my butt a few times, users have gotten cron jobs up to 95%, and I kill the process and send them an email that there process was killing the CPU usage, and that the script was being executed at $time, using $memory, and was killed, and its all automated.
I have big plans for this, and I know how to do it all in PHP, but if I want to either give it away, or sell it, I am not sure that people would want PHP running on Cron scripts to help the server.
WHat would you rather have?
1) Well coded PHP Script running on root level that works well on cron
2) Bash scripting on daemon
3) PHP script on daemon (Is that even possible?)
Not to be throwing a wet blanket on your party, but you really need to rethink the way you are doing this (not the php/bash issue) but you are running 5 different instances of ps. There is nothing saying that all the processes from the first instance will still be running during the 5th instance. Therefore the $pid[x] may REALLY belong to $cpu[x-1] and you are getting your statistics and killing the wrong process. Furthermore, you could be pointing to the wrong user as the offending cpu hog.
Here is a better script in bash (yes, you really should be doing this in bash as your php code is just calling bash shells which call ps and awk anyway).
Code:
#!/bin/bash
function checkPs() {
echo -n "Populating Lists .."
# Get all the pertinent data at once
PS=`ps aux | awk '{ print $1 "|" $3 "|" $4 "|" $11 "|" $2 }'`
COUNT=0
for PROCESS in $PS ; do
#This AWK command outputs a short bash "program" which can be called w/ eval to set the right variables
eval `echo $PROCESS | awk -F\| '{printf ("USR=%s;CPU=%s;MEM=%s;CMD=%s;PID=%s", $1, $2, $3, $4, $5)}'`
#Now put the variables into arrays
USRS[${COUNT}]=$USR
CPUS[${COUNT}]=$CPU
MEMS[${COUNT}]=$MEM
CMDS[${COUNT}]=$CMD
PIDS[${COUNT}]=$PID
let COUNT=COUNT+1
done
echo -e " \033[32m[ OK ]\033[0m"
}
checkPs
# To access the array values do like the following
echo ${PIDS[50]} ${USRS[50]} ${CMDS[50]} ${CPUS[50]} ${MEMS[50]}
All this being said, if a process is taking up a lot of CPU usage, it doesn't necessarily need to be killed. Only if the server is being effected. If a process is running and no other processes need the CPU, it will take all the CPU it can get until it is done. You should only kill a high CPU using process if the system is actually being affected.
HTH
Forrest
Last edited by forrestt; 12-13-2008 at 12:15 AM.
Reason: Changed list to lists in the first echo statement
Not to be throwing a wet blanket on your party, but you really need to rethink the way you are doing this (not the php/bash issue) but you are running 5 different instances of ps. There is nothing saying that all the processes from the first instance will still be running during the 5th instance. Therefore the $pid[x] may REALLY belong to $cpu[x-1] and you are getting your statistics and killing the wrong process. Furthermore, you could be pointing to the wrong user as the offending cpu hog.
Here is a better script in bash (yes, you really should be doing this in bash as your php code is just calling bash shells which call ps and awk anyway).
Code:
#!/bin/bash
function checkPs() {
echo -n "Populating Lists .."
# Get all the pertinent data at once
PS=`ps aux | awk '{ print $1 "|" $3 "|" $4 "|" $11 "|" $2 }'`
COUNT=0
for PROCESS in $PS ; do
#This AWK command outputs a short bash "program" which can be called w/ eval to set the right variables
eval `echo $PROCESS | awk -F\| '{printf ("USR=%s;CPU=%s;MEM=%s;CMD=%s;PID=%s", $1, $2, $3, $4, $5)}'`
#Now put the variables into arrays
USRS[${COUNT}]=$USR
CPUS[${COUNT}]=$CPU
MEMS[${COUNT}]=$MEM
CMDS[${COUNT}]=$CMD
PIDS[${COUNT}]=$PID
let COUNT=COUNT+1
done
echo -e " \033[32m[ OK ]\033[0m"
}
checkPs
# To access the array values do like the following
echo ${PIDS[50]} ${USRS[50]} ${CMDS[50]} ${CPUS[50]} ${MEMS[50]}
All this being said, if a process is taking up a lot of CPU usage, it doesn't necessarily need to be killed. Only if the server is being effected. If a process is running and no other processes need the CPU, it will take all the CPU it can get until it is done. You should only kill a high CPU using process if the system is actually being affected.
HTH
Forrest
I tried to execute that script
Quote:
./test: line 13: called: command not found
awk: cmd. line:2: ("USR=%s;CPU=%s;MEM=%s;CMD=%s;PID=%s", $1, $2, $3, $4, $5)}
awk: cmd. line:2: ^ syntax error
./test: line 13: called: command not found
awk: cmd. line:2: ("USR=%s;CPU=%s;MEM=%s;CMD=%s;PID=%s", $1, $2, $3, $4, $5)}
awk: cmd. line:2:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.