Find the process was run using nohup or ksh.


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Find the process was run using nohup or ksh.
# 1  
Old 08-10-2009
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..

Code:
#! /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
Code:
$ 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
Code:
$ 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.. Smilie
# 2  
Old 08-10-2009
Try this.

Code:
#!/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

# 3  
Old 08-10-2009
yyooo.. it worked.. :)

Thanks a lot.. it worked.. Smilie

---------- 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.. Smilie
# 4  
Old 08-10-2009
See "man test".
We are asking if channel 1 is associated with a terminal. If it is not, then we are in background.
# 5  
Old 08-10-2009
Lightbulb

Thankyou.. i got it.. Smilie Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh behavior in scripts spawned w/nohup

I have a need to run any number of identical scripts simultaneously, so I've created a driver script which reads a template script, edits these appropriately and then submits them via nohup. The spawned scripts all check to see at some point how many of their number are running and once the count... (7 Replies)
Discussion started by: safedba
7 Replies

2. UNIX for Advanced & Expert Users

Running process in nohup

Hi All, I am facing issue in running a process in nohup. I ran a process in terminal since it is taking too long to complete I need to make it as background and nohup. I tried below and was able to make it in back ground 1. Cntrl + Z 2. bg I am using Korn Shell so disown is not working... (5 Replies)
Discussion started by: arunkumar_mca
5 Replies

3. Shell Programming and Scripting

can a nohup'ed ksh script interact with user

I have a long running ksh script that I need to run with "nohup" in the backgound which is all well and good but at the very start of the script it needes to output to the screen to query the user and accept a response before continuing. Any thoughts on how to accomplish this other than... (11 Replies)
Discussion started by: twk
11 Replies

4. UNIX for Dummies Questions & Answers

Run process with nohup every certain time

Hi, I need execute a script every 30 minutes. As might be done without using cron Thx. (6 Replies)
Discussion started by: pepeli30
6 Replies

5. Shell Programming and Scripting

find pid of process run in specific location

Hello, I have a process a.out that runs from /a and /b How can I get the pid of the one running from /a ps -C /a/a.out does not work Thanks! (4 Replies)
Discussion started by: JCR
4 Replies

6. UNIX for Advanced & Expert Users

nohup and background process

What is the difference between running a process using nohup and running a process in background ? Please explain (6 Replies)
Discussion started by: srksn
6 Replies

7. Shell Programming and Scripting

stoped job using ctrl+z now want to run in nohup

once i have stoped process by ctrl+z then i want to run it again in nohup plz help me out. (6 Replies)
Discussion started by: RahulJoshi
6 Replies

8. Shell Programming and Scripting

pid of nohup process

I want to print the pid of a nohup process to a file so later I can use the list of pid's in that file to stop the background processes again. I use ksh on AIXv5.3: nohup /start/script.ksh 1>/dev/null 2>&1 print $$ > .pid nohup /start/script2.ksh 1>/dev/null 2>&1 print $$ >> .pid But... (2 Replies)
Discussion started by: rein
2 Replies

9. Shell Programming and Scripting

Is there a way to make this find job run as one process, more efficiently?

Hi, unixers, I'm doing something like find -type f -name '*.gif' | xargs rm & find -type f -user root | xargs chown idealo & wait #do more stuff on a very large dir. I'm thinking this would be more efficient if it was running as a single process that did one thing for the first condition,... (1 Reply)
Discussion started by: tphyahoo
1 Replies

10. Shell Programming and Scripting

nohup process hangs

Hi All, I tried searching for this, but I have yet to find anything useful. So here goes, if a script executed from another script with nohup & hangs, does it affect the parent script? Reason I ask, we have a windows box with NFS, and we use it to store some of our files. Currently, I mount the... (2 Replies)
Discussion started by: Sully
2 Replies
Login or Register to Ask a Question