Sponsored Content
Top Forums Shell Programming and Scripting How to identify the calling script? Post 302171388 by crematoriumm on Thursday 28th of February 2008 10:53:24 AM
Old 02-28-2008
Quote:
Originally Posted by vino
Interesting.

Couple of ways.

1. Why dont you embed k2.sh in k1.sh ? After all k2 serves k1 alone.

2. Just before you call k2.sh, export a unique variable from k1.sh. When you start k2.sh, check for that new export. If not there, then act accordingly.

3. Use the features from ps. You can list the ps output in a tree format. From k2.sh get your own pid and your parents pid ($PPID), traverse up the list 1 step to get your parent's PID and CMD. Compare that CMD with the complete path of k1.sh or compare just the pid value. If it matches, proceed. This works well if the path to k1.sh remains the same throughout.

There may be other ways as well... nothing strikes me now.

But none of these solutions stand (except for sol1) if someone writes their own k1.sh.
Vino,

I can't use the solutions 1 and 2 for security reasons, but the third solution sounds to be great.

I did this:

The code:

#!/bin/ksh
var1=`echo $$`
echo "k2.sh pid => " $var1 ## Just for checking. Will be erased...
echo "Listing processes with the k2.sh pid" ## Just for checking. Will be erased...
ps -ef | grep $var1 ## Just for checking. Will be erased...
v=`ps -ef | grep $var1 | sort +1 -2 | cut -d" " -f4 | head -1`
echo "k2.sh parent process => "$v ## Just for checking. Will be erased...
echo "Listing processes with the k2.sh parent process" ## Just for checking. Will be erased...
ps -ef | grep $v ## Just for checking. Will be erased...
echo "The parent process..." ## Just for checking. Will be erased...
ps -ef | grep $v | sort | head -1 | cut -d" " -f13

The result:

server01:/var/tmp$ /var/tmp/k1.sh
k2.sh pid => 29483
Listing processes with the k2.sh pid
user01 29484 29483 1 12:43:59 pts/38 0:00 ps -ef
user01 29483 29482 0 12:43:59 pts/38 0:00 /bin/ksh /var/tmp/k2.sh
k2.sh parent process => 29482
Listing processes with the k2.sh parent process
user01 29482 28411 0 12:43:59 pts/38 0:00 /bin/ksh /var/tmp/k1.sh
user01 29483 29482 0 12:43:59 pts/38 0:00 /bin/ksh /var/tmp/k2.sh
The parent process...
/var/tmp/k1.sh

Can you see any security hole in it?

Thanks a lot for your help!!

Cremm.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

test script to identify SHELL

I am new to BASH and writing a small script to identify the SHELL . #!/bin/bash BASH='/bin/bash' KSH='/bin/ksh' if then echo "it's Bash" else echo "it's not Bash" fi $ bash -x a.sh + BASH=/bin/bash + KSH=/bin/ksh + '' a.sh: line 4: where am I missing . PLease advice . (10 Replies)
Discussion started by: talashil
10 Replies

2. Shell Programming and Scripting

Identify script please!

Have a script that I'm trying to adapt for something else, but I do not understand the following lines. Can anyone help? I know what expr does, `expr 8 + 2`, but: FILENAME=`expr //$FILE : '.*/\(.*\)'` UNPACKDIR=`echo $FILE | sed -e s/$FILENAME//g` Thanks (6 Replies)
Discussion started by: bound4h
6 Replies

3. Shell Programming and Scripting

Using an awk script to identify dupes in two files

Hello, I have two files. File1 or the master file contains two columns separated by a delimiter: a=b b=d e=f g=h File 2 which is the file to be processed has only a single column a h c b What I need is an awk script to identify unique names from file 2 which are not found in the... (6 Replies)
Discussion started by: gimley
6 Replies

4. Shell Programming and Scripting

Syntax error calling TCL script from shell script

hello everyone i am beginner on shell scripting .and i am working on my project work on ad hoc network i wrote a batch (.sh) to do a looping and execute a tcl script i wrote before in each iteration ..but i got this problem " syntax error near unexpected token `('... (1 Reply)
Discussion started by: marcoss90
1 Replies

5. Shell Programming and Scripting

Script to Identify if folder has a file in it

Hi everyone I am new to the forums. I haven't done much linux myself but I have been asked if I can do the following. Write a linux script that needs to scan a certain folder every x amount of minutes and if there is a file in the folder then it needs to call a different script. Is this... (2 Replies)
Discussion started by: Bosbaba
2 Replies

6. Shell Programming and Scripting

Calling a Perl script in a Bash script -Odd Situation

I am creating a startup script for an application. This application's startup script is in bash. It will also need to call a perl script (which I will not be able to modify) for the application environment prior to calling the application. The problem is that this perl script creates a new shell... (5 Replies)
Discussion started by: leepet01
5 Replies

7. Shell Programming and Scripting

How we can pass the argument when calling shell script from perl script

Can someone let me know how could I achieve this In one of per script I am calling the shell script but I need to so one thing that is one shell script call I need to pass pne argument.In below code I am calling my ftp script but here I want to pass one argument so how could I do this (e.g:... (5 Replies)
Discussion started by: anuragpgtgerman
5 Replies

8. Shell Programming and Scripting

How to identify whether the script is in Unix format or not ?

Hi All, I have the below scenario in my environment Developers used to copy file from windows to Linux box. Some time on the copied file developers miss to run the dos2unix utility. Because of this script gets failed during the execution. Most of the failures are due to the dos2unix format... (7 Replies)
Discussion started by: kalpeer
7 Replies

9. Shell Programming and Scripting

Identify variables used in the shell script

Hi, Is there any simple way to get/identify the variables that are used in a file and print those variable names. If I have something like this in a file, /$var/temp_dir/${var2}${var3}.log I want to display the variables 'var', 'var2' and 'var3' from that file. I tried something like... (6 Replies)
Discussion started by: pvamsikr
6 Replies

10. Shell Programming and Scripting

Unable to identify error in the script

cat test3.sh #!/bin/sh set -x while getopts ":n:" opt; do case "$opt" in n) host=$OPTARG shift 2 ;; -h ) host=$2 shift 2 ;; *) break ;; esac done; echo "host =... (7 Replies)
Discussion started by: Vishal_dba
7 Replies
preap(1)							   User Commands							  preap(1)

NAME
preap - force a defunct process to be reaped by its parent SYNOPSIS
preap [-F] pid... DESCRIPTION
A defunct (or zombie) process is one whose exit status has yet to be reaped by its parent. The exit status is reaped via the wait(3C), waitid(2), or waitpid(3C) system call. In the normal course of system operation, zombies may occur, but are typically short-lived. This may happen if a parent exits without having reaped the exit status of some or all of its children. In that case, those children are reparented to PID 1. See init(1M), which periodically reaps such processes. An irresponsible parent process may not exit for a very long time and thus leave zombies on the system. Since the operating system destroys nearly all components of a process before it becomes defunct, such defunct processes do not normally impact system operation. However, they do consume a small amount of system memory. preap forces the parent of the process specified by pid to waitid(3C) for pid, if pid represents a defunct process. preap will attempt to prevent the administrator from unwisely reaping a child process which might soon be reaped by the parent, if: o The process is a child of init(1M). o The parent process is stopped and might wait on the child when it is again allowed to run. o The process has been defunct for less than one minute. OPTIONS
The following option is supported: -F Forces the parent to reap the child, overriding safety checks. OPERANDS
The following operand is supported: pid Process ID list. EXIT STATUS
The following exit values are returned by preap, which prints the exit status of each target process reaped: 0 Successfully operation. non-zero Failure, such as no such process, permission denied, or invalid option. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWesu (32-bit) | +-----------------------------+-----------------------------+ | |SUNWesxu (64-bit) | +-----------------------------+-----------------------------+ SEE ALSO
proc(1), init(1M), waitid(2), wait(3C), waitpid(3C), proc(4), attributes(5) WARNINGS
preap should be applied sparingly and only in situations in which the administrator or developer has confirmed that defunct processes will not be reaped by the parent process. Otherwise, applying preap may damage the parent process in unpredictable ways. SunOS 5.10 26 Mar 2001 preap(1)
All times are GMT -4. The time now is 08:58 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy