Discussion:
log if a script runs long
Robert Citek
2013-03-12 12:03:54 UTC
Permalink
Hello all,

I have a script that runs out of crontab that occasionally runs longer
than some time limit. I would like the script to log when and only
when it runs longer than some timelimit.

Here was the initial prototype for the script:

#!/bin/bash
runtime=10
sleeptime=30
(
sleep $sleeptime
date >> /tmp/log.txt
) &
bg=$!
sleep $runtime
kill $bg

This works for $runtime greater than $sleeptime. However, when
$runtime is less than $sleeptime, killing the parent process orphans
the sleep command. When I run this command several times, I end up
with lots of orphaned sleep processes.

The quick-n-dirty solution was to substitute this for..loop for the
sleep command:

for((i=0;i<$sleeptime;i++)) ; do sleep 1 ; done

It works good-enough, but am looking for a more elegant solution.

Any thoughts?

Regards,
- Robert
--
--
Central West End Linux Users Group (via Google Groups)
Main page: http://www.cwelug.org
To post: ***@googlegroups.com
To subscribe: cwelug-***@googlegroups.com
To unsubscribe: cwelug-***@googlegroups.com
More options: http://groups.google.com/group/cwelug
---
You received this message because you are subscribed to the Google Groups "Central West End Linux Users Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cwelug+***@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
David Dooling
2013-03-13 00:45:22 UTC
Permalink
Does it count as elegant if you use ps? Something like:

sleep_pid=$(ps -ef | awk "\$3 == $bg && \$8 ~ /sleep/ { print $2 }")
kill $bg $sleep_pid

where the first awk conditional targets children of the background process.
This may have a race condition if $sleep_pid responds to the kill before
$bg.
Post by Robert Citek
Hello all,
I have a script that runs out of crontab that occasionally runs longer
than some time limit. I would like the script to log when and only
when it runs longer than some timelimit.
#!/bin/bash
runtime=10
sleeptime=30
(
sleep $sleeptime
date >> /tmp/log.txt
) &
bg=$!
sleep $runtime
kill $bg
This works for $runtime greater than $sleeptime. However, when
$runtime is less than $sleeptime, killing the parent process orphans
the sleep command. When I run this command several times, I end up
with lots of orphaned sleep processes.
The quick-n-dirty solution was to substitute this for..loop for the
for((i=0;i<$sleeptime;i++)) ; do sleep 1 ; done
It works good-enough, but am looking for a more elegant solution.
Any thoughts?
Regards,
- Robert
--
--
Central West End Linux Users Group (via Google Groups)
Main page: http://www.cwelug.org
More options: http://groups.google.com/group/cwelug
---
You received this message because you are subscribed to the Google Groups
"Central West End Linux Users Group" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/groups/opt_out.
--
David Dooling
--
--
Central West End Linux Users Group (via Google Groups)
Main page: http://www.cwelug.org
To post: ***@googlegroups.com
To subscribe: cwelug-***@googlegroups.com
To unsubscribe: cwelug-***@googlegroups.com
More options: http://groups.google.com/group/cwelug
---
You received this message because you are subscribed to the Google Groups "Central West End Linux Users Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cwelug+***@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Robert Citek
2013-03-17 05:02:48 UTC
Permalink
X-Received: by 10.49.71.50 with SMTP id r18mr960305qeu.26.1363496569876;
Sat, 16 Mar 2013 22:02:49 -0700 (PDT)
X-BeenThere: ***@googlegroups.com
Received: by 10.49.35.241 with SMTP id l17ls915888qej.99.gmail; Sat, 16 Mar
2013 22:02:49 -0700 (PDT)
X-Received: by 10.224.185.79 with SMTP id cn15mr7061167qab.4.1363496569061;
Sat, 16 Mar 2013 22:02:49 -0700 (PDT)
Received: from mail-qa0-f49.google.com (mail-qa0-f49.google.com [209.85.216.49])
by gmr-mx.google.com with ESMTPS id x1si529139qci.2.2013.03.16.22.02.49
(version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128);
Sat, 16 Mar 2013 22:02:49 -0700 (PDT)
Received-SPF: pass (google.com: domain of ***@gmail.com designates 209.85.216.49 as permitted sender) client-ip=209.85.216.49;
Received: by mail-qa0-f49.google.com with SMTP id o13so1054158qaj.8
for <***@googlegroups.com>; Sat, 16 Mar 2013 22:02:49 -0700 (PDT)
X-Received: by 10.224.181.210 with SMTP id bz18mr13800576qab.68.1363496568937;
Sat, 16 Mar 2013 22:02:48 -0700 (PDT)
Received: by 10.49.71.105 with HTTP; Sat, 16 Mar 2013 22:02:48 -0700 (PDT)
In-Reply-To: <CAE888266cTwYEy3zkOOqurZUEOxMMM1Q7utvWJh=nSMt=D-***@mail.gmail.com>
X-Original-Sender: ***@gmail.com
X-Original-Authentication-Results: gmr-mx.google.com; spf=pass
(google.com: domain of ***@gmail.com designates 209.85.216.49 as
permitted sender) smtp.mail=***@gmail.com; dkim=pass header.i=@gmail.com
Precedence: list
Mailing-list: list ***@googlegroups.com; contact cwelug+***@googlegroups.com
List-ID: <cwelug.googlegroups.com>
X-Google-Group-Id: 960259670415
List-Post: <http://groups.google.com/group/cwelug/post?hl=en>, <mailto:***@googlegroups.com>
List-Help: <http://groups.google.com/support/?hl=en>, <mailto:cwelug+***@googlegroups.com>
List-Archive: <http://groups.google.com/group/cwelug?hl=en>
Sender: ***@googlegroups.com
List-Unsubscribe: <http://groups.google.com/group/cwelug/subscribe?hl=en>, <mailto:googlegroups-manage+960259670415+***@googlegroups.com>
Archived-At: <http://permalink.gmane.org/gmane.org.user-groups.linux.cwelug/8632>

It does. And substitute using process groups instead of process ids:

bg=$!
pgid=$(ps h -o "%r" -p ${bg})
...
kill -- -${pgid}

The ps command returns the process group of the background process.

But the reason this works is a bit of luck because the kill command is
the last command. The kill command will kill all processes in the
same process group id. The "sleep" child process will be in that
group as well as the sleep's parent shell. But so will the script
itself. So that kill is killing all the script's child processes and
itself.

For example, a script like this will kill off the child sleep process,
but will never echo "world" :

#!/bin/bash
( sleep 20 ) &
bg=$!
pgid=$(ps h -o "%r" -p ${bg})
echo "hello"
kill -- -${pgid}
echo "world"

The optimal solution would be to have some tool that does one of two things:
1) given a process id, kills all child processes (I would have thought
that this would be a option to kill)
... or
2) given a process id, returns a list of child pids that can be passed to kill.

For a reasonably good description of processes, process groups,
sessions, and threads:

http://www.win.tue.nl/~aeb/linux/lk/lk-10.html

And to view them all:

$ ps jx -L | sort -k4,4n -k3,3n -k1,1n -k2,2n -k7,7n -k5,5n | less -iX -S

Regards,
- Robert
Post by David Dooling
sleep_pid=$(ps -ef | awk "\$3 == $bg && \$8 ~ /sleep/ { print $2 }")
kill $bg $sleep_pid
where the first awk conditional targets children of the background process.
This may have a race condition if $sleep_pid responds to the kill before
$bg.
Post by Robert Citek
Hello all,
I have a script that runs out of crontab that occasionally runs longer
than some time limit. I would like the script to log when and only
when it runs longer than some timelimit.
#!/bin/bash
runtime=10
sleeptime=30
(
sleep $sleeptime
date >> /tmp/log.txt
) &
bg=$!
sleep $runtime
kill $bg
This works for $runtime greater than $sleeptime. However, when
$runtime is less than $sleeptime, killing the parent process orphans
the sleep command. When I run this command several times, I end up
with lots of orphaned sleep processes.
The quick-n-dirty solution was to substitute this for..loop for the
for((i=0;i<$sleeptime;i++)) ; do sleep 1 ; done
It works good-enough, but am looking for a more elegant solution.
Any thoughts?
Regards,
- Robert
--
--
Central West End Linux Users Group (via Google Groups)
Main page: http://www.cwelug.org
More options: http://groups.google.com/group/cwelug
---
You received this message because you are subscribed to the Google Groups
"Central West End Linux Users Group" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/groups/opt_out.
--
David Dooling
--
--
Central West End Linux Users Group (via Google Groups)
Main page: http://www.cwelug.org
More options: http://groups.google.com/group/cwelug
---
You received this message because you are subscribed to the Google Groups
"Central West End Linux Users Group" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/groups/opt_out.
--
--
Central West End Linux Users Group (via Google Groups)
Main page: http://www.cwelug.org
To post: ***@googlegroups.com
To subscribe: cwelug-***@googlegroups.com
To unsubscribe: cwelug-***@googlegroups.com
More options: http://groups.google.com/group/cwelug
---
You received this message because you are subscribed to the Google Groups "Central West End Linux Users Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cwelug+***@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Loading...