0

I have one strange sutuation to solve, I have the software compiled in Python; so, to Run it I'm using from the first scrip (named startup.sh), made by the following code:

#!/bin/sh
./install_deps.sh
python3 app.py >/dev/null 2>&1 & exit

as you can see the first script startup.sh should run the install_deps.sh (if will NOT be the conf.txt file into the same folder) and the actual Python app.py. But here come out my problem, if I will try to run it the Python app will run perfectly but the script will jump the install_deps.sh. So, even if I will give the chmod +x or whatever to both of the script they will not communicate to each other. But If I will run separately/manually the scripts will run and works just fine. So, my question it's, why I cannot let it run or communicate install_deps.sh from startup.sh?

To help you more, I attach the install_deps.sh coding below:

#!/bin/bash
if [[ ! -f conf.txt ]]
then
    sudo apt-get install python3 -y
    sudo apt install python3-pip -y
    sudo apt-get install python3-tk -y
    sudo apt install python3-gi -y
    sudo apt python3-gi-cairo -y
fi
Niewbie
  • 21
  • 4
  • 2
    maybe add the full path to `conf.txt`, I have the impression that is not found. Or try remove the `if` as a test. – piertoni Jul 09 '21 at 07:00
  • 2
    As an aside, you can combine all the `apt install` commands into one; `apt` (and `apt-get`, which I believe is still recommended for scripting) accepts a list of packages as arguments. – tripleee Jul 09 '21 at 07:06
  • 1
    Possibly review [What exactly is Current working directory?](https://stackoverflow.com/questions/45591428/what-exactly-is-current-working-directory) – tripleee Jul 09 '21 at 07:08
  • I will try @piertoni , this kind of test didn't try yet I will report you back .... – Niewbie Jul 09 '21 at 07:14
  • Ok, I have tried to take out the command `if` into `install_deps.sh` and `startup.sh` still jump the `install_deps.sh` , after I have tried to put the full path of conf.txt `/usr/local/bin/"Python app folder/conf.txt"` and still jump. After I have tried to delete `install_deps.sh` and add the `if` coding into the `startup.sh` and it jumps all the command except the python app.. I have the feeling that the python app maybe create immediately the `conf.txt` that kind of jump all the `if` coding. There is a way to delay of few seconds the python app at least I give the time to `install_deps.sh`? – Niewbie Jul 09 '21 at 07:47
  • 1
    What is the exact output you get with `set -x` at the beginning of both scripts? Also, when you run this, are you in the same directory as all of the scripts? (This is important because you're using relative paths for everything.) – Gordon Davisson Jul 09 '21 at 07:58
  • The scripts are running fine and working and `install_deps.sh` if I run manually if there is the conf.txt file it will not run the Terminal , but if I will delete the conf.txt file `install_deps.sh` it will run the Terminal as should be. Yes everything it's happening in the same folder. But when you run the `startup.sh` and the bash will run the app.py as well; the app.py will create immediately the conf.txt. So, maybe this process it's really fast that the coding into `if` will read that the conf.txt file it's alwayst there because the app.py it create it immediately. – Niewbie Jul 09 '21 at 08:10
  • I believe that into `startup.sh` between `./install_deps.sh` and `python3 app.py >/dev/null 2>&1 & exit` , i need to add some second of delay? How? – Niewbie Jul 09 '21 at 08:13
  • No need for delay, the python command is executed only after `install_deps.sh` exits. I would try to add some debugging messages to the script: `if [[ ! -f conf.txt ]]; then echo "conf.txt not found, installing deps" >&2; [...]; else echo "conf.txt found in the current directory ($(pwd))" >&2; fi` – David Jul 09 '21 at 08:21
  • P.S. maybe a silly question, but if the Python app runs correctly, doesn't that mean that Python is already installed? I'm not familiar with apt-get, but wouldn't it just exit silently in that case? – David Jul 09 '21 at 08:28
  • Yes, the Python app it's already installed, I have created a debian package that will install the software. But when you will press the icon app for the first time to run it, if the the app will not find the conf.txt file (as first time will not exist into the app folder because will be created after), so the app will run also the `install_deps.sh` to install you all the missing dependencies and some system setting that avoid you to do it manually for the first time. So, for this reason it's important, that the `install_deps.sh` will run for the first time automatically for the end user.. – Niewbie Jul 09 '21 at 08:46
  • @David , I will try to add your code and let's see what happen..... – Niewbie Jul 09 '21 at 08:48
  • For @GordonDavisson , this it's the output that gives me `set -x` : `test@test-PC:/usr/local/bin/test_app$ set -x at_startup.sh ++ echo -ne '\033]0;test@test-PC: /usr/local/bin/test_app\007' test@test-PC:/usr/local/bin/test_app$ set -x install_deps.sh + set -x install_deps.sh ++ echo -ne '\033]0;test@test-PC: /usr/local/bin/test_app\007'` – Niewbie Jul 09 '21 at 08:52
  • 1
    @Niewbie I can't read that very well (it'd be better to edit the output into your question as a code-formatted block), but... are you typing `set -x` at the command line? You need to edit it into each of the scripts, as the second line (after the `#!` line). Also, why do you use `& exit` at the end of the startup.sh script? That lets app.py continue running in the background, so is it possible that's creating conf.txt *after* you expect (and interfering with the next time you run the script)? – Gordon Davisson Jul 09 '21 at 09:01
  • @David, I have tried to add your coding and if I run the `install_deps.sh` alone by Terminal come out the message and everything it's fine, but if I try to run it by the `startup.sh` doesn't make anything except to run the app directly. It seems that by `startup.sh` the `install_deps.sh` doesn't run at all ...... make really mad this. – Niewbie Jul 09 '21 at 09:17
  • @GordonDavisson, as Newbe yes I wrote `set -x` in Terminal ... am I correct or you mean to add it into the `startup.sh`. Sorry but I don't know this command, can you tell me more deep on it? And yes, `& exit` it's in purpose because this software have to check to manage the fans in background, so have to be running in background.... – Niewbie Jul 09 '21 at 09:19
  • 1
    `set -x` is one way to turn on the shell's execution tracing feature; see [this](https://stackoverflow.com/questions/52663302/shell-command-set-x-mean-in-script) and [this](https://stackoverflow.com/questions/951336/how-to-debug-a-bash-script). I recommend adding it to the beginning of both scripts, so you can see what both are doing as they execute. – Gordon Davisson Jul 09 '21 at 09:30
  • @Niewbie You are not redirecting the stderr to /dev/null when invoking `startup.sh`, are you? Bash cannot skip lines, if it cannot run `install_deps.sh` it must print an error message. About my second question, I understand that your app is already installed, but you said «if I try to run it, it runs perfectly». This means that all your dependencies are already installed too. Do you remove them along with the `conf.txt` file when you test the script? – David Jul 09 '21 at 10:55
  • Thanks @GordonDavisson, finally after other test (even with set -x) i discovered where is the problem and I don't know if it's weird or not. The problem it's in the forwarder, my icon desktop forwarded it as following: `[Desktop Entry] Name=test Exec=/usr/local/bin/test/at_startup.sh Type=Application StartupNotify=true Icon=/usr/local/bin/test/icon.png Path=/usr/local/bin/test/` – Niewbie Jul 09 '21 at 10:58
  • So, what happen if i will add the voice `Terminal=true` the forwarder will run the `at_startup.sh` and it will run (in meantime showing the Terminal) the 'install_deps.sh' but will not run the `app.py`, if I take out the `Terminal=true` it will run only the app and not the 'install_deps.sh' . By Terminal true, it's running because 'install_deps.sh' it needs to ask you the sudo password for this reason can run....So now I need to understand how can I let them to coexist .... – Niewbie Jul 09 '21 at 11:03
  • @David , because each everytime I uninstall the dependencies + the settings of the app to simulate a new fresh installation how it will run for the end user. But, as I wrote above , it seems that the `.desktop` icon if set the forwarding to run the `startup.sh` for some reason it exscape the `install_deps.sh`, if I add/ invoke the `Terminal=true` into the `.desktop` forwarder it run the `install_deps.sh` but not the app (so reverse story). The `install_deps.sh` it need the sudo password maybe could be this the reason why it doesn't run, because there is not any terminal pop-up from forwarder – Niewbie Jul 09 '21 at 13:08

1 Answers1

0

Thanks everyone for the support on that.

SOLUTION:

With a lot of experiments on my case, the problems was related when I was launching the .desktop icon that I created to launching the startup.sh, then launching the Python app.py and the install_deps.sh together; so, it seems when you are creating the Icon Desktop to launch a bash that should launch an app in Python + a bash in Terminal; from this icon "forwarder" it launchs exactly an App and jump the Terminal commands because are not an app (as in my case was the install_deps.sh). To solve this, instead of to add the line ./install_deps.sh into the bash file startup.sh I have simple created the similar line into the app.py to launch by Python command the Terminal that helps me to install the dependencies in install_deps.sh. And everything it works as should be.

Niewbie
  • 21
  • 4