Execute python Script on Crontab










72















I'm trying to execute a python script using the linux crontab, but I found a lot of solutions and none of them works. For example: edit the anacron at /etc/cron.d or use crontab -e.



I want to run this script every 10 minutes.



What file must I edit to configure this?



Thanks in advance



EDIT



I put this line at the end of the file, but it doesn't changes anything. Do I have to restart any service?



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


EDIT [2]



Guys, I have followed the source code. There maybe a problem here, but when I execute it manually, it works:



http://pastebin.com/qsWHNzqT










share|improve this question
























  • When you say "it doesn't change anything". Does it show an error, does it not run? What is the behaviour?

    – Raul Marengo
    Jan 4 '12 at 15:47











  • is "Documets" instead of "Documents" intentional?

    – Raul Marengo
    Jan 4 '12 at 15:48











  • Simply doesn't occurs anything. :(

    – guisantogui
    Jan 4 '12 at 15:53











  • This sort of goes beyond the scope of the question but, what do you expect your "listener.py" script to do? Does it do anything that could indicate that it has run? Do ps -ef | grep 'crond' in your command line to check if cron is running.

    – Raul Marengo
    Jan 4 '12 at 15:57











  • No, this script send several cURL's commands to another computer. When I execute the "ps -f | grep 'crond'", it returns this: "souza 4736 3947 0 14:01 pts/1 00:00:00 grep --color=auto crond"

    – guisantogui
    Jan 4 '12 at 16:00
















72















I'm trying to execute a python script using the linux crontab, but I found a lot of solutions and none of them works. For example: edit the anacron at /etc/cron.d or use crontab -e.



I want to run this script every 10 minutes.



What file must I edit to configure this?



Thanks in advance



EDIT



I put this line at the end of the file, but it doesn't changes anything. Do I have to restart any service?



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


EDIT [2]



Guys, I have followed the source code. There maybe a problem here, but when I execute it manually, it works:



http://pastebin.com/qsWHNzqT










share|improve this question
























  • When you say "it doesn't change anything". Does it show an error, does it not run? What is the behaviour?

    – Raul Marengo
    Jan 4 '12 at 15:47











  • is "Documets" instead of "Documents" intentional?

    – Raul Marengo
    Jan 4 '12 at 15:48











  • Simply doesn't occurs anything. :(

    – guisantogui
    Jan 4 '12 at 15:53











  • This sort of goes beyond the scope of the question but, what do you expect your "listener.py" script to do? Does it do anything that could indicate that it has run? Do ps -ef | grep 'crond' in your command line to check if cron is running.

    – Raul Marengo
    Jan 4 '12 at 15:57











  • No, this script send several cURL's commands to another computer. When I execute the "ps -f | grep 'crond'", it returns this: "souza 4736 3947 0 14:01 pts/1 00:00:00 grep --color=auto crond"

    – guisantogui
    Jan 4 '12 at 16:00














72












72








72


33






I'm trying to execute a python script using the linux crontab, but I found a lot of solutions and none of them works. For example: edit the anacron at /etc/cron.d or use crontab -e.



I want to run this script every 10 minutes.



What file must I edit to configure this?



Thanks in advance



EDIT



I put this line at the end of the file, but it doesn't changes anything. Do I have to restart any service?



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


EDIT [2]



Guys, I have followed the source code. There maybe a problem here, but when I execute it manually, it works:



http://pastebin.com/qsWHNzqT










share|improve this question
















I'm trying to execute a python script using the linux crontab, but I found a lot of solutions and none of them works. For example: edit the anacron at /etc/cron.d or use crontab -e.



I want to run this script every 10 minutes.



What file must I edit to configure this?



Thanks in advance



EDIT



I put this line at the end of the file, but it doesn't changes anything. Do I have to restart any service?



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


EDIT [2]



Guys, I have followed the source code. There maybe a problem here, but when I execute it manually, it works:



http://pastebin.com/qsWHNzqT







linux cron crontab






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 30 '16 at 23:47









FractalSpace

3,40822840




3,40822840










asked Jan 4 '12 at 13:46









guisantoguiguisantogui

1,33333371




1,33333371












  • When you say "it doesn't change anything". Does it show an error, does it not run? What is the behaviour?

    – Raul Marengo
    Jan 4 '12 at 15:47











  • is "Documets" instead of "Documents" intentional?

    – Raul Marengo
    Jan 4 '12 at 15:48











  • Simply doesn't occurs anything. :(

    – guisantogui
    Jan 4 '12 at 15:53











  • This sort of goes beyond the scope of the question but, what do you expect your "listener.py" script to do? Does it do anything that could indicate that it has run? Do ps -ef | grep 'crond' in your command line to check if cron is running.

    – Raul Marengo
    Jan 4 '12 at 15:57











  • No, this script send several cURL's commands to another computer. When I execute the "ps -f | grep 'crond'", it returns this: "souza 4736 3947 0 14:01 pts/1 00:00:00 grep --color=auto crond"

    – guisantogui
    Jan 4 '12 at 16:00


















  • When you say "it doesn't change anything". Does it show an error, does it not run? What is the behaviour?

    – Raul Marengo
    Jan 4 '12 at 15:47











  • is "Documets" instead of "Documents" intentional?

    – Raul Marengo
    Jan 4 '12 at 15:48











  • Simply doesn't occurs anything. :(

    – guisantogui
    Jan 4 '12 at 15:53











  • This sort of goes beyond the scope of the question but, what do you expect your "listener.py" script to do? Does it do anything that could indicate that it has run? Do ps -ef | grep 'crond' in your command line to check if cron is running.

    – Raul Marengo
    Jan 4 '12 at 15:57











  • No, this script send several cURL's commands to another computer. When I execute the "ps -f | grep 'crond'", it returns this: "souza 4736 3947 0 14:01 pts/1 00:00:00 grep --color=auto crond"

    – guisantogui
    Jan 4 '12 at 16:00

















When you say "it doesn't change anything". Does it show an error, does it not run? What is the behaviour?

– Raul Marengo
Jan 4 '12 at 15:47





When you say "it doesn't change anything". Does it show an error, does it not run? What is the behaviour?

– Raul Marengo
Jan 4 '12 at 15:47













is "Documets" instead of "Documents" intentional?

– Raul Marengo
Jan 4 '12 at 15:48





is "Documets" instead of "Documents" intentional?

– Raul Marengo
Jan 4 '12 at 15:48













Simply doesn't occurs anything. :(

– guisantogui
Jan 4 '12 at 15:53





Simply doesn't occurs anything. :(

– guisantogui
Jan 4 '12 at 15:53













This sort of goes beyond the scope of the question but, what do you expect your "listener.py" script to do? Does it do anything that could indicate that it has run? Do ps -ef | grep 'crond' in your command line to check if cron is running.

– Raul Marengo
Jan 4 '12 at 15:57





This sort of goes beyond the scope of the question but, what do you expect your "listener.py" script to do? Does it do anything that could indicate that it has run? Do ps -ef | grep 'crond' in your command line to check if cron is running.

– Raul Marengo
Jan 4 '12 at 15:57













No, this script send several cURL's commands to another computer. When I execute the "ps -f | grep 'crond'", it returns this: "souza 4736 3947 0 14:01 pts/1 00:00:00 grep --color=auto crond"

– guisantogui
Jan 4 '12 at 16:00






No, this script send several cURL's commands to another computer. When I execute the "ps -f | grep 'crond'", it returns this: "souza 4736 3947 0 14:01 pts/1 00:00:00 grep --color=auto crond"

– guisantogui
Jan 4 '12 at 16:00













3 Answers
3






active

oldest

votes


















108














Just use crontab -e and follow the tutorial here:



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



Look at point 3 for a guide on how to specify the frequency.



Based on your requirement, it should effectively be:



*/10 * * * * /usr/bin/python script.py





share|improve this answer




















  • 1





    I follow this tutorial, but when i save the file appears a message: "/tmp/crontab.JTQ0My/crontab":22: bad minute errors in crontab file, can't install. Do you want to retry the same edit? (y/n)" if I type "y", I've returned to file edit. And if I type "n" the file is not saved. I add this line at last line of the file: "/1 * * * * /usr/bin/python script.py"

    – guisantogui
    Jan 4 '12 at 13:56












  • @guisantogui there is a point in the tutorial that explains that using "/1" might not be supported by all operating systems. What operating system are you running this on?

    – Raul Marengo
    Jan 4 '12 at 14:02






  • 3





    @guisantogui just noticed you are missing a "*" before the "/"

    – Raul Marengo
    Jan 4 '12 at 14:03











  • another way is to add an env declaration in your script.py. See my comments to the accepted solution at: stackoverflow.com/questions/25633737/python-crontab-and-paths

    – Quetzalcoatl
    Sep 3 '14 at 14:56












  • What if you want to execute the script.py only in the given directory?

    – Shubham A.
    Jan 3 '18 at 10:09


















53














Put your script in a file foo.py starting with



#!/usr/bin/python


then give execute permission to that script using



chmod a+x foo.py


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



See documentation of execve(2) which is handling the shebang






share|improve this answer

























  • is #!/bin/sh good for shell scripts?

    – Tomer
    May 14 '17 at 12:19






  • 1





    @Tomer If they are POSIX sh shell scripts then yes. If they use nonstandard features specific to ksh, zsh, or bash then they need to be run using that specific shell.

    – tripleee
    Jul 22 '18 at 13:58


















13














As you have mentioned doesn't changes anything,



First, you should redirect both stdin and stderr from crontab execution like below:



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


then you can view the file /tmp/listener.log to see if the script executed as you expect.



Second, guess what you mean change anything is by watching the files created by your program:



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


the crontab job above won't create these file in directory /home/souza/Documets/Listener, as the cron job is not executed in this directory, and you use relative path in the program. So to create these file in directory /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 to the working directory and execute the script from there, then you can view the files created in place.






share|improve this answer























  • what is 2>&1 mean?

    – Mohideen ibn Mohammed
    Feb 21 '18 at 12:30











  • @MohideenibnMohammed redirect error messages (stderr) to the visible command line (stdout)

    – Juha Untinen
    Apr 3 '18 at 15:05










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8727935%2fexecute-python-script-on-crontab%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









108














Just use crontab -e and follow the tutorial here:



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



Look at point 3 for a guide on how to specify the frequency.



Based on your requirement, it should effectively be:



*/10 * * * * /usr/bin/python script.py





share|improve this answer




















  • 1





    I follow this tutorial, but when i save the file appears a message: "/tmp/crontab.JTQ0My/crontab":22: bad minute errors in crontab file, can't install. Do you want to retry the same edit? (y/n)" if I type "y", I've returned to file edit. And if I type "n" the file is not saved. I add this line at last line of the file: "/1 * * * * /usr/bin/python script.py"

    – guisantogui
    Jan 4 '12 at 13:56












  • @guisantogui there is a point in the tutorial that explains that using "/1" might not be supported by all operating systems. What operating system are you running this on?

    – Raul Marengo
    Jan 4 '12 at 14:02






  • 3





    @guisantogui just noticed you are missing a "*" before the "/"

    – Raul Marengo
    Jan 4 '12 at 14:03











  • another way is to add an env declaration in your script.py. See my comments to the accepted solution at: stackoverflow.com/questions/25633737/python-crontab-and-paths

    – Quetzalcoatl
    Sep 3 '14 at 14:56












  • What if you want to execute the script.py only in the given directory?

    – Shubham A.
    Jan 3 '18 at 10:09















108














Just use crontab -e and follow the tutorial here:



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



Look at point 3 for a guide on how to specify the frequency.



Based on your requirement, it should effectively be:



*/10 * * * * /usr/bin/python script.py





share|improve this answer




















  • 1





    I follow this tutorial, but when i save the file appears a message: "/tmp/crontab.JTQ0My/crontab":22: bad minute errors in crontab file, can't install. Do you want to retry the same edit? (y/n)" if I type "y", I've returned to file edit. And if I type "n" the file is not saved. I add this line at last line of the file: "/1 * * * * /usr/bin/python script.py"

    – guisantogui
    Jan 4 '12 at 13:56












  • @guisantogui there is a point in the tutorial that explains that using "/1" might not be supported by all operating systems. What operating system are you running this on?

    – Raul Marengo
    Jan 4 '12 at 14:02






  • 3





    @guisantogui just noticed you are missing a "*" before the "/"

    – Raul Marengo
    Jan 4 '12 at 14:03











  • another way is to add an env declaration in your script.py. See my comments to the accepted solution at: stackoverflow.com/questions/25633737/python-crontab-and-paths

    – Quetzalcoatl
    Sep 3 '14 at 14:56












  • What if you want to execute the script.py only in the given directory?

    – Shubham A.
    Jan 3 '18 at 10:09













108












108








108







Just use crontab -e and follow the tutorial here:



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



Look at point 3 for a guide on how to specify the frequency.



Based on your requirement, it should effectively be:



*/10 * * * * /usr/bin/python script.py





share|improve this answer















Just use crontab -e and follow the tutorial here:



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



Look at point 3 for a guide on how to specify the frequency.



Based on your requirement, it should effectively be:



*/10 * * * * /usr/bin/python script.py






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 4 '12 at 14:08

























answered Jan 4 '12 at 13:50









Raul MarengoRaul Marengo

1,7371119




1,7371119







  • 1





    I follow this tutorial, but when i save the file appears a message: "/tmp/crontab.JTQ0My/crontab":22: bad minute errors in crontab file, can't install. Do you want to retry the same edit? (y/n)" if I type "y", I've returned to file edit. And if I type "n" the file is not saved. I add this line at last line of the file: "/1 * * * * /usr/bin/python script.py"

    – guisantogui
    Jan 4 '12 at 13:56












  • @guisantogui there is a point in the tutorial that explains that using "/1" might not be supported by all operating systems. What operating system are you running this on?

    – Raul Marengo
    Jan 4 '12 at 14:02






  • 3





    @guisantogui just noticed you are missing a "*" before the "/"

    – Raul Marengo
    Jan 4 '12 at 14:03











  • another way is to add an env declaration in your script.py. See my comments to the accepted solution at: stackoverflow.com/questions/25633737/python-crontab-and-paths

    – Quetzalcoatl
    Sep 3 '14 at 14:56












  • What if you want to execute the script.py only in the given directory?

    – Shubham A.
    Jan 3 '18 at 10:09












  • 1





    I follow this tutorial, but when i save the file appears a message: "/tmp/crontab.JTQ0My/crontab":22: bad minute errors in crontab file, can't install. Do you want to retry the same edit? (y/n)" if I type "y", I've returned to file edit. And if I type "n" the file is not saved. I add this line at last line of the file: "/1 * * * * /usr/bin/python script.py"

    – guisantogui
    Jan 4 '12 at 13:56












  • @guisantogui there is a point in the tutorial that explains that using "/1" might not be supported by all operating systems. What operating system are you running this on?

    – Raul Marengo
    Jan 4 '12 at 14:02






  • 3





    @guisantogui just noticed you are missing a "*" before the "/"

    – Raul Marengo
    Jan 4 '12 at 14:03











  • another way is to add an env declaration in your script.py. See my comments to the accepted solution at: stackoverflow.com/questions/25633737/python-crontab-and-paths

    – Quetzalcoatl
    Sep 3 '14 at 14:56












  • What if you want to execute the script.py only in the given directory?

    – Shubham A.
    Jan 3 '18 at 10:09







1




1





I follow this tutorial, but when i save the file appears a message: "/tmp/crontab.JTQ0My/crontab":22: bad minute errors in crontab file, can't install. Do you want to retry the same edit? (y/n)" if I type "y", I've returned to file edit. And if I type "n" the file is not saved. I add this line at last line of the file: "/1 * * * * /usr/bin/python script.py"

– guisantogui
Jan 4 '12 at 13:56






I follow this tutorial, but when i save the file appears a message: "/tmp/crontab.JTQ0My/crontab":22: bad minute errors in crontab file, can't install. Do you want to retry the same edit? (y/n)" if I type "y", I've returned to file edit. And if I type "n" the file is not saved. I add this line at last line of the file: "/1 * * * * /usr/bin/python script.py"

– guisantogui
Jan 4 '12 at 13:56














@guisantogui there is a point in the tutorial that explains that using "/1" might not be supported by all operating systems. What operating system are you running this on?

– Raul Marengo
Jan 4 '12 at 14:02





@guisantogui there is a point in the tutorial that explains that using "/1" might not be supported by all operating systems. What operating system are you running this on?

– Raul Marengo
Jan 4 '12 at 14:02




3




3





@guisantogui just noticed you are missing a "*" before the "/"

– Raul Marengo
Jan 4 '12 at 14:03





@guisantogui just noticed you are missing a "*" before the "/"

– Raul Marengo
Jan 4 '12 at 14:03













another way is to add an env declaration in your script.py. See my comments to the accepted solution at: stackoverflow.com/questions/25633737/python-crontab-and-paths

– Quetzalcoatl
Sep 3 '14 at 14:56






another way is to add an env declaration in your script.py. See my comments to the accepted solution at: stackoverflow.com/questions/25633737/python-crontab-and-paths

– Quetzalcoatl
Sep 3 '14 at 14:56














What if you want to execute the script.py only in the given directory?

– Shubham A.
Jan 3 '18 at 10:09





What if you want to execute the script.py only in the given directory?

– Shubham A.
Jan 3 '18 at 10:09













53














Put your script in a file foo.py starting with



#!/usr/bin/python


then give execute permission to that script using



chmod a+x foo.py


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



See documentation of execve(2) which is handling the shebang






share|improve this answer

























  • is #!/bin/sh good for shell scripts?

    – Tomer
    May 14 '17 at 12:19






  • 1





    @Tomer If they are POSIX sh shell scripts then yes. If they use nonstandard features specific to ksh, zsh, or bash then they need to be run using that specific shell.

    – tripleee
    Jul 22 '18 at 13:58















53














Put your script in a file foo.py starting with



#!/usr/bin/python


then give execute permission to that script using



chmod a+x foo.py


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



See documentation of execve(2) which is handling the shebang






share|improve this answer

























  • is #!/bin/sh good for shell scripts?

    – Tomer
    May 14 '17 at 12:19






  • 1





    @Tomer If they are POSIX sh shell scripts then yes. If they use nonstandard features specific to ksh, zsh, or bash then they need to be run using that specific shell.

    – tripleee
    Jul 22 '18 at 13:58













53












53








53







Put your script in a file foo.py starting with



#!/usr/bin/python


then give execute permission to that script using



chmod a+x foo.py


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



See documentation of execve(2) which is handling the shebang






share|improve this answer















Put your script in a file foo.py starting with



#!/usr/bin/python


then give execute permission to that script using



chmod a+x foo.py


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



See documentation of execve(2) which is handling the shebang







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 7 '15 at 16:47

























answered Jan 4 '12 at 13:51









Basile StarynkevitchBasile Starynkevitch

178k13169365




178k13169365












  • is #!/bin/sh good for shell scripts?

    – Tomer
    May 14 '17 at 12:19






  • 1





    @Tomer If they are POSIX sh shell scripts then yes. If they use nonstandard features specific to ksh, zsh, or bash then they need to be run using that specific shell.

    – tripleee
    Jul 22 '18 at 13:58

















  • is #!/bin/sh good for shell scripts?

    – Tomer
    May 14 '17 at 12:19






  • 1





    @Tomer If they are POSIX sh shell scripts then yes. If they use nonstandard features specific to ksh, zsh, or bash then they need to be run using that specific shell.

    – tripleee
    Jul 22 '18 at 13:58
















is #!/bin/sh good for shell scripts?

– Tomer
May 14 '17 at 12:19





is #!/bin/sh good for shell scripts?

– Tomer
May 14 '17 at 12:19




1




1





@Tomer If they are POSIX sh shell scripts then yes. If they use nonstandard features specific to ksh, zsh, or bash then they need to be run using that specific shell.

– tripleee
Jul 22 '18 at 13:58





@Tomer If they are POSIX sh shell scripts then yes. If they use nonstandard features specific to ksh, zsh, or bash then they need to be run using that specific shell.

– tripleee
Jul 22 '18 at 13:58











13














As you have mentioned doesn't changes anything,



First, you should redirect both stdin and stderr from crontab execution like below:



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


then you can view the file /tmp/listener.log to see if the script executed as you expect.



Second, guess what you mean change anything is by watching the files created by your program:



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


the crontab job above won't create these file in directory /home/souza/Documets/Listener, as the cron job is not executed in this directory, and you use relative path in the program. So to create these file in directory /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 to the working directory and execute the script from there, then you can view the files created in place.






share|improve this answer























  • what is 2>&1 mean?

    – Mohideen ibn Mohammed
    Feb 21 '18 at 12:30











  • @MohideenibnMohammed redirect error messages (stderr) to the visible command line (stdout)

    – Juha Untinen
    Apr 3 '18 at 15:05















13














As you have mentioned doesn't changes anything,



First, you should redirect both stdin and stderr from crontab execution like below:



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


then you can view the file /tmp/listener.log to see if the script executed as you expect.



Second, guess what you mean change anything is by watching the files created by your program:



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


the crontab job above won't create these file in directory /home/souza/Documets/Listener, as the cron job is not executed in this directory, and you use relative path in the program. So to create these file in directory /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 to the working directory and execute the script from there, then you can view the files created in place.






share|improve this answer























  • what is 2>&1 mean?

    – Mohideen ibn Mohammed
    Feb 21 '18 at 12:30











  • @MohideenibnMohammed redirect error messages (stderr) to the visible command line (stdout)

    – Juha Untinen
    Apr 3 '18 at 15:05













13












13








13







As you have mentioned doesn't changes anything,



First, you should redirect both stdin and stderr from crontab execution like below:



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


then you can view the file /tmp/listener.log to see if the script executed as you expect.



Second, guess what you mean change anything is by watching the files created by your program:



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


the crontab job above won't create these file in directory /home/souza/Documets/Listener, as the cron job is not executed in this directory, and you use relative path in the program. So to create these file in directory /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 to the working directory and execute the script from there, then you can view the files created in place.






share|improve this answer













As you have mentioned doesn't changes anything,



First, you should redirect both stdin and stderr from crontab execution like below:



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


then you can view the file /tmp/listener.log to see if the script executed as you expect.



Second, guess what you mean change anything is by watching the files created by your program:



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


the crontab job above won't create these file in directory /home/souza/Documets/Listener, as the cron job is not executed in this directory, and you use relative path in the program. So to create these file in directory /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 to the working directory and execute the script from there, then you can view the files created in place.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 12 '16 at 3:33









greenqygreenqy

26635




26635












  • what is 2>&1 mean?

    – Mohideen ibn Mohammed
    Feb 21 '18 at 12:30











  • @MohideenibnMohammed redirect error messages (stderr) to the visible command line (stdout)

    – Juha Untinen
    Apr 3 '18 at 15:05

















  • what is 2>&1 mean?

    – Mohideen ibn Mohammed
    Feb 21 '18 at 12:30











  • @MohideenibnMohammed redirect error messages (stderr) to the visible command line (stdout)

    – Juha Untinen
    Apr 3 '18 at 15:05
















what is 2>&1 mean?

– Mohideen ibn Mohammed
Feb 21 '18 at 12:30





what is 2>&1 mean?

– Mohideen ibn Mohammed
Feb 21 '18 at 12:30













@MohideenibnMohammed redirect error messages (stderr) to the visible command line (stdout)

– Juha Untinen
Apr 3 '18 at 15:05





@MohideenibnMohammed redirect error messages (stderr) to the visible command line (stdout)

– Juha Untinen
Apr 3 '18 at 15:05

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8727935%2fexecute-python-script-on-crontab%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto