Problem with nohup use in scripting

I am trying to execute following code:

alarm_file_array="test1.alarms test2.alarms test3.alarms test4.alarms"
for file in ${alarm_file_array[@]}
do
	nohup tail -f $file |awk 'NR>10' >> output.alarms 2>/dev/null &
done

Whenever it tries to execute nohup command it hangs because of the following prompt in output.

linus> Sending output to nohup.out

I tried to suppress nohup output using 2>/dev/null but still its not working.

Any ideas/suggestions are appreciated.

I see a few things problematic with this:

alarm_file_array="test1.alarms test2.alarms test3.alarms test4.alarms"
for file in ${alarm_file_array[@]}
do
	nohup tail -f $file |awk 'NR>10' >> output.alarms 2>/dev/null &
done

First and foremost, if you did get all three of these pipe chains writing to output.alarms independently, they wouldn't know or care about each other -- they'd just write whatever they want, wherever they want, whenever they want, potentially overwriting each others' contents. You should have a separate logfile for each tail.

Second of all, 'tail' and 'awk' are separate processes. The warning message is coming from the standard error of tail, and you're only redirecting awk.

Third of all, you're only putting awk in the background, not tail, which is why tail hangs -- not because of nohup.

Fourth, it makes no sense to use nohup at the start of a pipe chain, because it does what it says it's doing -- sends its stdout into nohup.out and not into the pipe feeding into awk.

You can achieve a similar effect to nohup by redirecting all streams to /dev/null, which will work for the entire pipe chain. You can also put an entire subshell in the background with ( ) .

alarm_file_array="test1.alarms test2.alarms test3.alarms test4.alarms"
for file in ${alarm_file_array[@]}
do
        (   exec 0</dev/null # stdin
            exec 1>/dev/null # stdout
            exec 2>/dev/null # stderr
            tail -f $file |awk 'NR>10' >> ${file}.out ) &
      disown
done

The 'disown' is so the script won't wait for the background jobs to finish before it quits. Some shells don't have disown, but those wouldn't wait for background jobs anyway.

Thanks for your valuable inputs.

The above solution works fine but not for my SUN platforms. If I execute the above commands and close the putty session then the tail command is also stopped.

I want a solution which should keep executing as a background process even if the putty session from which this is invoked is closed.

Hmm, try this:

alarm_file_array="test1.alarms test2.alarms test3.alarms test4.alarms"
for file in ${alarm_file_array[@]}
do
        (   exec 0</dev/null # stdin
            exec 1>/dev/null # stdout
            exec 2>/dev/null # stderr
            trap "" HUP
            tail -f $file |awk 'NR>10' >> ${file}.out ) &
      disown
done

trap "" HUP works to prevent child processes dying with SIGHUP in my shell. Any more complex trap won't work, since child processes can't inherit actual shell code from the shell, just SIG_IGN.