8

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 ?

orlea
  • 547
  • 2
  • 5
  • 13

9 Answers9

17

In OpenWRT (before 2023) 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.

rubo77
  • 19,527
  • 31
  • 134
  • 226
2

The following command will also work:

((/root/wget/wget_download.sh)&)&

This way you don't have to install the 'nohub' command in the tight memory space of the router used for OpenWrt.

I found this somewhere several years ago. It works.

sep
  • 29
  • 2
1

The &at the end of script should be enough, if you see output from the script it means, that stdout and/or stderr is not closed, or not redirect to /dev/null

You can use this answer:

How to redirect all output to /dev/null

Community
  • 1
  • 1
Daniofb
  • 71
  • 6
1

I am using openwrt merlin and the only way to get it working was using the crud cron manager[1]. Nohub and screen are not available as solutions.

cru a pinggw "0 * * * * /bin/ping -c 10 -q 192.168.2.254"

works like charm

[1][https://www.cyberciti.biz/faq/how-to-add-cron-job-on-asuswrt-merlin-wifi-router/]

PlagTag
  • 6,107
  • 6
  • 36
  • 48
1

https://openwrt.org/packages/pkgdata/coreutils-nohup

opkg update
opkg install coreutils-nohup
nohup yourscript.sh &
emj365
  • 2,028
  • 2
  • 19
  • 24
0

You can use nohup.

nohup yourscript.sh

or

nohup yourscript.sh &

Your script will keep running even if you close your putty session, and all the output will be written to a text file in same directory.

nohup is often used in combination with the nice command to run processes on a lower priority.

nohup nice yourscript.sh &

See: http://en.wikipedia.org/wiki/Nohup

Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133
0

For busybox in Openwrt Merlin system, I got a better solution which combined cru and date command

cru a YOUR_UNIQUE_CRON_NAME "`date -D '%s' +'%M %H %d %m *' -d $(( \`date +%s\`+2*60 ))` YOUR_CMD_HERE"

which add a cron job running 2 minutes later, and only run once.

Inspired by PlagTag's idea.

Ben Li
  • 101
  • 1
  • 2
0

In another way these code would tried:

ssh admin@192.168.1.1 "/jffs/your_script.sh &"

Simple and without any programs like nohup screen...
(BTW: worked on Asus-Merlin firmware)

vrqq
  • 448
  • 3
  • 8
-4

Try this:

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

It will go to the background so when you close your Putty session, it will be still running, and it won't send messages to the terminal.

jcbermu
  • 557
  • 6
  • 12
  • although your process will start in background but still OP will see the output from script on terminal. – SMA Dec 24 '14 at 09:42
  • The complete command should be : nohup /root/wget/wget_download.sh >/dev/null 2>&1 & – jcbermu Dec 24 '14 at 09:47
  • I hadn't write it right the first time, so I edited it to add the /dev/null 2>&1. It was my fault. – jcbermu Dec 24 '14 at 10:01
  • 1
    The accepted answer should be the (command) & solution. There is no nohup on OpenWrt see this: http://bitzof.me/doku.php?id=electronics:wr703n:nohup – bownie Jan 15 '17 at 15:39