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.
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:
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.
Thank You Don and Rudic,
So far, here is what I did:
Program name is cvlc as I am running under command line.
At the moment, there are two processes and the code gives:
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
I am working on it..
Update:
Output:
Latest:
Not-working process has been killed and restarted..
Sorted now:
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; 03-09-2019 at 11:52 AM..
Reason: updated
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
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:
Thanks
Boris
Last edited by baris35; 03-09-2019 at 02:45 PM..
Reason: final
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
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:
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
I'm a newbie to shell scripting, I was given this script to modify. This script that monitors when CPU Usage is too high based off the top command. The comparison is not working as it should. Its comparing a decimal to a regualar interger. When it send me an email, it send an email and ignores the... (21 Replies)
Hi All,
I have a script which does report the cpu usuage, there are few output parameter/fields displayed from the script. My problem is I have monitor the output and decide
which cpu number (column 2) has maximum value (column 6).
Since the output is displayed/updated every seconds, it's very... (1 Reply)
Hi,
Linux redhat 5.5
I need to write a kshell script that shows all the process that consume 100% CPU (or more. strange but there are time that top shows higger value that 100) and they are active more than 5 minute.
The top command shows all the relevat information:
The PID of the cpu ,... (0 Replies)
Hi,
could someone give me an example for a debian server script? I need to check a process if the process has a high cpu load (top).
If yes the whole server needs to reboot.
Thats it, nothing more. ;)
Hope someone could help me.
Regards
woisch (2 Replies)
Hi ,
I am new to scripting. please help me out how to write a script to monitor cpu , vmstat, iostat in Redhat linux. we are doing the load test.
Thanks in advance !!!! (1 Reply)
Hello All,
I was wondering if someone can help me to develop restart logic for a Korn Shell program (a master script) that I am developing. The program is design to invoke a series of child processes (Korn Shell Scripts) whose function is to create a series of load files on a specified mount... (0 Replies)
I need a shell script that will monitor a few conditions and not execute until the these conditions are met. The problem I'm having is that I can not perform a database snapshot (backup) of a sybaseIQ database unless the CPU Status Idle % is above 60% or the snapshot (backup) fails.
If... (2 Replies)
how could I use shell script to determine which CPU structure
because I found that I compile my program under Itanium base that cannot run on the PA-RISC base
but PA-RISC program can run on Itanium base
i would like to use shell script to know which CPU structure it is,how could i do
thanks (1 Reply)
I need a shell script to kill apache and restart it, in case the number of processes keeps growing. The logic is like the below, but I don't know how to get the number and neither the syntax. Could somebody kindly help?
if no_of_processes (ps ax ¦ grep httpd) > 200
then
killall httpd... (14 Replies)
Hi,
I have a task that Im stuck on. I have an elementary script named 'myscript' that prints "the script is running" once a second. It runs for 27 seconds. I need to write a 2nd script that starts 'myscript' and takes a parameter '$1' for a number. my 2nd script then needs to pause myscript... (1 Reply)