How to run a script in background (linux openwrt)?

orlea picture orlea · Dec 24, 2014 · Viewed 12.8k times · Source

I have this script:

#!/bin/sh
while [ true ] ; do
    urlfile=$( ls /root/wget/wget-download-link.txt | head -n 1 )
    dir=$( cat /root/wget/wget-dir.txt )
    if [ "$urlfile" = "" ] ; then
        sleep 30
        continue
    fi

    url=$( head -n 1 $urlfile )
    if [ "$url" = "" ] ; then
        mv $urlfile $urlfile.invalid
        continue
    fi

    mv $urlfile $urlfile.busy
    wget -b $url -P $dir -o /www/wget.log -c -t 100 -nc
    mv $urlfile.busy $urlfile.done
done

The script basically checks for any new URLs at wget-download-link.txt for every 30 seconds and if there's a new URL it'll download it with wget, the problem is that when I try to run this script on Putty like this

/root/wget/wget_download.sh --daemon

it's still running in the foreground, I still can see the terminal output. How do I make it run in the background ?

Answer

rubo77 picture rubo77 · Oct 2, 2016

In OpenWRT there is neither nohup nor screen available by default, so a solution with only builtin commands would be to start a subshell with brackets and put that one in the background with &:

(/root/wget/wget_download.sh >/dev/null 2>&1 )&

you can test this structure easily on your desktop for example with

(notify-send one && sleep 15 && notify-send two)&

... and then close your console before those 15 seconds are over, you will see the commands in the brackets continue execution after closing the console.