How to make crontab run a Python script

Asked

Viewed 2,703 times

3

I’m trying to schedule a script I wrote in Python to run on crontab, But I can’t do it. The script is simple thing, it makes a telnet connection through the terminal and sends some commands. I managed to schedule one shellscript, but I am unable to schedule the Python script.

I tried to schedule the following 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 it at the beginning of the script #!/usr/bin/python and also was not.

Script works normally on the terminal, just doesn’t want to work on crontab scheduling.

Python is the default for Ubuntu, version 2.7.6.

Follow 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""");
  • 1

    Check the file permissions.

  • Try to make the file executable: chmod +x /root/pasta/script.py

  • The script is with permission 777, I put the folder also with 777 and continues in the same problem, the terminal runs smoothly in ". /script.py", but crontab does not.

1 answer

1


I was able to make it work... Actually I don’t know what the problem was, but after rebooting the entire system it started working right. If I’m useful to anyone, my crontab looks like this:

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

Valeuu personal!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.