General

Script in cron job kills needed processes

 
George_231
Occasional Advisor

Script in cron job kills needed processes

I have a script running in cron that kills processes NOT attached to a terminal, i.e. with a question mark in the ps -ef output. The script has a provision to exclude certain users like root, lp, application users. I recently added one more ID to be excluded but it continues to kill his processes.
Do I need to recycle cron?
Here is the script:
#!/usr/bin/sh
# kill_nonterm_procs
#
ps -ef | grep "?" > /tmp/RUNPROCS
cat /tmp/RUNPROCS | awk '{ print $1 }' > /tmp/RUNPROCS_USERS
cat /tmp/RUNPROCS_USERS |
grep -v mfg |
grep -v mls |
grep -v root |
grep -v adusys |
grep -v pedi53 |
grep -v pedi54 |
grep -v lp |
grep -v tngadmin|
grep -v ftp |
grep -v patrol |
grep -v patuser |
grep -v jzbnvs |
grep -v grep | sort -u > /tmp/RUNPROCS_USERS_CLEAN
if [ -s /tmp/RUNPROCS_USERS_CLEAN ]
then
for i in `cat /tmp/RUNPROCS_USERS_CLEAN`
do
echo `date` >>/tmp/GG
echo `ps -fu $i | grep -v UID | grep "?"` >> /tmp/GG
PROCS=`ps -fu $i | grep "?" | grep -v PID | grep -v grep | sort -u | awk '{print $2 }'`
if [ -n "$PROCS" ]
then
kill -15 $PROCS
else
:
fi
done
rm /tmp/RUNPROCS
rm /tmp/RUNPROCS_USERS
rm /tmp/RUNPROCS_USERS_CLEAN
else
exit 2
fi
14 REPLIES 14
Pete Randall
Outstanding Contributor

Re: Script in cron job kills needed processes

No need to recycle cron, but you do have to have it re-read your cron input. Generally, it's done like this:

crontabl -l > cronfile
edit cronfile to make changes
crontab cronfile


Pete

Pete
Pete Randall
Outstanding Contributor

Re: Script in cron job kills needed processes

I'll take that back. If you change the script that cron is executing, then the change should have been implemented immediately. I would look for a flaw in your script logic instead.


Pete

Pete
Geoff Wild
Honored Contributor

Re: Script in cron job kills needed processes

No need to recycle cron - should be able to just make the change - and the next time it is run - it will run the newest version of the script..

Question is, why are you wanting to kill processes not attatched to a terminel?

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
George_231
Occasional Advisor

Re: Script in cron job kills needed processes

Geoff,
I need to run this script every 30 minutes because there are thousands of users who connect to the server using a windows telnet client and do not log out. Their session shows in "ps -ef" with a question mark.
Geoff Wild
Honored Contributor

Re: Script in cron job kills needed processes

Do those show up with a?

who -p

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Geoff Wild
Honored Contributor

Re: Script in cron job kills needed processes

Sorry - that should be:

who -du

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Geoff Wild
Honored Contributor

Re: Script in cron job kills needed processes

Just took a quick look - don't think who -du will help...do they show up with a "who" at all?

Otherwise, the ps way may be the only way....

Which user is it still killing?

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Howard Marshall
Regular Advisor

Re: Script in cron job kills needed processes

After giving this a little extra though I think I would take a different approach.

Instead of making an exclusion list and killing everyone else I think I would make an inclusion list, set up in a for loop, and use ps -fu instead of -ef

That way you are certain that you are only killing user processes for those users its ok to kill those processes for and that your not missing that should be killed just because the processes happens to have one of your excluded strings in description.

#!/usr/bin/sh

for user in user1 user2 user3 user4 user5
do
ps -fu $user | get process list to kill
kill process list
done
George_231
Occasional Advisor

Re: Script in cron job kills needed processes

The processes to be killed do not show in "who -du" b ut only in "ps -ef"
The script works fine when run manually meaning it excludes the newly added user (in my case user "tngadmin").
The problem occurs when run from cron. I re-read the crontab file with "crontab -e" but it keeps killing the newly added to the exclusion list user "tngadmin".
So I am wondering if I need to recycle cron at a suitable time tonight!?