How to run Python venv script with screen in crontab
I have a sh script with the following content:
screen -mdS myscript /home/myscript/myscript_env/bin/python3 /home/myscript/bot.py
This script executes a python script and opens it in a new screen.
It works fine when executing the sh file in the terminal, however when using a crontab to execute this sh script at reboot, it does not work:
@reboot ./home/myscript/start.sh
Solution 1:
Firstly scripts should really start with a shebang pointing to the interpreter that will run it, especially when passing it to something other than an existing shell to run it as you don't know what environment will be used. So the script in full should probably look like
#!/bin/sh
screen -mdS myscript /home/myscript/myscript_env/bin/python3
Secondly, as mentioned in the question I linked to, @reboot
is not always supported, either for all users or at all.
A much better option for getting something to start at boot time is a systemd service (assuming you are not using something old enough to still use init.d scripts). A reasonable discussion of how to setup a service is here