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
  • 34,963
mrin9san
  • 123

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
  • 34,963