How to run a script in the 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 new URLs in wget-download-link.txt every 30 seconds, and if the new URL is loaded using wget, the problem is that when I try to run this script on Putty like this

 /root/wget/wget_download.sh --daemon 

it still works in the foreground, I can still see the terminal output. How to make it work in the background?

+5
source share
7 answers

Try the following:

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

It will go into the background, so when you close the Putty session, it will still work and will not send messages to the terminal.

-4
source

In OpenWRT, by default there is neither nohup nor screen , so the solution with only built-in commands should be to start the subshell using brackets and put it in the background with & :

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

you can easily test this structure on your desktop, for example, using

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

... and then close the console before 15 seconds have ended, you will see that the commands in brackets continue to execute after closing the console.

+6
source

The following command will also work:

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

Thus, you do not need to install the nohub command in the limited memory space of the router used for OpenWrt.

I found this some years ago. He works.

+2
source

You can use nohup .

 nohup yourscript.sh 

or

 nohup yourscript.sh & 

Your script will continue to work even if you close the putty session and all the output will be written to a text file in the same directory.

nohup is often used in conjunction with the nice command to start lower priority processes.

 nohup nice yourscript.sh & 

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

0
source

& at the end of the script should be enough if you see the output from the script, this means that stdout and / or stderr not closing or redirecting to /dev/null

You can use this answer:

How to redirect all output to / dev / null

0
source

I use openwrt merlin, and the only way to get it working is to use 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 a charm

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

0
source

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

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

Source: https://habr.com/ru/post/1209827/


All Articles