How to make crontab run a Python script

Question:

I'm trying to schedule a script I wrote in Python to run in crontab , but I can't do it. The script is simple, it makes a telnet connection through the terminal and sends some commands. I managed to schedule a shellscript , but I am not able to schedule the Python script.

I tried to schedule in the following ways in crontab:

* * * * * /root/pasta/script.py
* * * * * /root/pasta/script.py > /dev/tty1
* * * * * /usr/bin/python /root/pasta/script.py
* * * * * /usr/bin/python /root/pasta/script.py > /dev/tty1

I put #!/usr/bin/python at the beginning of the script and it wasn't either.

The script works normally in the terminal, it just doesn't want to work in the crontab schedule.

Python is the default for Ubuntu, version 2.7.6.

Here's the Python code:

#!/usr/bin/python
import os;

# Conecta no servidor telnet e envia uma mensagem.
# O servidor responde com um 'ok'.
os.system("""
(echo 'teste';
sleep 1;
echo 'quit';
exit) | telnet localhost 23""");

Answer:

I managed to get it to work… Actually I don't know what the problem was, but after restarting the entire system it started to work fine. If it's useful for someone, my crontab looks like this:

* * * * * /usr/bin/python /root/pasta/script.py

Thanks guys!

Scroll to Top