LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash script not working as expected when executed at startup (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-not-working-as-expected-when-executed-at-startup-4175614511/)

kuhnto 09-25-2017 06:46 PM

bash script not working as expected when executed at startup
 
I have the following test script that does not behave as I would expect when I try starting it at boot. My overall script is larger, but this is the essence of the issue at hand. What it is doing is turning on a light (just to verify the script is running), waiting a few seconds, starting a co-process for linphonec, a CLI based SIP phone client, and then sends the "call" command to the co-process for linphone to dial (my cell number replaced with a fake number).
Code:

    #!/bin/bash

    #Turn on the LED Light
    gpio mode 3 out
    gpio write 3 1

    #sleep
    sleep 5

    #Intial coproc startup for Linphonec
    coproc linphonec
    sleep 4
    echo "call 5555555555" >&"${COPROC[1]}"
    while true
    do
        #[DOES OTHER STUFF HERE]
            sleep 0.05
    done

I am running this on Raspian as a root user. If I run this from a putty CLI, it works as expected, But my goal is for the script to start at boot. If I put the following in the rc.local and reboot:
Code:

    /usr/local/bin/test.sh &
I see that both the script and linphonec are running:
Code:

    root      1936    1  0 15:26 ?        00:00:00 /bin/bash /usr/local/bin/test.sh
    root      2016  1936  1 15:26 ?        00:00:00 linphonec

The script turns on the light, but the call does not get placed. I am wondering if this has to do with coproc and how it works? I have also tried putting it in a CRON job to start as shown below with the same results:
Code:

    SHELL=/bin/bash
    @reboot /bin/sleep 10 ; /usr/local/bin/test.sh &

The light will turn on, but no call. It is interesting to note that if I kill the script process using "kill", the linphonec process is not killed. If I terminate the process while it is running in a putty terminal using ctrl-c, both processes are stopped.

So how can I get this script to operate correctly at startup? I am sure it has something to do with shells and how they behave, but I do not know much about this.

Emerson 09-25-2017 06:56 PM

Bootup script is not running in your user environment, there is no $PATH set, no executables will be found. Meaning if you try to execute foo it wont' be found, you need to execute /full/path/to/foo.

kuhnto 09-26-2017 05:42 AM

Quote:

Originally Posted by Emerson (Post 5762923)
Bootup script is not running in your user environment, there is no $PATH set, no executables will be found. Meaning if you try to execute foo it wont' be found, you need to execute /full/path/to/foo.

Are you referring to the coproc call? So it should be "coproc /usr/bin/linphonec"? Both my script and linphonec start up when called from the rc.local, so I am not sure that is the issue. I cal do a ps and see them running. I posted the ps output in my main post.

Fergupicus 09-26-2017 05:50 AM

When I wanted to do this recently I just used cronjob/crontab and put a ./Path/To/Script in the config file.

Hope this helps :)
-Fergus

kuhnto 09-26-2017 10:43 AM

Quote:

Originally Posted by Fergupicus (Post 5763043)
When I wanted to do this recently I just used cronjob/crontab and put a ./Path/To/Script in the config file.

I have tried this too. The same think occurs, the script runs, starts linephonec, but no call. I even put a sleep in the cronjob to ensure the PBX (Running on the same board) is started up before starting the SIP client.

Is there anything special about

Code:

echo "call 5555555555" >&"${COPROC[1]}"
That would not work in a script started without being in a console?

One thing could be that the PBX (IncrediblePBX) that is running on the board has not started fully by the time the script runs, this no call. Would me adding a sleep in the cronjob still allow for the PBX to startup?

ondoho 09-26-2017 11:01 AM

Quote:

Originally Posted by kuhnto (Post 5763041)
Are you referring to the coproc call?

no, s/he was refering to everything that is called from your script, every command that is not a shell builtin.
quick glance, i'd say: gpio, coproc.

lsalab 09-26-2017 01:35 PM

If you have the environment all set up in your root user home directory, you could try to include your bash profile to the beginning of the script. Try to include the '.bashrc' and/or '.profile' from your home directory.

Code:

. /root/.bashrc
or

Code:

. /root/.profile
that should include all the required environment to run the script as if you were running it through putty.

kuhnto 09-26-2017 07:54 PM

I finally figured out the issue. After I figured out how to generate logs on linphonec, I notice the following on the first line when run from putty:
Code:

ortp-message-Using (r/w) config information from /root/.linphonerc
And When I ran at startup:
Code:

ortp-message-Using (r/w) config information from (null)/.linphonerc
So it looks like it could not get to the config file located in /root. The config file is generated by default if there is not one, so it seemed like it could not read the existing config file (which I had a port change) and looked at a new default one. Without the config file port change, nothing would work. I do not know enough about Linux to know why this was happening. But I ended up putting a new copy of the config in a regular place and now, it can read and work with the new config file at boot.

Thanks everyone!

michaelk 09-26-2017 08:00 PM

coproc is a builtin bash command (as of version 4).

Did you create a proper root account or are you logging in as the default user pi?

Did you create the cronjob as root or pi?

About the only thing I can think of at the moment is that you might want to specify the configuration file in the script.

Code:

coproc linphonec -c /path/to/.linphonec
I see that you already figured it out...


All times are GMT -5. The time now is 11:28 AM.