Problems with a simple script.
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Problems with a simple script.




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Problems with a simple script.  
Materialised


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:36 PM

Hello everyone, i'm trying to write a script that will run from a cron
job and check the number of processes a user has and if it is over a
specified amount, kill all of that users processes.

Here is what I have so far:

#!/usr/local/bin/bash


i=`/bin/ps aux -U joe | wc -l`
if [ "$i" -eq 6 ]
then
echo "You are approaching your maximum process limit. Please
Kill some processes or the system will automatically kill all of your
processes" | `mail -s "Process Warning" joe`
fi
if [ "$i" -ge 8 ]
kill `ps aux|grep joe | awk '{print $2}'`
fi


upon executing this script i get the warning message

./process.sh: line 11: syntax error near unexpected token `fi'
./process.sh: line 11: `fi'

Does anyone know why?

Also how would it be possible to have a plain text file simply
containing user names, and have the script read from that in a loop and
make the checks as detailed above? As this would save me from having to
add to the script each time a new user was added.

Thanks for your patience





[ Post a follow-up to this message ]



    Re: Problems with a simple script.  
Paul Pluzhnikov


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:36 PM

Materialised <materialised@privacy.net> writes:
quote:
> upon executing this script i get the warning message > > ./process.sh: line 11: syntax error near unexpected token `fi' > ./process.sh: line 11: `fi' > > Does anyone know why?
Because you forgot 'then':
quote:
> if [ "$i" -ge 8 ]
then
quote:
> kill `ps aux|grep joe | awk '{print $2}'` > fi
The 'grep joe' is very bad: if you happen to have users 'joe' and 'joeshmoe', the latter will get killed without any warning whenever the former exceeds his process limit. Further, this script may kill arbitrary process: if any of the processes selected by 'grep joe' terminates before kill begins; and it's PID is recycled for some new and unrelated process, you can kiss that new process goodby :-(
quote:
> Also how would it be possible to have a plain text file simply > containing user names, and have the script read from that in a loop > and make the checks as detailed above? As this would save me from > having to add to the script each time a new user was added.
A better way would be use /etc/passwd (assuming you are not using NIS): that way you would not have to add to the "plain text file" every time a new user is added. Start by reading the "Advanced Bash Scripting Guide" at http://www.tldp.org/LDP/abs/html Then do something like: sed 's/:.*//' /etc/passwd | while read user; do ## whatever with $user done Cheers, -- In order to understand recursion you must first understand recursion. Remove /-nsp/ for email.




[ Post a follow-up to this message ]



    Re: Problems with a simple script.  
Tobias Oed


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:36 PM

Materialised wrote:
quote:
> Hello everyone, i'm trying to write a script that will run from a cron > job and check the number of processes a user has and if it is over a > specified amount, kill all of that users processes. > > Here is what I have so far: > > #!/usr/local/bin/bash > > > i=`/bin/ps aux -U joe | wc -l` > if [ "$i" -eq 6 ] > then > echo "You are approaching your maximum process limit. Please > Kill some processes or the system will automatically kill all of your > processes" | `mail -s "Process Warning" joe`
I don't think you want the quotes arround `mail...`
quote:
> fi > if [ "$i" -ge 8 ] > kill `ps aux|grep joe | awk '{print $2}'` > fi > > > upon executing this script i get the warning message > > ./process.sh: line 11: syntax error near unexpected token `fi' > ./process.sh: line 11: `fi' > > Does anyone know why?
You're missing a "then". Tobias




[ Post a follow-up to this message ]



    Re: Problems with a simple script.  
Materialised


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:36 PM

Paul Pluzhnikov wrote:
quote:
> Materialised <materialised@privacy.net> writes: > > > > > Because you forgot 'then': > > > > then > > > > The 'grep joe' is very bad: if you happen to have users 'joe' and > 'joeshmoe', the latter will get killed without any warning > whenever the former exceeds his process limit. > > Further, this script may kill arbitrary process: > if any of the processes selected by 'grep joe' terminates before > kill begins; and it's PID is recycled for some new and unrelated > process, you can kiss that new process goodby :-( > > > > > A better way would be use /etc/passwd (assuming you are not using NIS): > that way you would not have to add to the "plain text file" every > time a new user is added. > > Start by reading the "Advanced Bash Scripting Guide" at > http://www.tldp.org/LDP/abs/html > > Then do something like: > > sed 's/:.*//' /etc/passwd | while read user; do > ## whatever with $user > done > > Cheers,
See the problem with sed 's/:.*//' /etc/passwd | while read user; do ## whatever with $user done is that I wish to exempt certian users. Would it be possbile to use a if block withing the loop like if [ "$user" == "Whichever User"] then Skip to next user somehow and stop processign the current user. if If I was using /etc/passwd, there should be some way to read the login class. All the users I want this script to effect have thier own login class (users). Thanks for your speedy reply.




[ Post a follow-up to this message ]



    Re: Problems with a simple script.  
Martin Blume


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:36 PM

"Materialised" <materialised@privacy.net> schrieb[QUOTE] 

You can do in your script

userlist="a b c d e f"
for user in $userlist; do
# do the stuff with $user
echo $user
done

or you can put the users in a file called userlist and do

while read user; do
# do the stuff with $user
echo $user
done < userlist

HTH
Martin









[ Post a follow-up to this message ]



    Re: Problems with a simple script.  
Paul Pluzhnikov


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:36 PM

Materialised <materialised@privacy.net> writes:
quote:
> If I was using /etc/passwd, there should be some way to read the login > class. All the users I want this script to effect have thier own login > class (users).
That's just as easy: users_gid=`awk -F: '/^users:/ { print $3 }' /etc/group` awk -F: '{ if ($4 == '$users_gid') { print $1 }}' /etc/passwd | while read user; do ## whatever done Cheers, -- In order to understand recursion you must first understand recursion. Remove /-nsp/ for email.




[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 01:11 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register