Chris Given
Chris Given

Reputation: 11

Python Cron Job Dies Prematurely

I'm trying to run a python script at regular intervals via a cron job. The python script is a chat bot for Twitch which checks once per minute to see if a streamer goes live or turns off their stream, and launches a bot or disconnects it as needed.

The script works in bash and when I run it with sh. It starts correctly every minute, but each bot dies somewhere between 2 and 30 seconds (a much shorter time than expected). The bots die at different times, so at first I thought they may be encountering errors, so I redirected the output to a text file but nothing unexpected appeared.

My cron job:

* * * * * cd [path to script] && python3 Chatbot.py &

I can post more information about the script if requested, but since it works as intended in the shell and in bash I don't think the error lies there. There is an empty line at the end of my crontab file and the cron service is running.

EDIT: Solved it! I went to the end of my log and searched through the Twitch vod for the message following the last one successfully logged, turns out it was an emoji. My python script handles this but I guess cron changes the encoding? I added the following to my crontab file:

LANG=en_US.UTF-8

This seems to have fixed my issue -- it's been running for a few minutes with no problems.

Upvotes: 0

Views: 366

Answers (1)

Chris Given
Chris Given

Reputation: 11

Cron apparently changes encoding, so to keep consistent with the encoding used in my python scripts I added the following to my crontab:

LANG=en_US.UTF-8

Upvotes: 1

Related Questions