Execute python Script on Crontab
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
|
show 22 more comments
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
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
|
show 22 more comments
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
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
linux cron crontab
edited Sep 30 '16 at 23:47
FractalSpace
3,32022840
3,32022840
asked Jan 4 '12 at 13:46
guisantogui
1,29333371
1,29333371
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
|
show 22 more comments
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
|
show 22 more comments
3 Answers
3
active
oldest
votes
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
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 thescript.py
only in the given directory?
– Shubham A.
Jan 3 '18 at 10:09
|
show 1 more comment
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
is #!/bin/sh good for shell scripts?
– Tomer
May 14 '17 at 12:19
@Tomer If they are POSIXsh
shell scripts then yes. If they use nonstandard features specific toksh
,zsh
, orbash
then they need to be run using that specific shell.
– tripleee
Jul 22 '18 at 13:58
add a comment |
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.
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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 thescript.py
only in the given directory?
– Shubham A.
Jan 3 '18 at 10:09
|
show 1 more comment
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
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 thescript.py
only in the given directory?
– Shubham A.
Jan 3 '18 at 10:09
|
show 1 more comment
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
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
edited Jan 4 '12 at 14:08
answered Jan 4 '12 at 13:50
Raul Marengo
1,7271119
1,7271119
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 thescript.py
only in the given directory?
– Shubham A.
Jan 3 '18 at 10:09
|
show 1 more comment
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 thescript.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
|
show 1 more comment
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
is #!/bin/sh good for shell scripts?
– Tomer
May 14 '17 at 12:19
@Tomer If they are POSIXsh
shell scripts then yes. If they use nonstandard features specific toksh
,zsh
, orbash
then they need to be run using that specific shell.
– tripleee
Jul 22 '18 at 13:58
add a comment |
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
is #!/bin/sh good for shell scripts?
– Tomer
May 14 '17 at 12:19
@Tomer If they are POSIXsh
shell scripts then yes. If they use nonstandard features specific toksh
,zsh
, orbash
then they need to be run using that specific shell.
– tripleee
Jul 22 '18 at 13:58
add a comment |
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
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
edited Nov 7 '15 at 16:47
answered Jan 4 '12 at 13:51
Basile Starynkevitch
176k13165359
176k13165359
is #!/bin/sh good for shell scripts?
– Tomer
May 14 '17 at 12:19
@Tomer If they are POSIXsh
shell scripts then yes. If they use nonstandard features specific toksh
,zsh
, orbash
then they need to be run using that specific shell.
– tripleee
Jul 22 '18 at 13:58
add a comment |
is #!/bin/sh good for shell scripts?
– Tomer
May 14 '17 at 12:19
@Tomer If they are POSIXsh
shell scripts then yes. If they use nonstandard features specific toksh
,zsh
, orbash
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
@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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
answered Jan 12 '16 at 3:33
greenqy
25635
25635
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
add a comment |
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
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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