Starting venv on reboot

February 13, 2024, 14:45

winikolo

I am using my raspberry to run a discord bot and usually i ssh into it and run source file/bin/activate python file/main.py i tried adding those commands to crontab hoping it'd work but it does not @reboot source file/bin/activate & @reboot python file/main.py i am fairly new to this and i just followed youtube so far to set stuff up. anyone know how to get my main.py file running on reboot?

oops.se

Did you sudo crontab -e editing the crontab?

winikolo

yup

oops.se

Then you run as root

oops.se

Don't use sudo

k9t33n

this still opens a venv but it will not open it in the same terminal as the one you log into with ssh

k9t33n

oh nvm just read it properly

k9t33n

try changing what you add to
@reboot source file/bin/activate && python file/main.py

oops.se

I don't think that will work, as crontab doesn't have any environment variables. My solution is to create a bash script.

k9t33n

oh. would you be able to run this bash script in crontab then?

oops.se

And bash can't use relative paths, you need to be in the correct directory or use absolute paths

oops.se

Yes

k9t33n

go ahead with creating a bash script then

winikolo

im trying out your method of creating a bash file with source and the python command in it

winikolo

but that causes python errors for some reason

winikolo

AYYYYYY WE GOT IT

winikolo

apparently it couldnt read upto the cogs folder for some reason

winikolo

went only until home/winikolo/

winikolo

bruh even with that fixed nothing is happening when i run the bash off crontab wtf, but manually running it works?

inwamos

Cd into your bot’s directory

inwamos

Then, run python after cd into your main.py

inwamos

Or simply specify the absolute path to all your files

inwamos

Or learn how to use docker instead. It gives you a variety of different options such as autostart a container on startup and so on

inwamos

+ docker is a great hard skill for your it future 😛

winikolo

I fixed the python errors by absolute path, yes, but what i want is for the bash script that runs the virtual env + main.py file to start on reboot, manually when i run it as in './start_script.sh' it works but when using cron tab to run it on reboot, it doesnt work

winikolo

Idk if theres a way to see what the errors crontab sees but idk tbh

inwamos

Just make it via docker container

winikolo

Sure ill see, thank you

inwamos

Welcome