Sponsored Content
Top Forums Shell Programming and Scripting Code that has to end no matter what Post 303022399 by drysdalk on Friday 31st of August 2018 06:22:25 AM
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:
 

5 More Discussions You Might Find Interesting

1. 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

2. 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

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

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

5. 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
escape(1)							Mail Avenger 0.8.3							 escape(1)

NAME
escape - escape shell special characters in a string SYNOPSIS
escape string DESCRIPTION
escape prepends a "" character to all shell special characters in string, making it safe to compose a shell command with the result. EXAMPLES
The following is a contrived example showing how one can unintentionally end up executing the contents of a string: $ var='; echo gotcha!' $ eval echo hi $var hi gotcha! $ Using escape, one can avoid executing the contents of $var: $ eval echo hi `escape "$var"` hi ; echo gotcha! $ A less contrived example is passing arguments to Mail Avenger bodytest commands containing possibly unsafe environment variables. For example, you might write a hypothetical reject_bcc script to reject mail not explicitly addressed to the recipient: #!/bin/sh formail -x to -x cc -x resent-to -x resent-cc | fgrep "$1" > /dev/null && exit 0 echo "<$1>.. address does not accept blind carbon copies" exit 100 To invoke this script, passing it the recipient address as an argument, you would need to put the following in your Mail Avenger rcpt script: bodytest reject_bcc `escape "$RECIPIENT"` SEE ALSO
avenger(1), The Mail Avenger home page: <http://www.mailavenger.org/>. BUGS
escape is designed for the Bourne shell, which is what Mail Avenger scripts use. escape might or might not work with other shells. AUTHOR
David Mazieres Mail Avenger 0.8.3 2012-04-05 escape(1)
All times are GMT -4. The time now is 05:45 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy