Code that has to end no matter what


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Code that has to end no matter what
# 1  
Old 08-30-2018
Code that has to end no matter what

Since this is maxxing out my CPU, is there anything I can add that will make sure stress always ends?


Code:
echo "CPU Stress Test" >> CPU_Stress_Test.txt
echo "stress --cpu 8 --io 4 --vm 2 --vm-bytes 128M --timeout 1m" >> CPU_Stress_Test.txt
echo >> CPU_Stress_Test.txt
date +"%Y-%m-%d-%H-%M" >> CPU_Stress_Test.txt
echo >> CPU_Stress_Test.txt
sensors -f | grep "temp4" >> CPU_Stress_Test.txt
# Send fan speed as well
sensors -f | grep "fan1" >> CPU_Stress_Test.txt
echo >> CPU_Stress_Test.txt
#
stress --cpu 8 --io 4 --vm 2 --vm-bytes 128M --timeout 2m

I found this.

Code:
#!/bin/bash 
function finish {
  # Your cleanup code here
}
trap finish EXIT 


        You place any code that you want to be certain to run in this     "finish" function.  A good common example: creating a temporary     scratch directory, then deleting it after.
   #!/bin/bash
scratch=$(mktemp -d -t tmp.XXXXXXXXXX)
function finish {
  rm -rf "$scratch"
}
trap finish EXIT

# 2  
Old 08-31-2018
enhanced formatting

What is your problem exactly? Does the option "--timeout 2m" not work? If that is the case, the manual says:
Code:
-t, --timeout N
    timeout after N seconds

So try "--timeout 120" . And if you still do not trust it you might consider to use the "timeout" command:
Code:
$ timeout --help
Usage: timeout [OPTION] DURATION COMMAND [ARG]...
  or:  timeout [OPTION]
Start COMMAND, and kill it if still running after DURATION.


Last edited by Ivo Breeden; 08-31-2018 at 07:12 AM.. Reason: formatting quote tags not as expected
This User Gave Thanks to Ivo Breeden For This Post:
# 3  
Old 08-31-2018
Hello,

I'm not 100% clear on what you might mean here, but I see two possibilities. A script will normally exit either when the last command it contains is executed, or when an exit statement is explicitly executed. So you don't really have to worry about "making sure it always ends" - by its very nature, a script will end so long as it contains no loop or logic that would cause it to keep running.

However, if what you're meaning here is that you want a command in your script to always exit after a certain amount of time to prevent it from running indefinitely, then you could try something like this:

Code:
#!/bin/bash

command="/usr/bin/yes"
maxruntime=10

"$command" >/dev/null 2>/dev/null &
watchpid=$!

/usr/bin/sleep "$maxruntime"

if [ -d "/proc/$watchpid" ]
then
        echo "Max runtime exceeded, killing PID $watchpid"

        if /bin/kill -9 "$watchpid" >/dev/null 2>/dev/null
        then
                echo "Killed."
                exit 0
        else
                echo "Could not kill, please investigate manually."
                exit 1
        fi
fi

Here you define command as the full command you want to run, and maxruntime as the maximum number of seconds it can be permitted to run for. The script then executes the command specified in the variable command in the background, with all output re-directed to /dev/null. It then waits for maxruntime seconds, and if the process still exists, it will attempt to kill it and show you the result.

Note that in its current form the script will always wait for maxruntime seconds no matter what, so even if your command has exited before then the script will wait at least that long. You could amend that easily enough though, but this is just to give you an idea of how this might work.

Hope this helps - if not, or if I've not quite grasped what you're actually struggling with here, then if you could provide a bit more info I'd be happy to help further.
This User Gave Thanks to drysdalk For This Post:
# 4  
Old 08-31-2018
Yes, I wanted to make sure the stress command does end.

If it didn't, it could burn up my CPU.

Thanks, I will study your code.

------ Post updated at 06:49 AM ------

stress is never executed.?

Code:
command="stress --cpu 8 --io 4 --vm 2 --vm-bytes 128M --timeout 1m"
maxruntime=60

"$command" >/dev/null 2>/dev/null &
watchpid=$!

/bin/sleep "$maxruntime"

if [ -d "/proc/$watchpid" ]
then
        echo "Max runtime exceeded, killing PID $watchpid"

        if /bin/kill -9 "$watchpid" >/dev/null 2>/dev/null
        then
                echo "Killed."
                exit 0
        else
                echo "Could not kill, please investigate manually."
                exit 1
        fi
fi

------ Post updated at 06:59 AM ------

Quote:
Originally Posted by Ivo Breeden
What is your problem exactly? Does the option "--timeout 2m" not work? If that is the case, the manual says:
Code:
-t, --timeout N
    timeout after N seconds

So try "--timeout 120" . And if you still do not trust it you might consider to use the "timeout" command:
Code:
$ timeout --help
Usage: timeout [OPTION] DURATION COMMAND [ARG]...
  or:  timeout [OPTION]
Start COMMAND, and kill it if still running after DURATION.

Yes, the command does work. I want to make sure nothing occurs whereby the stress commands does not end.
# 5  
Old 08-31-2018
Hi,

OK, two things to try here:

1. Use the full path in your command - so /usr/bin/stress (assuming that's where it's installed on your system, naturally) rather than just stress
2. Remove the double-quotes from around the "$command" statement, so that it simply reads $command

Hope this helps.
This User Gave Thanks to drysdalk For This Post:
# 6  
Old 08-31-2018
Is there a way so I don't have to manually change maxruntime?

So that --timeout "time" = maxruntime
Code:
command="/usr/bin/stress --cpu 8 --io 4 --vm 2 --vm-bytes 128M --timeout 15s"
maxruntime=20s

# 7  
Old 09-01-2018
Hi,

One modification you could make here would be to change the line defining maxruntime to this instead:

Code:
maxruntime=$1

The variable $1 has a special meaning referring to the first command-line parameter passed into the script. So for a ten-second timeout, you could then run the script by typing script.sh 10, and when the script ran maxruntime would take the definition of the first parameter passed to the script - 10, in this case.
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Strange system activity no matter what I try

When I choose to encrypt my drive during a Linux install, it encryps it, but I receive errors in dmesg and in ~/.xsessions-errors during use. The first error is in dmesg where it sometimes shows errors writing to the encypted device. The second error is in ~/.xsessions-errors with an error about... (0 Replies)
Discussion started by: justgoogleit
0 Replies

2. Shell Programming and Scripting

grep matter between braces

#include<header.h> void classname :: pvvd_init ( abcd ,efgh ,ijkl ,mnop ) { rvcl_tabl_name_tabl.pvvd_init ( xxxx ,"tabl_mame" ) ; ... (2 Replies)
Discussion started by: ultimatix
2 Replies

3. Shell Programming and Scripting

Terminal to the front no matter what

Is there a way to bring the terminal script to the front? I am running this script through OMCEdit which is then running it through Terminal. I have some dialog boxes (using osascript) and the dialog boxes are not coming to the front...Terminal bounces and I have to click on Terminal to see the... (1 Reply)
Discussion started by: mainegate
1 Replies

4. Shell Programming and Scripting

How to delete matter in between two lines

help I am having text file like this... ------------------------END OF UPDATION ------------------ xxxxxxxxxxxxxxxxxxxxxxxxx yyyyyyyyyyyyyyyyyyyyy yyyyyyyyyyyyyyyyyyyyy 1 row updated ------------------------END OF UPDATION ------------------ TTTTTTTTTTTTTTTT FFFFFFFFFFFFFFFFF ... (3 Replies)
Discussion started by: suryanarayana
3 Replies

5. Linux

Need help with this matter

I have a PC that was built in Europe pre-installed with Windows 2000. The HDD is 40GB, but, its split up as two 20GB (Taken up by Windows). I want to take over my Mandrake 9.1 CDs and install Linux on that machine. My question is, how would I proceed to install Linux this way??? Now, If... (1 Reply)
Discussion started by: wardialer
1 Replies
Login or Register to Ask a Question