Unix Programming - Problems with a simple script.

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > January 2004 > Problems with a simple script.





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Problems with a simple script.
Materialised

2004-01-23, 5: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
Paul Pluzhnikov

2004-01-23, 5: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.
Tobias Oed

2004-01-23, 5: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

Materialised

2004-01-23, 5: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.
Martin Blume

2004-01-23, 5:36 pm

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

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




Paul Pluzhnikov

2004-01-23, 5: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.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com