How to make crontab run a Python script


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/
* * * * * /root/pasta/ > /dev/tty1
* * * * * /usr/bin/python /root/pasta/
* * * * * /usr/bin/python /root/pasta/ > /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:

import os;

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


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/

Thanks guys!

Scroll to Top