LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-10-2019, 01:42 PM   #1
mackowiakp
Member
 
Registered: Jun 2014
Location: Poland/Gdynia
Distribution: Mageia 7, SH4, Debian
Posts: 190

Rep: Reputation: 4
Bash script not work properly from rc.local


I try to run the script below from Ubuntu "rc.local" file. The file /home/pi/ramdisk/ESP is created correctly but "while" loop seam to not work.
The same script run from CLI or cron, works properly. Any idea what I am doing wrong?

Code:
#!/bin/bash

touch /home/pi/domoticz/ESP.conf
cat /home/pi/domoticz/ESP.conf|sed '/^#/ d' > /home/pi/ramdisk/ESP
[ ! -f /home/pi/ramdisk/domoticz_on ] && exit
case $1 in
        reboot_off)
                event="reboot_off"
        ;;
        reboot_on)
                event="reboot_on"
        ;;
        *)
                event="alive"
        ;;
esac
while read ip
do
wget -q -P /home/pi/ramdisk --delete-after --connect-timeout=2 --tries=5 "http://$ip/control?cmd=event,$event" > /dev/null
done < /home/pi/ramdisk/ESP
 
Old 09-10-2019, 02:10 PM   #2
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.6
Posts: 3,635

Rep: Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206
Assumed: the .conf file exists -- so updating the timestamp thereon -- else creating an empty .conf file
(useless use of cat) to remove all comments from the empty file. Create a new file without comments (1)
if file /home/pi/ramdisk/domoticz_on does not exist, exit the script

Set the value of event according to the first parameter the script was run with. (2)

Read the new file. For each line in the file execute a wget command (3)

I don't know how you expect us to help you. We don't know what parameter is passed to the script at (2), we don't know what's in the conf file/new file (1), and most importantly we don't know what the wget command is supposed to do (3) or how it "seems to not work"

What have you tried to do to debug the script? set x? echo values? redirect the wget output to someplace you can review it instead of to /dev/null?

Please review the How_To_Ask_a_Question link in my sig.
 
1 members found this post helpful.
Old 09-10-2019, 02:46 PM   #3
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 12,307
Blog Entries: 9

Rep: Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310
Quote:
Originally Posted by mackowiakp View Post
Code:
wget -q -P /home/pi/ramdisk --delete-after --connect-timeout=2 --tries=5 "http://$ip/control?cmd=event,$event" > /dev/null
$ip has not been defined, so the wget command might fail.
 
Old 09-10-2019, 02:53 PM   #4
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.6
Posts: 3,635

Rep: Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206
Quote:
Originally Posted by ondoho View Post
$ip has not been defined, so the wget command might fail.
That may be the case, but doesn't the while read ip ... < input file define $ip? (If there is, in fact, an IP and only an IP on each line in the input file...which we don't know)

I don't use the while read < input very often; I could be wrong about that.
 
Old 09-10-2019, 02:58 PM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 12,307
Blog Entries: 9

Rep: Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310
^ you're absolutely right.
Of course, it would help to know what exactly is being passed to the while loop, and how it fills $ip, and how exactly it "doesn't work"...
 
1 members found this post helpful.
Old 09-10-2019, 03:56 PM   #6
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,000

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
is this script executable?
what does your rc.local look like?
does the script work if you run it 'manually'?

Code:
#!/bin/bash

# touch /home/pi/domoticz/ESP.conf
# why touch it?
# cat /home/pi/domoticz/ESP.conf|sed '/^#/ d' > /home/pi/ramdisk/ESP
# UUOC , the seds ok but why write a new file?
# I'll use grep below, just habit

#[ ! -f /home/pi/ramdisk/domoticz_on ] && exit
# if /home/pi/ramdisk/domoticz_on is not a file exit
# where does that file come from?
[[ ! -f /home/pi/ramdisk/domoticz_on ]] && exit
# we are using bash, not that it matters with that one

# need to see rc.local
case $1 in
        reboot_off)
                event="reboot_off"
        ;;
        reboot_on)
                event="reboot_on"
        ;;
        *)
                event="alive"
        ;;
esac
while read ip
do
   wget -q -P /home/pi/ramdisk --delete-after \
     --connect-timeout=2 --tries=5 \
     "http://$ip/control?cmd=event,$event" > /dev/null
# just break up the long line a bit
done < <( grep -v ^# /home/pi/domoticz/ESP.conf )
# the grep I typed about
#done < /home/pi/ramdisk/ESP
 
Old 09-10-2019, 04:52 PM   #7
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (Chicago area)
Distribution: CentOS, MacOS, [Open]SuSE, Raspian, Red Hat, Slackware, Solaris, Tru64
Posts: 1,478

Rep: Reputation: 154Reputation: 154
Quote:
Originally Posted by mackowiakp View Post
I try to run the script below from Ubuntu "rc.local" file. The file /home/pi/ramdisk/ESP is created correctly but "while" loop seam to not work.
The same script run from CLI or cron, works properly. Any idea what I am doing wrong?

Code:
#!/bin/bash

touch /home/pi/domoticz/ESP.conf
cat /home/pi/domoticz/ESP.conf|sed '/^#/ d' > /home/pi/ramdisk/ESP
[ ! -f /home/pi/ramdisk/domoticz_on ] && exit
case $1 in
        reboot_off)
                event="reboot_off"
        ;;
        reboot_on)
                event="reboot_on"
        ;;
        *)
                event="alive"
        ;;
esac
while read ip
do
wget -q -P /home/pi/ramdisk --delete-after --connect-timeout=2 --tries=5 "http://$ip/control?cmd=event,$event" > /dev/null
done < /home/pi/ramdisk/ESP
Two things I'd do:
  • Display the contents of "/home/pi/ramdisk/ESP"
    Code:
    LOGFILE=/tmp/log.file
    ...
    cat /home/pi/ramdisk/ESP >> ${LOGFILE}
    This could help explain what's supposed to be happening in the while loop.
  • Redirect the output of "wget" to the log file:
    Code:
    wget ...     >> ${LOGFILE}  2>&1
    Chances are you're tossing out valuable information about why nothing's happening the way you think it should be. (Like error messages from "wget".) I suspect that "$ip" is not getting defined. Try testing that "$ip" is not a null string:
    Code:
    if [ -n "$ip" ]; then
       echo "Retrieving from ${ip}..."  >> ${LOGFILE}
       wget ...                         >> ${LOGFILE} 2>&1
    else
       echo "Bad address."              >> ${LOGFILE}
    fi


HTH...
 
Old 09-10-2019, 06:16 PM   #8
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,000

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
running the script manually with

Code:
bash -x /path/to/script.sh foo
# I have no idea what the case does, so foo will
# trigger alive event
then you would see the while read do its thing
 
Old 09-10-2019, 07:05 PM   #9
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.6
Posts: 3,635

Rep: Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206
The OP has already said the script works from the command line and in cron.
I think we’ll just have to wait until they come back after trying our suggestions...all of which have been helpful, IMO
 
Old 09-10-2019, 07:52 PM   #10
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,000

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by scasey View Post
The OP has already said the script works from the command line and in cron.
I think we’ll just have to wait until they come back after trying our suggestions...all of which have been helpful, IMO
wget wont run, since no PATH and full path not used.
 
Old 09-10-2019, 08:09 PM   #11
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.6
Posts: 3,635

Rep: Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206
Quote:
Originally Posted by Firerat View Post
wget wont run, since no PATH and full path not used.
That’s a possibility.
Another question to which we don’t know the answer: Is Ubuntu of unspecified version even a SysV distribution?
 
Old 09-11-2019, 01:16 AM   #12
mackowiakp
Member
 
Registered: Jun 2014
Location: Poland/Gdynia
Distribution: Mageia 7, SH4, Debian
Posts: 190

Original Poster
Rep: Reputation: 4
Thanks @scasey ! You did not indicate exactly what I do wrong, but you showed me where to look for the obvious mistake I made.

Code:
If file /home/pi/ramdisk/domoticz_on does not exist...
The problem was semaphore file /home/pi/ramdisk/domoticz_on. Domoticz default startup script contain line "sleep 15" at the beginning. In this case of course, rc.local is executed before this "sleep" command ends. And Domoticz is off during rc.local execution. So script exit without executing "while" loop.
THX everybody! Solved!
 
Old 09-11-2019, 01:54 AM   #13
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.6
Posts: 3,635

Rep: Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206
Quote:
Originally Posted by mackowiakp View Post
Thanks @scasey ! You did not indicate exactly what I do wrong, but you showed me where to look for the obvious mistake I made.
I'm happy to have pointed you in the right direction to find the solution.
 
  


Reply

Tags
bash, ubuntu 14.04


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Pipe output of local script, ran on remote machine, to local python script trendal Programming 17 07-04-2016 01:56 AM
[SOLVED] Bash script not running properly on XFCE4 startup BridgeTheMasterBuilder Linux - Software 2 07-30-2014 12:20 PM
Bash script not working properly in cron Iyyappan Linux - Server 7 12-13-2013 12:21 PM
Bash script not performing a check properly da_667 Programming 15 08-06-2012 03:16 PM
Linux bash script: sort command - not sorting properly lulwot Programming 3 04-19-2011 07:28 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration