0

I am a newbie in Ubuntu 20.04. I have a file structure for an application as -

Home-->Folder1-->execute.sh,main.py, utils.py, ui.py etc..

What i intend to do is run the execute.sh file on system startup. The lines in my execute.sh file are :

export PATH="/home/Folder1"
python3 -m venv venv
. ./venv/bin/activate
pip install python-dotenv
python /home/Folder1/main.py

I tried a cron job cron -e to put a line sh \home\Folder1\execute.sh, and start the process but could not make it work. On further check, I have seen that when i access root and try to run sh \home\Folder1\execute.sh throws error main.py not found. I have to cd into \home\Folder1 and then execute sh execute.sh to make it run. What will be the best way to achieve this in system reboot each time automatically?

Raffa
  • 32,237
mrin9san
  • 123
  • 5
  • One way would be to run the job with cron but using you user rather than root. The root user runs in a different environment so from that perspective things are not where you expect them to be. Alternatively use the complete path to each program wherever referenced – PonJar Jun 23 '22 at 07:24
  • @Raffa, shall look into it if I can use cron as you said. can you elaborate on the last part? – mrin9san Jun 23 '22 at 09:44
  • @PonJar, yea one solution will be to refer full path to all modules – mrin9san Jun 23 '22 at 09:46

1 Answers1

2

This is wrong on many levels :) ... but, it's OK, we'll make it right together.

First level

cron is a daemon/service to execute scheduled commands ... You don't add your cron jobs to it ... you add your cron jobs to your user's crontab like so:

crontab -e

or to root's crontab like so:

sudo crontab -e

It will then be executed by cron automatically ... That's it.

Second level

sh \home\Folder1\execute.sh is a Windows style path which is not valid on Ubuntu and will translate to a single filename(not a path) like so:

sh homeFolder1execute.sh

it should be with the forward slash / and not the backward slash e.g. :

sh /home/Folder1/execute.sh

Third level

Your export PATH="/home/Folder1" is not the correct usage that might yield the result you expect of adding /home/Folder1 to PATH ... instead it will replace everything already in the environment variable PATH with just /home/Folder1 and that is another problem ... to just add /home/Folder1, you should do it like so:

export PATH="$PATH:/home/Folder1"

Bonus level

As a general advice, when using crontab in general and the root's crontab in particular, specify the full path to the executable/s on your system(you can find it with e.g. which sh) like so:

/usr/bin/sh /home/Folder1/execute.sh

also use the full path to executables that are used in your script e.g.:

/usr/bin/python3 -m venv venv

and:

/usr/bin/python3 /home/Folder1/main.py

and:

/usr/bin/pip install python-dotenv

Although, for the last two, it will work with just python and pip in a python virtual environment like you do in your script.

Otherwise, the running shell might fail in finding the path to your command/s as for example the root's environment variable PATH will return different paths from those returned by your user's PATH environment variable.

You can, however, save specifying the full path to your script file along with other scripts/files sourced/referenced in it by, first, cding to the directory containing it in the crontab line like so:

cd /home/Folder1 && /usr/bin/sh execute.sh

That wont save you from not providing the full path to the executables on your system i.e. like /usr/bin/sh, you still need to provide those ... why not the full path to cd? you might ask ... well, the answer is cd is a builtin shell command and will work just fine without needing a path.

Raffa
  • 32,237
  • A cd statement && executing full path sh as you pointed out, worked. Thank you. Now the process gets initiated in cron. However, I have one more concern, the application still does not start due to netifaces module could not connect to ethernet and get a ip. Error is in following line -- ip = ni.ifaddresses(os.environ.get("NETWORK_MODE", "eth0"))[ni.AF_INET][0]['addr']. Error is KeyError: 2. It works fine if I run it from terminal. I tried @reboot sleep 120 && cd .....&& sh. Hoping that it will start after two minutes atleast but could not. – mrin9san Jun 24 '22 at 05:54
  • @mrin9san You probably need to install that module in the venv you run your python script in … the virtual env is self contained and it will not be enough to have that module only installed in the system wide python but in the venv as well. – Raffa Jun 24 '22 at 08:12
  • @mrin9san Read this as well … and if you can’t fix it, then post a new question with your code and the error message and explain how you run your code and the expected result so we can help you. – Raffa Jun 24 '22 at 08:27