0

We have a app that is run out of a specific directory, it uses flask (never worked with it before) and it is called from /home/automation_admin/rest-auto/production/TestLauncherApp

Once in that directory, we run flask run --host=0.0.0.0 --port=6000 &

I can get things running that way without any problem. If I try it from my home directory and use:

/home/automation_admin/rest-auto/production/TestLauncherApp/ "flask run --host=0.0.0.0 --port=6000 &" 

I get:

No such file or directory

I'm trying to set this up so it runs on startup, and have a .sh file that cron should be running on bootup, but when I try the .sh file manually, which has:

#!/bin/sh
#Starts the automation website on reboot
 /home/automation_admin/rest-auto/production/TestLauncherApp/ "flask run --host=0.0.0.0 --port=5000 &"

I get:

./auto.sh: 3: /home/automation_admin/rest-auto/production/TestLauncherApp/flask run --host=0.0.0.0 --port=5000 &: not found

How can I make this work??

4
  • I tried to improve the formatting, but I'm not convinced that I got it right. Are you missing a cd command?
    – harrymc
    Commented Apr 6, 2021 at 17:51
  • The problem is with quoting, but the question in its current form is incoherent. The last error is as if there was no space before the first ". Also please clarify: do you want to (1) change the working directory and then run flask … (as opposed to ./flask …)? or (2) change the working directory and then run flask existing in the directory? or (3) run /home/automation_admin/rest-auto/production/TestLauncherApp/flask … without changing the working directory? The beginning of the question suggests (1); your try suggests (3); (2) is in between (in some sense). Commented Apr 6, 2021 at 18:01
  • I would like to either 1) run the command from the home directory, or switch to the /TestLauncherApp directory and run the flask command from there Commented Apr 6, 2021 at 18:17
  • Your reply did not clear my doubts, therefore the answer I posted tries to be helpful in any of these cases. Commented Apr 6, 2021 at 20:41

1 Answer 1

0

The error you got from the script:

./auto.sh: 3: /home/automation_admin/rest-auto/production/TestLauncherApp/flask run --host=0.0.0.0 --port=5000 &: not found

suggests the actual path the shell tried to run was

/home/automation_admin/rest-auto/production/TestLauncherApp/flask run --host=0.0.0.0 --port=5000 &

where each space belongs to the path. This is because in the script you quoted these spaces:

/home/automation_admin/rest-auto/production/TestLauncherApp/"flask run --host=0.0.0.0 --port=5000 &"

(In the question body there is one unquoted space before the first " but I assume it's a typo because the form with the space would generate a different error.)

Due to the quoting, the shell interpreting the script treats the entire line (after removing the quotes) as one word, the executable you want to run. The error you got from the script says such executable was not found.

On the other hand spaces in flask run --host=0.0.0.0 --port=6000 & are unquoted, so when you run it, the shell recognizes & as a command terminator, flask as the command (executable to run), run, --host=0.0.0.0 and --port=6000 as separate command line arguments to the command.

The line from the script with no quotes looks better, although this is not necessarily the line you want to run:

/home/automation_admin/rest-auto/production/TestLauncherApp/flask run --host=0.0.0.0 --port=5000 &

Here the executable that should run is /home/automation_admin/rest-auto/production/TestLauncherApp/flask. It is not necessarily what you want because when you run flask … after changing the directory to TestLauncherApp, you run flask that is somewhere in your PATH.

If you invoked ./flask … then the shell would (try to) run flask existing in the current working directory. flask resolved by PATH may or may not be ./flask. If your PATH contains the current working directory (explicitly or as .) and there is no flask in directories appearing earlier in the PATH, then flask will be ./flask.

In other words the question tells me you run flask but it might not be /home/automation_admin/rest-auto/production/TestLauncherApp/flask. Therefore I cannot be sure the line that tries to run the latter is what you need.

If flask run --host=0.0.0.0 --port=6000 works when invoked from one path, it should work when invoked from another path, unless:

  • . is in the PATH and makes flask mean ./flask as described above. Then ./flask may exist in one directory but not in the other.
  • Or maybe run is a relative path meaning ./run; the same applies to other arguments. If your flask is this one then run is its internal command. --host=0.0.0.0 and --port=6000 definitely look like options that don't depend on the current working directory. So in your case this potential issue is most likely not the issue.
  • Or maybe flask implicitly uses the current working directory (e.g. it tries to read or create files there) and the other directory is not right for this (e.g. the necessary files are not there, or new files cannot be created due to insufficient permissions).

I assume you really need to change the working directory to /home/automation_admin/rest-auto/production/TestLauncherApp.

Your script should do exactly what you do when you manage to run flask in an interactive shell:

#!/bin/sh
cd /home/automation_admin/rest-auto/production/TestLauncherApp
flask run --host=0.0.0.0 --port=6000 &

Or better (in case cd fails):

#!/bin/sh
cd /home/automation_admin/rest-auto/production/TestLauncherApp \
&& flask run --host=0.0.0.0 --port=6000 &

It will still use PATH to locate flask. If you run the script as ./auto.sh in an interactive shell then it will inherit PATH from the interactive shell; so it should work. But in general PATH will be different if the script is run from cron or with sudo, or at startup from rc.local, or with systemd, or in whatever way different than from your interactive shell. In such case the most straightforward solution is to use the absolute path to flask executable.

Or maybe you deliberately wanted to run /home/automation_admin/rest-auto/production/TestLauncherApp/flask as opposed to flask from your PATH (e.g. because these are different versions and you strictly want the former). In such case use the full path even if you're going to use the script from your interactive shell. Changing the directory may not be necessary.

And to be clear: the current working directory and the directory where the shell finds flask are two different concepts. With cd you set the current working directory. The other one depends on PATH, unless instead flask you use a path containing / (e.g. ./flask or another/relative/path/to/flask or /absolute/path/to/flask). This answer is long because I was not sure if you wanted to cd and run flask via PATH (the beginning of the question suggests this), or you wanted to use the exact absolute path /home/automation_admin/rest-auto/production/TestLauncherApp/flask (the error you got from your imperfect script suggests this).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .