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
# 8  
Old 09-01-2018
Do NOT run this unless you are prepared to reboot. :-(

When I ran the code below....

test.sh 5
Max runtime exceeded, killing PID 2749
/home/andy/bin/test.sh: line 30: 2749 Killed $command > /dev/null 2> /dev/null
Killed.


Code:
command="/usr/bin/stress --cpu 8 --io 4 --vm 2 --vm-bytes 128M"

maxruntime=$1

$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

If you are gonna slice haberno peppers, either use gloves or wash hands 3 times. I forgot and rubbed my eyes.

Last edited by drew77; 09-01-2018 at 02:27 PM..
# 9  
Old 09-01-2018
Hi,

I'm not entirely clear on what could have caused a reboot here - from the output you've provided it seems that after five seconds the script proceeded to kill the previously-backgrounded command. If you can give us a bit more info on what went wrong, what necessitated a reboot, and how this came about then I'd be happy to offer further suggestions as to how to proceed.
# 10  
Old 09-01-2018
It did not cause a reboot, but the stress test never ended resulting in an unchecked rise in my cpu temp.

I made some corrections.

Code:
#!/bin/bash
# Ubuntu_Mate 18.04 LTS
#-----------------------------------
# Much help from https://www.unix.com/shell-programming-and-scripting/
# https://ubuntuforums.org/ https://askubuntu.com/ https://ubuntu-mate.community                                 
#                                 
# don't use ALLCAPS for variable names
#-----------------------------------

LogFile="/home/andy/bin/CPU_Stress_Test.txt"
maxruntime=15s
command=(/usr/bin/stress --cpu 8 --io 4 --vm 2 --vm-bytes 128M --timeout "$maxruntime")
# remove logfile if it exists
if [ -f "$Scripts" ];
then
rm $Scripts
fi

"${command[@]}" & watchpid=$!

date +"%Y-%m-%d-%H:%M:%S" >> $LogFile
sensors -f | grep "temp4" >> $LogFile
sensors -f | grep "fan1" >>  $LogFile

/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
date +"%Y-%m-%d-%H:%M:%S" >> $LogFile
sensors -f | grep "temp4" >> $LogFile
sensors -f | grep "fan1" >>  $LogFile

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