Register
It is currently Sun Aug 30, 2015 12:01 pm

A little help please


All times are UTC - 6 hours


Post new topic Reply to topic  [ 12 posts ] 
Author Message
 PostPosted: Fri Jun 26, 2015 4:57 pm   

Joined: Fri Jun 26, 2015 4:43 pm
Posts: 7
Here is a script I'm working on. It works to kill one process at a time which is slightly inconvenient. I wish to make it kill as many pids as needed. Lets say 2-6 at a time. Posting here in the hope that someone can help.
Thanks in advanced,
Infini
Code:
#/bin/bash
if [ `whoami` != root ]; then
    echo "Please run this script as root or using sudo"
    exit
fi
echo
echo "finding proceses that use teamviewer"
echo
ps -ef | grep teamviewer
echo -n "Do you wish to kill a process listed above?[Y/n]"
read choose
if [ "$choose" = "Y" ] || [ "$choose" = "y" ] || [ -z "$choose" ]
then
echo "enter pid to kill"
read procesId
kill -9 $procesId
fi
echo "done exiting"
exit 0


Top
 Profile  
 PostPosted: Fri Jun 26, 2015 5:15 pm   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 188
Code:
killall teamviewer
If name is really "teamviewer"

It is a bad attitude to use kill -9
The process in question got no time to shutdown properly and cleaning up.
Avoid this and use appropriate signal:
Code:
kill -SIGTERM  # uses signal SIG TERMinate
kill -l        # lists available signals


If the name of processes only contain the word "teamviewer" use somthing like:
Code:
pgrep teamviewer | while read pid; do
    kill -SIGTERM $pid
done
pgrep finds all processes, if name contains "teamviewer" and prints a line for each found pid.
This is piped to a while loop which reads it into the var pid.


Last edited by uhelp on Fri Jun 26, 2015 5:26 pm, edited 1 time in total.

Top
 Profile  
 PostPosted: Fri Jun 26, 2015 5:24 pm   

Joined: Fri Jun 26, 2015 4:43 pm
Posts: 7
Sorry I didn't explain more. I do not wish to use killall as there are only certain pids I want to kill using teamviewer and kill -9 works perfect for the work I do.


Top
 Profile  
 PostPosted: Fri Jun 26, 2015 5:27 pm   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 188
DO NOT USE kill -9


Top
 Profile  
 PostPosted: Fri Jun 26, 2015 5:34 pm   

Joined: Fri Jun 26, 2015 4:43 pm
Posts: 7
uhelp wrote:
DO NOT USE kill -9

We need these processes killed as fast as possible to restart them. kill -9 is what we use and it works perfect. What other solutions do you have? I just want to make it better by being able to enter multiple pids to kill at one time instead of having to run the script again, and again.


Top
 Profile  
 PostPosted: Fri Jun 26, 2015 5:38 pm   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 188
Code:
pgrep teamviewer | while read pid; do
    while true; do
        read -p "Do you want to kill process $pid  (y|n)" confirm
        case $pid in
             y ) kill -SIGTERM $pid
                ;;
             n ) echo skipping $pid
                ;;
             *  ) echo wrong answer. Skipping $pid
                ;;
         esac
     done
done


Top
 Profile  
 PostPosted: Fri Jun 26, 2015 5:43 pm   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 188
infini wrote:
We need these processes killed as fast as possible to restart them.
This is a contradiction to asking interactively for confirmation.

And you'll experience no difference in using kill -9 compared to the proper usage kill -SIGTERM $pid

Reread my answers. I've explained the kill command.


Top
 Profile  
 PostPosted: Fri Jun 26, 2015 6:11 pm   

Joined: Fri Jun 26, 2015 4:43 pm
Posts: 7
This does not answer the queston of how to make MY script accept more then one pid after I select yes to kill pid? -SIGTERM also works fine. Sorry if I do not understand as I only have basic knowledge of bash scripting.


Top
 Profile  
 PostPosted: Fri Jun 26, 2015 6:31 pm   

Joined: Fri Jun 26, 2015 4:43 pm
Posts: 7
I have fixed it. Just added in.
Code:
read procesId
read procesId2
read procesId3
kill -SEGTERM $procesId $procesId1 $procesId2 

Thank you for your help


Top
 Profile  
 PostPosted: Fri Jun 26, 2015 7:44 pm   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 188
This is not a solution to to your problem.
It is slow because you use interactive confirmation.

It does not matter whether you kill many processes with one call to kill, or by calling one kill for many processes. Still the interactive part slows down.
(Of course calling kill once for many processes IS faster)

It is hard to help, if you do not describe properly what the goal is.
Stating clearly, what the conditions for a process to be selected for kill, would have helped.
Then one could give good advice.

But just try on.
I even doubt that it is of any use to kill processes fast in order to restart em.
This is not the approach to gain speed.
A kill is just a signal send to the process done by kernel.
No matter what you program in a shell script, it will take it's time defined by kernel.


Top
 Profile  
 PostPosted: Sat Jun 27, 2015 8:58 am   

Joined: Fri Jun 26, 2015 4:43 pm
Posts: 7
I'll try to fill you in more. The company I work for has devices all over the world. Sometimes teamviewer gets processes stuck open in the background so we have to ssh in and kill in order for us to get get teamviewer working properly. Sometimes it is more then one pid we have to kill but we cant kill the teamviewer daemon and certain processes using teamviewer. They will kill the scripts we have running and leave customers unhappy because loss of feed. The daemon must be restarted properly. This will also be implemented for other stuff we run like octoshape and hamachi which from time to time have to be restarted and the processes stuck in background killed. This stuff happens ALL the time. Over 200 machines running 24/7 so we see bugs in all the programs everyday.
Just to clarify. This script is faster then entering in all the commands by hand.


Top
 Profile  
 PostPosted: Sun Jun 28, 2015 6:43 pm   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 188
Then speed is not a concern at all.

It might be a better approach to write a watchdog for teamviewer.
If we know what the exact conditions for killing teamviewer processes are,
we can write a watchdog automating this task completly.

As i'm total open source, i do not use teamviewer. Hence i do not know, which and how many threads or processes it spawns.
If some precesses hang, do they show up as zombie, sleeping or stopped?

I wrote this dirty little script to gather information about these processes.
Code:
#!/usr/bin/env bash
# we let env find the bash as it is not guaranteed that bash is located at /bin

LANG=C    # some used fields are subject to i18n/l10n; so we use US-ASCII
          # to avoid problems with different LOCALEs
          # The written data file then holds only only US-ASCII and is
          # is easily parsable.

# in order to administer lots of client data, we use a uuid. This ensures to
# not disclose any sensitive data. And you can upload the data files directly
# to a forum or pastebin, if needed.
my_id=$(uuidgen)

# the process to analyze; if script is called with a parameter we assume
# it to be the process name
process_to_watch="teamviewer"
if [[ $# == 1 ]] ; then process_to_watch=$1;  fi

# as we often use a pid list of all running instances of $process_to_watch
# we use an arry and set it to:
pid_list=( $(pidof $process_to_watch) )

# if no process is found, we exita
# note: we check the length of first entry in array pid_list without explicitly
# addressing the first entry.
if [[ ${#pid_list} == 0 ]]; then
    printf "\n   No process \"\033[1;32m%s\033[0m\" found.\n   Try again later. p:\n\n" \
            $process_to_watch 
    exit 65   # setting exit status code to 65. Set it at will, allbeit
              # there is a guideline which exit codes one should use.
fi

# we add generated myId to data file name. Makes live easier.
# would expand to a file name like
#  process_data_of_teamviewer_bbf4663e-726d-49a0-94ad-f5a8cd09ab84
data_file="process_data_of_${process_to_watch}_${my_id}"

# we delete any content of this file from maybe earlier runs and add a header.
# change redirection in next line to ">>$data_file", if wipe out of data
# from prior runs is not wanted
echo "#########################################################" > $data_file
echo "$process_to_watch process data created $(date +'%c epoche: %s ns: %N' )" >>$data_file



# adding the uuid to data file too
echo "uuid: $my_id" >> $data_file
echo "#########################################################" >> $data_file
printf "\n\n\n\n" >> $data_file

# we gather a detailed list of used memory for each instance process_to_watch
echo "####   MEMORY MAPPING of PIDs  ##########################"  >> $data_file
( echo ${pid_list[@]} | fold -s -w 80 ) >> $data_file #fold command formats line
                   # so it is still good readable in console.
                   # -s breaks at spaces   -w width.
echo "#########################################################" >> $data_file
printf "\n\n" >> $data_file

pmap -x $( pidof $process_to_watch ) >> $data_file
echo "#########################################################" >> $data_file
printf "\n\n\n\n" >> $data_file

# now we read all infos from /proc that might give us a clue on what's the matter
# we hold these files in an array and these files are:
proc_files=( cmdline environ limits mountinfo mountstats statm status session )
echo "####### INFOS FROM /proc ################################" >> $data_file
printf "\n\n" >> $data_file
for p in ${pid_list[@]}; do
    path="/proc/"$p
    for f in ${proc_files[@]}; do
        cur_file=${path}/$f
        echo "### pid $p file $cur_file :" >> $data_file
        ( cat $cur_file | tr "\0" "\n" ) >> $data_file
    echo "______________________________________________________" >> $data_file
    done
done
printf "\n\n\n\n" >> $data_file

echo "####### TOP INFOS ########################################" >> $data_file
printf "\n" >> $data_file

# we use top with -p which awaits a comma separated list of pids. we build it.
pid_string=$( pidof $process_to_watch  | tr " " "," )

# as "top -p" is limited to 20 pids, we have to count pids
pid_count=$( echo $pid_string | awk -F, '{print NF}' ) # using awk NF
                                             # NumberOfFields to count
top_limit=20
i=0
if (( $pid_count > $top_limit )); then # if pids above limit of top
    for pid in ${pid_list[@]}; do      # for each pid
        temp_pid_string=${temp_pid_string}","$pid #build comma separated string
       
        # using modulo operation and logical compare
        if (( $i % $top_limit == 0 )); then # first 20 pids
            # ${var:1} strips away first comma
            top -b -c -n 1 -p ${temp_pid_string:1} >> $data_file
            printf "\n" >> $data_file
            temp_pid_string= # reset string for next 20 pids
        fi
        ((i++))
    done
    # doing remaining pids
    top -b -c -n 1 -p ${temp_pid_string:1} >> $data_file
    printf "\n" >> $data_file
else  # if lower than top_limit given pids
    top -b -c -n 1 -p $pid_string >> $data_file
    printf "\n" >> $data_file
fi
If you like, run it and paste resulting file.
It gathers about 100kB data per process running.
Kinda overkill and redundant information, but enough to figure out condition for watchdog.
At least i think so.

If someone find it's usefull, drop a note and i will work this out.
(Adding error handling, i18n, usage and so on)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP