Score:0

How to run Python venv script with screen in crontab

jp flag

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
cn flag
Can you [edit](https://serverfault.com/posts/1081562/edit) the question to include all of `./home/myscript/start.sh` please
cn flag
Also this is probably worth a read https://unix.stackexchange.com/questions/109804/crontabs-reboot-only-works-for-root
jp flag
@hardillb I already posted the content of the script above
Score:1
cn flag

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

Score:0
us flag

Your shell script either should have #!/bin/bash or #!/bin/sh at the top. or if it's called via crontab it should be called either with /bin/sh or /bin/bash your-script. An alternative solution comparing to crontab is to take the advantages of the "/etc/rc.d/rc.local" file.

Since this file already runs at startup, we can append a line that invokes our script:
sh screen -mdS myscript /home/myscript/myscript_env/bin/python3

just need to ensure that the rc.local file itself is executable:

chmod +x /etc/rc.d/rc.local
Score:0
cn flag

Do you have any output in the mail generated by cron? Try checking the cron or mail logs for the user whose crontab contains this entry and that should give a hint about the issue.

At a glance, it seems to me that the reason is probably the extra . in the command. Try changing

@reboot ./home/myscript/start.sh

to

@reboot /home/myscript/start.sh

and see if that helps.

As far as I know the cronjob runs commands from the user's home directory (e.g., /root) instead of the file system root (i.e., /).

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.