Shell script validation using process.

Hi All,
I have a shell script in Linux and it will be invoked by 2 methods,

1) An automated background process .
2) Logining into the box and directly executing the script.

I need to put a validation in my shell script such that it will be executed successfully on when an automated background process invokes it. When any user login and invoke it manually it should throw an error and exit. Please advise how this can be achieved inside my shell script

Shell logins will have terminal devices, things created by cron and the like won't.

if /bin/stty -F /dev/tty >/dev/null 2>/dev/null
then
        echo "Cannot run this program from a terminal" >&2
        exit 1
fi
1 Like

Thanks a lot for your reply. If I need to validate whether the process is invoked only from a particular parent (i,e automated background parent process p1), then how can I do the validation in the shell script?

How to get that information depends on your system. What is it?

Hi,
Actually, the script is invoked in background from harvest. So, In order to identify whether the script is invoked only from harvest and not in foreground or through anyother background process, I got the $PPID in myscript.ksh, it gives the details as,

myid1 22511 22510 0 02:53 ? 00:00:00 /usr/bin/ksh /tmp/har37sbp6
myid1 23346 22511 02:54 ? 00:00:00 /bin/ksh /home/myid1/myscript.ksh myid1 23366 23346 0 02:54 ? 00:00:00 grep 22511

But the process ("/usr/bin/ksh /tmp/har37sbp6") is forked by a parent harvest process,

myid1 22510 25146 0 02:53 ? 00:00:00 /host1harvest/dstart/harrefreshd 50000 /host1harvest/Harvestr/5

So, In the current shell script(myscrip.ksh) how can I get the parent's parent process name(i,e, 22510, /host1harvest/dstart/harrefreshd 50000 /host1harvest/Harvestr/5) and validate it for harvest string. Please advise.

The Shell way to test if you are in background:

if [ ! -t 1 ]
then
           echo "In background"
fi

Script tests whether STDOUT is a terminal.

For question 2, please post your exact Operating System and version and what Shell you are using. It also helps to know if the script will be normally invoked by root or another named user.
In the situation where I don't want a script executed by accident I usually call the script with an activation parameter and exit if the correct parameter is not supplied.

1 Like

Thanks for your reply.

OS:-
---
Linux 2.6.18-164.el5 x86_64 x86_64 x86_64 GNU/Linux

Shell:-
-----
ksh

The script will be invoked a normal user and not by root. Are there any other approaches other than parameters?

Not familiar with your O/S but if you have the "ptree" and "pgrep" commands they could prove useful.

There's infinitely many approaches, parameters are just the most reliable.

Since we know you have linux:

$ cat ppid.awk

BEGIN {
        OFS="\t"
        D=0

        do
        {
                D++

                while(getline<("/proc/" PID "/status"))
                {
                        if($1 == "Name:")       NAME=$2
                        if($1 == "PPid:")       PPID=$2
                }

                close("/proc/" PID "/status");

                print D, "PID", PID, "PPID", PPID, "NAME", NAME;

                PID=PPID;       NAME="";        PPID=""
        } while(PID>0)

        exit 1
}

$ awk -v PID="$$" -f ppid.awk

1       PID     6925    PPID    6924    NAME    bash
2       PID     6924    PPID    6921    NAME    sshd
3       PID     6921    PPID    3937    NAME    sshd
4       PID     3937    PPID    1       NAME    sshd
5       PID     1       PPID    0       NAME    init

$

Thanks a lot for your reply.

1) My current shell script(myscript.sk where I am doing validation) process is,

myid1 23346 22511 02:54 ? 00:00:00 /bin/ksh /home/myid1/myscript.ksh

So in the shell script PID=$$ will fetch the value as, PID=23346.

2) The parent process of 23346 is 22511, which is,

myid1 22511 22510 0 02:53 ? 00:00:00 /usr/bin/ksh /tmp/har37sbp6

so the $PPID will return the value as "22511" when I get it from the /proc/ PID /status system file.

3) The parent ID of 22511 process is 22510, which is,

myid1 22510 25146 002:53 ? 00:00:00 /host1harvest/dstart/harrefreshd 50000 /host1harvest/Harvestr/5

So, will I get the process name as "/host1harvest/dstart/harrefreshd" if I use the below script. Please advise. I am sorry, if I had typed wrongly in my previous posts.

What below script? You didn't post anything.

Have you tried the one I posted?

1 Like

Thanks a lot. Now, instead of calling the BEGIN inside a ppid.awk file, I planned to use it in a function get_process_name(). When I used it in a method, I am not getting the process name. How to change ppid.awk into a function in shell script? please advise.