Find the process was run using nohup or ksh.

Hi,

I want to write a script which should be run only on foreground. Is there any way that the script can check itself whether it was run using nohup or ksh and if the user runs the script using nohup then it should prompt the user to run it using ksh?

If (The user triggers the script using nohup)
{
The script should echo that the script should be run using ksh (ie, foreground) and exit.
}
Else
{
The script should run fine.
}

Please help. Thanks in advance.
RRVARMA

---------- Post updated at 06:59 PM ---------- Previous update was at 05:55 PM ----------

I tried giving something like this.. the script name is nohup_ksh_testing.sh..

#! /usr/bin/ksh

set -xe

echo "This Process is designed and developed to run on foreground.";

fg %1

echo "This Process is currently running in foreground.";

i ran this as

>> ksh nohup_ksh_testing.sh

I got the result as

$ ksh nohup_ksh_testing.sh
+ echo This Process is designed and developed to run on foreground.
This Process is designed and developed to run on foreground.
+ fg %1
$

I tried to run the same script using nohup as
>> nohup nohup_ksh_testing.sh > testing.log &
I got the result as

$ more testing.log
+ echo This Process is designed and developed to run on foreground.
This Process is designed and developed to run on foreground.
+ fg %1
$

Neither of the case got the script to echo the second text ie,

echo "This Process is currently running in foreground.";

which is after "fg %1" command.. :confused:

Try this.

#!/bin/ksh
MYTTY="`tty`"   # Terminal or "not a tty"
# Exit if we are in background
if [ ! -t 1 ]
then
        if [ ! "${MYTTY}" = "not a tty" ]
        then
                # Message user terminal
                echo "Error: Do not start in background" > ${MYTTY} 
        else
                # Message to user email
                echo "Error: Do not start from cron"
        fi
        exit
fi

Thanks a lot.. it worked.. :smiley:

---------- Post updated at 07:52 PM ---------- Previous update was at 07:48 PM ----------

Hi Methyl,

I'm just curious to know what is this doing exactly.. especially in this step..

if [ ! -t 1 ]

Thanks a lot again.. :slight_smile:

See "man test".
We are asking if channel 1 is associated with a terminal. If it is not, then we are in background.

Thankyou.. i got it.. :slight_smile: :b: