Shell Scripting

Shell Scripting

Post by Andrew Dua » Tue, 30 Jul 2002 23:33:19



Hi,

I'm new to this board so hopefully someone can help me. I shall try to
contribute back into the group also.

I'm trying to write a script in HPUX 11 to kill off processes older
than two days (48 hours).

These are specific processes whos name ends in ...mx. Now I'm admin so
I have root thats fine to kill these processes which are gui taking up
excess room.

To get the processes up I normally do $> ps -ef | grep mx | sort and
would normally $> kill PID to clear off all processes over two days
old.

First off my query is if I try to evaluate the date whereby the date
is show as Jun 16 for example but then if its a process run today it
shows as 08:17:09 in time rather than date.

So far I've got :

#!/usr/bin/sh
#     Script                    : Cleanup
#     Ver                       : 0.1
#     Last Modified             : 29-July-2002
#     Created by                : Andrew Duane

date_var=$(date|cut -c05-10)
age=$(ps -ef|grep mx|cut -c25-33)

echo $age
if
   [ "$1" < "$date_var" ]
then
   echo older
else
   echo opps
fi

Where I will change the echos into kills eventually but even this
doesn't work as it says sh: 1: Parameter not set.

I'm only new so please bear with me.

Many Thanks

Andrew

 
 
 

Shell Scripting

Post by Bill Marc » Wed, 31 Jul 2002 02:00:21


On 29 Jul 2002 07:33:19 -0700,

Quote:

>Where I will change the echos into kills eventually but even this
>doesn't work as it says sh: 1: Parameter not set.

$1 is the first argument in the command line that started the script.
You could test $# before assuming that $1 exists.

 
 
 

Shell Scripting

Post by Chris F.A. Johnso » Wed, 31 Jul 2002 03:34:16



> Hi,

> I'm new to this board so hopefully someone can help me. I shall try to
> contribute back into the group also.

> I'm trying to write a script in HPUX 11 to kill off processes older
> than two days (48 hours).

> These are specific processes whos name ends in ...mx. Now I'm admin so
> I have root thats fine to kill these processes which are gui taking up
> excess room.

> To get the processes up I normally do $> ps -ef | grep mx | sort and
> would normally $> kill PID to clear off all processes over two days
> old.

> First off my query is if I try to evaluate the date whereby the date
> is show as Jun 16 for example but then if its a process run today it
> shows as 08:17:09 in time rather than date.

> So far I've got :

> #!/usr/bin/sh
> #     Script                    : Cleanup
> #     Ver                       : 0.1
> #     Last Modified             : 29-July-2002
> #     Created by                : Andrew Duane

> date_var=$(date|cut -c05-10)

    No need to use an extra process (cut):

       date_var=$(date "+%b %d")

Quote:> age=$(ps -ef|grep mx|cut -c25-33)

    If your version of ps has an option to allow the formatting of its
    output, you can make parsing its output easier. You can still
    avoid calling an external process. I don't know the format that
    your ps uses, but something like this should work:

       age=$(ps -ef | grep mx | { read _ _ _ _ month day _; echo "$month $day"; })

    (The underscores represent unneeded fields; you may need to modify
    the number of them.)

    If the $age variable contains a colon, you know the process was
    run today, and can base further execution on that fact:

       case $age in
        *:*) age=$date_var ;; ## or just exit??
       esac

    Avoiding an external program (such as cut) will not make much
    difference in this case, but it would if the lines were in a loop
    that iterated many times; it's good to get into the habit of
    writing more efficient code.

Quote:> echo $age
> if
>    [ "$1" < "$date_var" ]

    First, as Bill Marcum pointed out, you should check that $1
    actually contains a valid string for comparison.

    Second, what does it contain, and why not generate it within the
    script?

    Third, why are you comparing it with $date_var (today's date)
    instead of $age?

Quote:> then
>    echo older
> else
>    echo opps
> fi

> Where I will change the echos into kills eventually but even this
> doesn't work as it says sh: 1: Parameter not set.

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2002, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License
 
 
 

1. handling SIGINT in shell scripts when executing another shell script.

I have a simple shell script foo1.sh that invokes another shell script
foo2.sh, something like below:

#!/bin/sh
SIGINT_handler()
{
  echo "## [$DATE_TIME]  User interrupt ignored,"
  continue
trap SIGINT_handler 2

...
./foo2.sh
...

----------
while executing foo1.sh, if the script receives SIGINT, how do I ignore
the SIGINT in foo2.sh ? somehow foo2.sh does not finish to completion
and is killed after receiving SIGINT. but foo1.sh is still executing.
Is there any way to trap this signal in the child script. ? Please
share your thots and inputs on this topic.
Regards,
Vikram Shekhar

2. Problem loading modules for Tekram DC-390U2W on boot in RH 7.1

3. Q: How can I have a shell script call another shell script...

4. Entry Level Software Engineer Position Wanted

5. Shell script invoking other shell scripts

6. vi: cannot load library 'libsm.so.6'

7. Convert Bash shell script to Korn shell script

8. Installing RH8.0 - 2 Hard Disks

9. Shell Script Help (C-Shell Script)

10. How to pass a variable from a shell script to another shell script...

11. shell script within shell script (general question)

12. Shell script acting as interactive shell - $0 not script name??

13. How to find other copies of same shell script running inside shell?