4

How can this be achieved in sh (not bash!)?

nohup sh -c 'helper-bcpy.sh "$0" "$1" "$2" "$3"' "$num" "$full_path" "$log" "$log_finished" >/dev/null &

I would have expected it to send the process in background (which it does) and detach it from the terminal (which it doesn't do — if I close the terminal from where I launched the script, helper-bcpy.sh won't fully execute although it seems the time-consuming wget call gets executed correctly...).

I know I could use disown but I need to do this in sh not bash...

#!/bin/sh

# $num $full_path $log $log_finished - computed before from parameters received by the script

nvram set led_disable=1
service restart_leds >/dev/null

nohup sh -c 'helper-bcpy.sh "$0" "$1" "$2" "$3"' "$num" "$full_path" "$log" "$log_finished" >/dev/null &

echo "copy process launched..."
exit 0

helper-bcpy.sh looks like this:

#!/bin/sh

wget -m -np -nH -nv --cut-dirs="$1" "$2" 1>/dev/null 2>"$3"
nvram set led_disable=0
service restart_leds
mv "$3" "$4"

exit 0

I need to make this work on an Asus router that uses the Merlin firmware

  • 2
    What is your /bin/sh symlinked to ? Which OS ? Do ls -l /bin/sh . You likely want to use something other, like screen to launch it in background. – Sergiy Kolodyazhnyy Feb 23 '17 at 18:45
  • 1
    Have you tried running the calling script with nohup, instead of using nohup in that script? – Tim Kennedy Feb 23 '17 at 18:47
  • 1
    @Serg: sh is symlinked to BusyBox and the system is reported by uname as follows: Linux AC66U 2.6.22.19 #1 Fri Feb 3 00:20:05 EST 2017 mips ASUSWRT-Merlin. I followed your advice and installed screen and I can accomplish my goal this way. – popliviustefan Feb 23 '17 at 23:17
  • @TimKennedy: I also tried what you suggested but obtained the same effect as before: process gets backgrounded but remains attached to the terminal (if I closed it, the script stops). My problem is solved but I'm still wondering why my original solution did not work, as I read that it should have accomplished exactly what I wanted. Thank you both for your suggestions! – popliviustefan Feb 23 '17 at 23:26
  • 1
    @tim-kennedy, redirecting stdout and/or stderr is optional, not required. By default they will go to nohup.out – bsd Feb 28 '17 at 19:54
0
#!/bin/sh
your_program &
disown %1

Works in dash, that is the closest to the POSIX shell that I know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.