How to restart shell script when cpu is 0.0%?


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 2 Weeks Ago
How to restart shell script when cpu is 0.0%?

Hello,
My operating system is ubuntu 16.04. I need to kill or restart a shell script when cpu usage of related process is equal to 0.0% for X seconds. Appearing name on top page is vlc
While surfing on forums, I found below script but not sure how to edit:
Also don't know the function of -gt command.

Code:
#!/bin/bash
CPU_LOAD=$(uptime | cut -d"," -f4 | cut -d":" -f2 | cut -d" " -f2 | sed -e "s/\.//g")
CPU_THRESHOLD=700
if [ $CPU_LOAD -gt $CPU_THRESHOLD ] ; then
  kill -9 $(ps -eo pid | sort -k 1 -r | grep -v PID | head -n 1)
fi
exit 0

When I run top:
Code:
PID     USER    PR   NI      VIRT     RES   SHR      S   %CPU   %MEM   TIME   COMMAND
4018   zoltan   20    0      631692   33384  12564  S   0.0      1.6   0:05.30     vlc
4019   zoltan   20    0      631692   32500  12304  S   0.0      1.7   0:04.14     vlc

I'd appreciate your help

Thank you
Boris
# 2  
Old 2 Weeks Ago
What makes you think that killing off (especially with kill -9 which is a very bad idea) a random process with a process ID is going to kill a shell script whose CPU is usage 0.0% for X seconds (whatever X happens to be)? Nothing in you script makes any checks to verify any of those assumptions. If you try doing this as root, this would be a great way to make the system unstable and leave lots of artifacts of killed off processes laying around for some poor administrator to try to clean up later and wondering why his or her usually stable system started randomly crashing. PLEASE DO NOT EVER CONSIDER RUNNING THIS SCRIPT ON ANY UNIX, Linux, or BSD SYSTEM!

To answer your question, your script does not call a -gt command; it calls a [ command whose 2nd operand is a -gt (AKA greater than) operator. The man [ command should tell you all about it; if not, try man test.
These 2 Users Gave Thanks to Don Cragun For This Post:
baris35 (2 Weeks Ago) RudiC (2 Weeks Ago)
# 3  
Old 2 Weeks Ago
Don Cragun is ABSOLUTELY right - your script is inconsistent, pointless, illogic, and DANGEROUS.



Please use a different approach: Describe / explain the underlying problem you encounter, present the basic facts of your system, and your thoughts. Then a solution taylored to your request might be found.


A guess: You seem to want to terminate a process that doesn't do any work any more but doesn't quit. None of your commands in your code addresses that. How about identifying the target processes' PID, run ps for those, and check e.g. the"time" format specifier. man ps:
Quote:
time TIME cumulative CPU time, "[DD-]HH:MM:SS" format. (alias cputime).
not increasing any more. Another approach might be to go for the process' state or wchan fields, if e.g. a link is broken.
This User Gave Thanks to RudiC For This Post:
baris35 (2 Weeks Ago)
# 4  
Old 2 Weeks Ago
Thank You Don and Rudic,
So far, here is what I did:
Program name is cvlc as I am running under command line.
Code:
watch -n5 "ps aux -y | grep 'zoltan' | grep 'cvlc' | awk '{print \$4}'"

At the moment, there are two processes and the code gives:
Code:
0.2
0.2
0.0

Normally 0.0 at the bottom line is the program itself. I am working on how to take/read all output lines excluding the bottom one or assigning pid value to search in the result.


Quote:
A guess: You seem to want to terminate a process that doesn't do any work any more but doesn't quit. None of your commands in your code addresses that.
Yes, that's what I wish to do but you both say it's no good.

PS: At first, I could not have found any info regarding abbreviations, then coincidentally learnt the meaning of gt and lt Smilie
I am working on it..



Update:
Code:
ps aux | grep 'zoltan' | grep 'cvlc' | awk '{print $4}'
                if [ $? -eq 0.0 ]; then
echo "vlc is not running"
./restart.sh
else
echo "vlc is okay"
fi
exit 0

Output:
Code:
0.2
0.2
./check.sh: line 2: [: 0.0: integer expression expected
vlc is okay

Latest:
Code:
                ps aux | grep 'zoltan' | grep 'cvlc' | awk '{print $4}'
                if [ $? -eq 0 ]; then
                id=$(ps aux | grep 'zoltan' | grep 'cvlc' | awk '{print $2}')
echo "vlc is not running"
kill $id
./restart.sh
else
echo "vlc is okay"
fi
exit 0

Not-working process has been killed and restarted..


Sorted now:
Code:
                ps aux | grep 'zoltan' | grep 'cvlc' | awk '{print $4}'
                if [ $? -eq 0 ]; then
                id=$(ps aux | grep 'zoltan' | grep 'cvlc' | awk '{print $2}')
echo "vlc is not running"
ps -f -u zoltan | awk '{ for(i=8; i<=NF; ++i) printf $i""FS; print "" }' > rerun
kill $id
sleep 2
sed -i '1d' rerun
chmod 755 rerun
sed -i "s|/usr/bin/vlc|sudo -u zoltan /usr/bin/vlc|g" rerun
sed -i "s|$| >> output.log 2>&1 < /dev/null \&|g" rerun
./rerun
else
echo "vlc is okay"
fi
exit 0

When cpu 0.0%, it restarts related process.. I am sure you can make it with shorter commands but works though.

Boris

Last edited by baris35; 2 Weeks Ago at 10:52 AM.. Reason: updated
# 5  
Old 2 Weeks Ago
I don't think you identified a "non-working" process. With above, you'd kill even the busiest vlc process ever on your system.
With the trailing awk, any result will be turned into "perfect". Like
Code:
$ ps aux | grep '[z]oltan' | grep 'cvlc'; echo $?
1
$  ps aux | grep '[z]oltan' | grep 'cvlc'| awk '{print $4}'; echo $?
0




Why not just use pkill vlc, then?

Last edited by RudiC; 2 Weeks Ago at 10:57 AM..
# 6  
Old 2 Weeks Ago
Hello Rudic,
You are right, works for just one process.
I think the problem happens as 0.0 not equal to 0.
I have multiple vlc processes, for that reason I should tell script to kill pid where cpu is 0.0%



Update: For your valuable comments:

Code:
set -x
ps -uax | grep 'zoltan' | grep '/usr/bin/vlc' | awk '{print $2,$3,$11,$12,$13,......}' > report2 #grep_all_required_columns_save_into_file
awk '$2==0.0' report2 > report
rm rerun
cat report | awk '{ for(i=3; i<=NF; ++i) printf $i""FS; print "" }' > rerun
while read COL1 COL2
do
kill -9 $COL1
done< report
sleep 2
#sed -i '1d' rerun
chmod 755 rerun
sed -i "s|/usr/bin/vlc|sudo -u zoltan /usr/bin/vlc|g" rerun
sed -i "s|$| >> output.log 2>&1 < /dev/null \&|g" rerun
./rerun
sleep 2
exit 0


Thanks
Boris

Last edited by baris35; 2 Weeks Ago at 01:45 PM.. Reason: final
# 7  
Old 2 Weeks Ago
Quote:
Originally Posted by baris35
I think the problem happens as 0.0 not equal to 0.
You are right. Most shells (with the notable exception of ksh93) only have integers as a data type but not floats. Therefore "0.0" is interpreted as a string but -gt (and similar operands like -ge, -eq, -lt, -le) expects integers and nothing else. i.e test 0 -eq abc will lead to the same error.

Quote:
Originally Posted by baris35
Code:
set -x
ps -uax | grep 'zoltan'

The first thing i see is the missing shebang: do yourself a favour ad explicitly state the shell you want to run this script. Otherwise you will eventually get disappointed.

Second, the ps command seems not to do what you probably intend it to do. ps -uax displays all processes run by a user named "ax". Notice that there is a difference between BSD-style ps and UNIX-SystemV (or POSIX-) style ps. Most implementations understand both sorts of syntax and because in BSD the ps-options were not introduced by dashes if you use dashes the options are interpreted in POSIX-syntax and if you don't they are interpreted in BSD-style. i.e:

Code:
ps aux     # BSD-style with options "a" (all processes), "u" (user-oriented format) and "x" (also processes without a tty)
ps -aux    # POSIX-style with options "-a" (all processes except session leaders and processes without a tty) and
           # -u <username>, since "-u" expects a user name "x" is interpreted as this

Note that some versions of ps, when no user named "x" exists, second-guess the intention and execute as they had been called like ps aux instead. Needless to say that one better not relies on such if i say a you will do as if i had said b because in reality i might mean c ... tactics.

With the correct syntax, though, you could also get rid of the

Code:
| grep zoltan

i.e by using -U <user>.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
baris35 (2 Weeks Ago)
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Help with Shell script that monitors CPU Usage mhannor Shell Programming and Scripting 21 01-02-2019 06:57 PM
Shell script to calculate the max cpu usage from the main script Optimus81 Shell Programming and Scripting 1 08-16-2013 04:04 PM
Shell script to monitor process with high CPU yoavbe Shell Programming and Scripting 0 04-08-2013 10:34 AM
Restart debian server if one specific process has more than 10 seconds have high cpu load woisch Shell Programming and Scripting 2 01-06-2013 04:40 PM
Shell script to monitor cpu and VMstat, iostat saanvi UNIX for Advanced & Expert Users 1 11-08-2012 01:17 PM
Need help on shell script to monitor each user cpu use on a cluster over time anuj06 Shell Programming and Scripting 6 08-14-2012 11:39 AM
Looking for shell script to monitor CPU utilization and send mail once exceed 75% clfever Shell Programming and Scripting 3 04-11-2012 12:35 PM
shell script to alert cpu memory and disk usage help please robo Shell Programming and Scripting 7 09-27-2011 03:13 PM
Restart Logic for a Korn Shell Master Script jonesdk5 Shell Programming and Scripting 0 07-02-2010 01:00 PM
Help on shell script conditional execution when CPU Idle > 60% pancona99 Shell Programming and Scripting 2 03-22-2010 01:33 PM
how could I use shell script to determine which CPU structure alert0919 HP-UX 1 06-26-2009 02:13 AM
Shell script to display top 5 process (per cpu consumption) mr_awd Shell Programming and Scripting 2 05-26-2009 09:23 AM
Solaris CPU/memory status monitoring (Shell script or c++) shingpui Programming 6 05-12-2009 08:57 AM
need help: shell script to restart apache when no. of processes keeps growing _joshua_ Shell Programming and Scripting 14 03-07-2007 07:06 AM
suspend/restart a process in shell script daneensign Shell Programming and Scripting 1 02-13-2006 11:43 PM