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

9

9

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 ?

Humanism answered 24/12, 2014 at 9:37 Comment(1)
You can use the & operator at the end of your command to run it in the background.Pippin
W
-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.

Wealthy answered 24/12, 2014 at 9:39 Comment(4)
although your process will start in background but still OP will see the output from script on terminal.Fauna
The complete command should be : nohup /root/wget/wget_download.sh >/dev/null 2>&1 &Wealthy
I hadn't write it right the first time, so I edited it to add the /dev/null 2>&1. It was my fault.Wealthy
The accepted answer should be the (command) & solution. There is no nohup on OpenWrt see this: bitzof.me/doku.php?id=electronics:wr703n:nohupHummocky
J
19

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.

Jara answered 2/10, 2016 at 21:49 Comment(1)
Just leaving this here, it's 2023, opkg has nohup: opkg install coreutils-nohupCaz
V
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.

Vixen answered 17/10, 2015 at 21:21 Comment(2)
could you explain why we need two &?Gain
@AllanRuin There exist no reasons to have two &s.Underline
T
2

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

opkg update
opkg install coreutils-nohup
nohup yourscript.sh &
Thingumajig answered 21/8, 2019 at 13:28 Comment(0)
I
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

Ibbison answered 24/12, 2014 at 10:3 Comment(0)
H
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/]

Hazaki answered 10/1, 2019 at 20:30 Comment(0)
A
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

Arrangement answered 24/12, 2014 at 9:49 Comment(0)
L
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.

Lowboy answered 18/5, 2020 at 15:21 Comment(0)
L
0

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)

Lunge answered 12/2, 2022 at 14:7 Comment(0)
W
-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.

Wealthy answered 24/12, 2014 at 9:39 Comment(4)
although your process will start in background but still OP will see the output from script on terminal.Fauna
The complete command should be : nohup /root/wget/wget_download.sh >/dev/null 2>&1 &Wealthy
I hadn't write it right the first time, so I edited it to add the /dev/null 2>&1. It was my fault.Wealthy
The accepted answer should be the (command) & solution. There is no nohup on OpenWrt see this: bitzof.me/doku.php?id=electronics:wr703n:nohupHummocky

© 2022 - 2024 — McMap. All rights reserved.