Forums

Scheduled Tasks not working

I've tried every command I can find in the forum and in the documentation but haven't had any luck.

I'm trying to run a .py file located in my root folder.

I've tried: python3 bot.py python3 /home/my_username/bot.py cd /home/my_username/; python3 bot.py (I'm using my actual user folder name in those placeholders above)

I've tried adding #!/usr/bin/python3.9 to the top of the .py file.

I can run the file perfectly fine from my bash console but it won't work in scheduled tasks.

Log is giving this every time: /bin/bash: python: command not found

2021-11-22 03:04:11 -- Completed task, took 5.01 seconds, return code was 127.

Does anyone have any suggestions?

I don't see how any of the commands you used could produce that error. Are you sure those are what you used. It looks like you've removed your task, so we have no way to check what was actually causing the error.

Oops sorry about that.I just recreated the task and tried once more this morning, still the same error.

I adjusted the script for testing purposes, so it should print a success message to bot_log.txt.

I appreciate your help.

Could you run a task with the command

/usr/bin/which python; /usr/bin/which python3; /usr/bin/which python3.9

and let us know what the result was?

Sure thing, looks to be the same results:

/bin/bash: python: command not found

2021-11-22 15:03:26 -- Completed task, took 19.07 seconds, return code was 127.

That's weird. The only thing I can think of now is that one or more of your initialisation files has been changed in a way that breaks the path in scheduled tasks. Have you edited .bashrc, .profile or other .something files in your home directory? You can try deleting .bashrc and .profile from your home directory and then start a new console to re-create them.

You are spot on Glenn. I was changing those files trying to get environment variables to work (later discovered the documentation for them).

Scheduled Tasks are working perfectly now, thank you for your help!

Great! Glad we could get to the bottom of that.

I'm having this issue since my upgrade to the latest image. Deleting my .bashrc and .profile did not change the behavior.

The tasks are Django Management commands. Before the image upgrade, they worked fine. The only change I made after the image upgrade was to update them to use the Python binary in my newly created venv — though of course I've tried other changes since, in attempts to get it working.

The exact commands I'm using for the tasks work as expected in an SSH session.

Is there any way to see more verbose log output of the failed runs?

All of the output from the code that you run in a scheduled task will appear in the logs for the scheduled task.

@glenn thanks, I was just finding "/bin/bash: python: command not found" a prety opaque error message.

I've added some troubleshooting tasks e.g. which python as suggested above, as well as printing $PATH and $SHELL for sanity. I'll report here if those reveal any culprits.

I could use some more detail on specifically what kinds of changes in .bashrc and/or .profile might cause this behavior? Is .bash_profile a suspect also? My interactive SSH sessions are working normally.

I do have conditional logic in my .bash_profile that only runs the full file for interactive sessions. Problematic?

[[ $- == *i* ]] && echo "Appears to be an interactive session; running .bash_profile" || return

Thanks.

Welp, a task that consists of a singe echo fails with that same "python: command not found" error.

Still would like to hear what to look out for in the dotfiles as possible culprits. Again, my interactive sessions are working fine; this problem with tasks emerged after my latest image upgrade and venv rebuild.

It's the PATH environment variable that determines where to find the python executable and, if there is no python executable on the PATH, then you will get that error.

Setting PATH correctly in .bash_profile did the trick. Thanks.

Glad we could help!

Hello!

Same problem - I have a program on person code that supports work of my telegrambot. Unfortunately, the program in console has been stopped after some time. I want to relaunch it using task. But it doesn't work so I have to relaunch it manually via bash console. I use the following code in bash and put the same in task "python3.10 /home/user_name/bot.py"

Consoles will always stop after some time. They're meant to do this. If you'd like to run something 24/7 the best way to do that is to use an always on task - Always-On Tasks

But it doesn't work at all. I got your point about 24/7. But it should work at least in first 1 min after launching

Yes, what happens when you run your bot in a bash console?