orlea
orlea

Reputation: 635

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

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 ?

Upvotes: 9

Views: 19054

Answers (9)

rubo77
rubo77

Reputation: 20865

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.

Upvotes: 19

vrqq
vrqq

Reputation: 576

In another way these code would tried:

ssh [email protected] "/jffs/your_script.sh &"

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

Upvotes: 0

Ben Li
Ben Li

Reputation: 101

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.

Upvotes: 0

emj365
emj365

Reputation: 2314

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

opkg update
opkg install coreutils-nohup
nohup yourscript.sh &

Upvotes: 2

PlagTag
PlagTag

Reputation: 6439

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/]

Upvotes: 1

sep
sep

Reputation: 29

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.

Upvotes: 2

Daniofb
Daniofb

Reputation: 71

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

Upvotes: 1

Chankey Pathak
Chankey Pathak

Reputation: 21676

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

Upvotes: 0

jcbermu
jcbermu

Reputation: 571

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.

Upvotes: -4

Related Questions