Test if script was ran w/ nohup


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Test if script was ran w/ nohup
# 1  
Old 11-10-2012
Test if script was ran w/ nohup

I want to test if script.sh is being run with nohup ... but $0 does not contain nohup part...

purpose: script.sh should only be ran with nohup if user forgets nohup then it should echo "run with nohup" && exit.......
# 2  
Old 11-10-2012
Create an alias

One way to achieve this is by creating an alias
Code:
alias script.sh="nohup /path/script.sh"

So if someone tries to run script.sh, it will actually call the alias.
Code:
script.sh

But this approach will fail if the script is ran with path specified like below (You can put a check in your script to make sure everyone is calling the alias):-
Code:
./script.sh

Code:
/path/script.sh

I'm not sure if there are any other ways to enforce use of nohup. I hope this helps.

Last edited by Yoda; 11-10-2012 at 06:49 PM..
# 3  
Old 11-10-2012
Inside a shell script it is hard to determine if it was started by the nohup utility. However, if STDOUT or STDERR for your script is a TTY device, you will know that it was not started by nohup. If being immune to SIGHUP signals and not having STDOUT directed to a TTY device and not having STDERR directed to a TTY device is sufficient for your script to continue running, the following code may help:
Code:
#!/bin/ksh
trap '' HUP
if [ "x$(tty <&1)" != "xnot a tty" ] || [ "x$(tty <&2)" != "xnot a tty" ]
then    printf '%s: This script must be started by "nohup %s ..."\n' "$0" "$0"
        exit 1
fi
printf '%s SIGHUP ignored and STDIN & STDERR directed to non-TTYs\n' "$0"
. . . other shell commands . . .

This was written and tested with the Korn shell, but should work with bash, ksh, or sh with no changes. It won't work with csh or similar shells.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If I ran perl script again,old logs should move with today date and new logs should generate.

Appreciate help for the below issue. Im using below code.....I dont want to attach the logs when I ran the perl twice...I just want to take backup with today date and generate new logs...What I need to do for the below scirpt.............. 1)if logs exist it should move the logs with extention... (1 Reply)
Discussion started by: Sanjeev G
1 Replies

2. Shell Programming and Scripting

Saving nohup output to a file other than nohup.out

Shell : bash OS : Oracle Linux 6.4 I want to save the ouput of a nohup command to file other than nohup.out . Below are my 3 attempts. For both Attempt1 and Attempt2 , the redirection logs the output correctly to the output file. But I get the error "ignoring input and redirecting stderr to... (7 Replies)
Discussion started by: kraljic
7 Replies

3. Shell Programming and Scripting

Need bash script to ping the servers and rename the output file each time the script is ran

HI, I have a file serverlist in that all host names are placed. i have written a small script #./testping #! /bin/bash for i in `cat serverlist` do ping $i >> output.txt done so now it creates a file output.txt till here fine.. now each time i run this script the output file... (4 Replies)
Discussion started by: madhudeva
4 Replies

4. Shell Programming and Scripting

how to find whether a script ran or not

Hi, I have written a script and placed in an application and the script can be executed manually only. But somehow one of the method in the script is being called and bringing the application down. But we are not able to find any instance of script running. Is there a way to findout whether the... (1 Reply)
Discussion started by: Satyak
1 Replies

5. Shell Programming and Scripting

absolute path for a script ran with relative path

I have a script in which i want to print absolute path of the same script irrespective of path from where i run script. I am using test.sh: echo "pwd : `pwd`" echo "script name: $0" echo "dirname: `dirname $0`" when i run script from /my/test/dir/struct as ../test.sh the output i... (10 Replies)
Discussion started by: rss67
10 Replies

6. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

7. UNIX for Dummies Questions & Answers

Script ran by job scheduler fails from the 15th to the 20th of the month

Hi, I have a script that finds the application logs from the previous day and sends it to another server via ftp. The code is something like this: yest_date=`TZ=CST+24 date "+%b %d"` logdir=/app/logs logs=app*.log tmpdir=/tmp cd $logdir for i in `ls -1 $logs` do chkstr=`ls -1l $i | grep... (2 Replies)
Discussion started by: tatchel
2 Replies

8. Shell Programming and Scripting

nohup command in the script....

I want to know how to use a nohup command in the script........ I have to run following command nohup /tmp/app/statuscheck.sh & After typing this command I will type ctrl D to come to the prompt and the that command will run in backround. Even after pressing & the command is not... (3 Replies)
Discussion started by: jayaramanit
3 Replies

9. Shell Programming and Scripting

Script to ran ant.xml file

Hi All, I want to login to Oracle Application Server and run ant.xml file. Can someone propose a script for that task? Thanks, Abraham123 (0 Replies)
Discussion started by: Abraham123
0 Replies

10. Shell Programming and Scripting

Reg: Shell script ran using informatica

Hi all, I am not sure whether this is the right place to ask this question...:) I am working in Informatica PowerCenter 8.1.1 tool and my server is on UNIX. I have got a shell script to copy files from one folder to another. When I run the script directly from UNIX prompt it is taking 60... (0 Replies)
Discussion started by: sam99
0 Replies
Login or Register to Ask a Question