NOHUP REDIRECTION NOT WORKING WORKAROUND – BUG PERHAPS?

If your running a script that redirects to files
Then you have nohup wrapping that script

Showing by Example:

Here is the contents of this script “record-space.sh”:

#!/bin/bash
while true
do
date >> /tmp/test
df >> /tmp/test
sleep 1
done

 What we want:

What we want to do is run this in the background with nohup because then we can close the shell and it will continue to run:
We can launch it like this

# nohup ./record-space.sh &

or:

# nohup ./record-space.sh > other-save-point.txt 2>&1 &

or:

# nohup ./record-space.sh > other-save-point.txt &

NOTE: technically you wouldnt want the nohups that redirect to other-save-point, because we dont want nohup.out output either way, but im just showing the “bug
What should happen:

The script record-space.sh will run in the background because of nohup (so you can turn off the shell and it will still run). This script should record the output of the “date” and disk space from “df” every second into a file called /tmp/test. This file should grow contniously and be appended upon.
The output of nohup.out or other-save-point.txt (depending on which nohup command you ran) should be empty. (they will get created as 0 size files)
Our redirects specifically in the script tell the system to save date and df to /tmp/test and nowhere else.

HOWEVER…

What actually happens:

All of the output goes(appends) to nohup.out or other-save-point, and nothing saves to /tmp/test (/tmp/test is made but its empty). Why? I dont know and I dont care.

How to fix it:

Well here is a hint: bash -c “command” runs anything in a bash shell. So i was thinking start a sub-shell (not one with parenthesis but a sub-bash-shell with nohup) like this:

nohup bash -c "./record-space.sh" &

or:

# nohup ./record-space.sh > other-save-point.txt 2>&1 &

or:

# nohup ./record-space.sh > other-save-point.txt &

 

Leave a Reply

Your email address will not be published. Required fields are marked *