You don't really need nohup
you know. In fact, I've personally never found a use for it.
( ./rfidreader.sh 1>&2 & ) 2>&- &
That'll detach your process from the terminal, it'll keep doing whatever it's supposed to do and should keep from breaking stuff.
Demo Script
cat <<-\DEMO >|${s=/tmp/script}
printf 'tty is %s\nparent pid is %s\npid is pid=%s\n' \
"$(tty)" "$PPID" "$$"
exec 1>&2 ; nums=$(seq 0 9)
rm ${files=$(printf "/tmp/file%s\n" $nums)}
for n in $nums ; do { for f in $files ; do
echo "Line $n" >>"$f" ; done
sleep 1 ; } ; done
#END
DEMO
Run Demo
s=/tmp/script ;chmod +x $s ;info="$(($s &)2>&- &)"
echo "$info" ; pid="${info##*=}" ; echo
while ps -p $pid >/dev/null ; do sleep 3 ; done
for f in /tmp/file[0-9] ; do
printf 'path : %s\tline count : %s\n' \
$f $(<$f wc -l)
done
Output:
tty is not a tty
parent pid is 1
pid is 12123
path : /tmp/file0 line count : 10
path : /tmp/file1 line count : 10
path : /tmp/file2 line count : 10
path : /tmp/file3 line count : 10
path : /tmp/file4 line count : 10
path : /tmp/file5 line count : 10
path : /tmp/file6 line count : 10
path : /tmp/file7 line count : 10
path : /tmp/file8 line count : 10
path : /tmp/file9 line count : 10
The above demonstrates. It builds and runs a script named /tmp/script
, chmod
's it as executable, and runs it in the &background
of a &backgrounded ( subshell )
.
The script rms /tmp/file0-9
10 files and echoes
a line every second into all 10 of them. I capture some $info
from the disowned process and present it via $(command substitution). While ps
still reports on the $pid
I capture, I know it still runs so I sleep.
When it completes, the lines in all 10 files are counted with wc.
After you invoke a process in this way you can freely close its original parent process and it will keep on trucking - it's effectively disowned.
Worth mentioning, I think, is that the process is actually initially called in $(command substitution)
and printfs
me the $info
I want so I can effectively control it. But as soon as it drops its terminal output with exec 1>&2
(which is closed in the same subshell with 2>&-
), the process escapes and I have to wait around for it on the other end. Kinda the best of both worlds, especially if you use it to handle input pipes, so long as you can wrap your mind around all of the redirections and process leaders.
Everything else is just for demonstration here. All you need to run this is the top script and:
info="$(($script_path &)2>&- &)"
NOTE: This only prints to terminal exactly what I wished to demonstrate it. As noted by the $PPID,
this process is disowned by the terminal and is a direct child of $PID 1.
nohup
issue, if you are usingbash
, you could trynice ./rfid_reader.sh </dev/null &>/dev/null & disown
as an alternative and see if this solves it. Otherwise it is surely an issue elsewhere as @terdon says.