nohup multiple lines?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting nohup multiple lines?
# 1  
Old 02-26-2011
Question nohup multiple lines?

Hi all,

I have the following problem. I have script1.sh that has a couple lines. This script is a polling script that will loop indefinitely. I have script2.sh which is:

nohup > poll.log script1.sh &

Is there any way I can get rid of script2.sh and have script1.sh nohup all of its lines and write to a log file?
# 2  
Old 02-26-2011
You can't use nohup on lines, just scripts and programs, but you can certainly make a script act like it's had nohup done on it.
Code:
#!/bin/sh
# Trap the HUP signal so it doesn't kill me
trap "" HUP
# Redirect stderr to /dev/null
exec 2>/dev/null
# Redirect stdin from /dev/null
exec 0</dev/null

while true
do
        stuff
done

# 3  
Old 02-26-2011
Thanks for the script, but is there any way to redirect this all to a log file?
# 4  
Old 02-26-2011
If you read the code I already gave you you'll find a pattern. I've showed you how to redirect file descriptor 2, standard error, to the file /dev/null. To redirect file descriptor 1, standard output, to poll.log, add another line that redirects that too.
Code:
# Redirect stdout to logfile
exec 1>poll.log

# 5  
Old 02-26-2011
Thank you very much again. However, the "fake" nohup doesn't seem to be working as it console sits there until I break. Is this possible?
# 6  
Old 02-26-2011
Backgrounding is the the shell's job, nohup never did that. If you run it as 'command &' it will run in the background. If you want it to do it all by its lonesome...hm.

It would be really helpful to know what your shell and system is.

This should work in many shells:
Code:
#!/bin/sh

(       trap "" HUP
        exec 2> /dev/null
        exec 0< /dev/null
        exec 1>poll.log
        while true
        do
                stuff
        done        ) &
echo "Running as pid $!" >&2

If it has to work in BASH or KSH, you might need this:
Code:
#!/bin/sh

(       trap "" HUP
        exec 2> /dev/null
        exec 0< /dev/null
        exec 1>poll.log
        while true
        do
                stuff
        done        ) & disown
echo "Running as pid $!" >&2

This User Gave Thanks to Corona688 For This Post:
# 7  
Old 02-26-2011
That works out great. Thank you very much.
This User Gave Thanks to Annorax For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing carriage returns from multiple lines in multiple files of different number of columns

Hello Gurus, I have a multiple pipe separated files which have records going over multiple Lines. End of line separator is \n and records going over multiple lines have <CR> as separator. below is example from one file. 1|ABC DEF|100|10 2|PQ RS T|200|20 3| UVWXYZ|300|30 4| GHIJKL|400|40... (7 Replies)
Discussion started by: dJHa
7 Replies

2. Shell Programming and Scripting

Removing multiple lines from input file, if multiple lines match a pattern.

GM, I have an issue at work, which requires a simple solution. But, after multiple attempts, I have not been able to hit on the code needed. I am assuming that sed, awk or even perl could do what I need. I have an application that adds extra blank page feeds, for multiple reports, when... (7 Replies)
Discussion started by: jxfish2
7 Replies

3. Shell Programming and Scripting

Merging multiple lines to columns with awk, while inserting commas for missing lines

Hello all, I have a large csv file where there are four types of rows I need to merge into one row per person, where there is a column for each possible code / type of row, even if that code/row isn't there for that person. In the csv, a person may be listed from one to four times... (9 Replies)
Discussion started by: RalphNY
9 Replies

4. Shell Programming and Scripting

Reading multiple values from multiple lines and columns and setting them to unique variables.

Hello, I would like to ask for help with csh script. An example of an input in .txt file is below, the number of lines varies from file to file and I have 2 or 3 columns with values. I would like to read all the values (probably one by one) and set them to independent unique variables that... (7 Replies)
Discussion started by: FMMOLA
7 Replies

5. 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

6. Shell Programming and Scripting

Awk match multiple columns in multiple lines in single file

Hi, Input 7488 7389 chr1.fa chr1.fa 3546 9887 chr5.fa chr9.fa 7387 7898 chrX.fa chr3.fa 7488 7389 chr21.fa chr3.fa 7488 7389 chr1.fa chr1.fa 3546 9887 chr9.fa chr5.fa 7898 7387 chrX.fa chr3.fa Desired Output 7488 7389 chr1.fa chr1.fa 2 3546 9887 chr5.fa chr9.fa 2... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

7. Shell Programming and Scripting

Script to find & replace a multiple lines string across multiple php files and subdirectories

Hey guys. I know pratically 0 about Linux, so could anyone please give me instructions on how to accomplish this ? The distro is RedHat 4.1.2 and i need to find and replace a multiple lines string in several php files across subdirectories. So lets say im at root/dir1/dir2/ , when i execute... (12 Replies)
Discussion started by: spfc_dmt
12 Replies

8. Shell Programming and Scripting

search and replace, when found, delete multiple lines, add new set of lines?

hey guys, I tried searching but most 'search and replace' questions are related to one liners. Say I have a file to be replaced that has the following: $ cat testing.txt TESTING AAA BBB CCC DDD EEE FFF GGG HHH ENDTESTING This is the input file: (3 Replies)
Discussion started by: DeuceLee
3 Replies

9. UNIX for Dummies Questions & Answers

grep command to find multiple strings in multiple lines in a file.

I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. I am writing it another way to make it clear, I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings, header.h... (2 Replies)
Discussion started by: ritikaSharma
2 Replies

10. Shell Programming and Scripting

using mutiple "nohup" to execute multiple commands.

I need to run multiple commands on remote server using the nohup... I have tried 2 options 1) rsh <SERVER_NAME> -n "nohup perl $SCRIPTS_DIR/abc.pl ; $SCRIPTS_DIR/xyz.ksh & " & 2) rsh <SERVER_NAME> -n "nohup perl $SCRIPTS_DIR/abc.pl & nohup $SCRIPTS_DIR/xyz.ksh & " & I need to know if... (2 Replies)
Discussion started by: aster007
2 Replies
Login or Register to Ask a Question