Run python script on crontab

I am trying to execute a python script using linux crontab, but I have found many solutions and none of them work. For example: edit anacron on /etc/cron.d or use crontab -e.

I want to run this script every 10 minutes.

Which file do you need to change to configure it?

Thank you in advance

EDIT

I put this line at the end of the file, but nothing changes. Do I need to restart any services?

*/2 * * * * /usr/bin/python /home/souza/Documets/Listener/listener.py 

EDIT [2]

Guys, I followed the source code. There may be a problem, but when I execute it manually, it works:

http://pastebin.com/qsWHNzqT

+49
linux cron crontab
Jan 04 2018-12-12T00:
source share
3 answers

Just use crontab -e and follow the instructions here:

http://adminschoice.com/crontab-quick-reference

See point 3 for guidance on determining frequency.

According to your requirement, this should be effective:

 */10 * * * * /usr/bin/python script.py 
+83
Jan 04 '12 at 13:50
source share
— -

Put your script in the foo.py file, starting with

 #!/usr/bin/python 

then grant execute permission for this script with

 chmod a+x foo.py 

and use the full path to your foo.py file in crontab .

See execve (2) documentation that handles shebang

+44
Jan 04 2018-12-12T00:
source share

As you mentioned, nothing changes ,

First you must redirect both stdin and stderr from crontab execution, as shown below:

 */2 * * * * /usr/bin/python /home/souza/Documets/Listener/listener.py > /tmp/listener.log 2>&1 

then you can look at the /tmp/listener.log file to see if the script is executing as you expect.

Secondly, suppose you mean to change something by observing the files created by your program:

 f = file('counter', 'r+w') json_file = file('json_file_create_server.json','r+w') 

the crontab work above will not create these files in the /home/souza/Documets/Listener , since the cron job is not running in this directory, and you are using the relative path in the program. So, to create this file in the /home/souza/Documets/Listener , the following cron job will do the trick:

 */2 * * * * cd /home/souza/Documets/Listener && /usr/bin/python listener.py > /tmp/listener.log 2>&1 

Change the working directory and execute the script, then you can view the created files.

+7
Jan 12 '16 at 3:33
source share



All Articles