Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

shell

ash

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 ?

like image 572
orlea Avatar asked Dec 24 '14 09:12

orlea


1 Answers

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.

like image 95
rubo77 Avatar answered Sep 28 '22 15:09

rubo77