I run a simple python script by crontab in a CentOS 7.x server.
This is the code:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
#MAILTO=root
MAILTO=my_email_address
00 15 * * * root /usr/bin/python ./home/python_devs/run_program.py >> /home/logs/Python_log_`date +\%Y-\%m-\%d_\%H\%M`.log
Unfortunately, it creates the log file, but it is completely blank. If I remove the >> .... the script output is being sent to my email address defined in MAILTO= field.
So, how to save that output into the log file, which I then send by email?
Thank you.
Try this line:
00 15 * * * /usr/bin/python /home/python_devs/run_program.py >> /home/logs/Python_log_`date +\%Y-\%m-\%d_\%H\%M`.log 2>&1
I'm unclear why you had 'root' at the start of the line. Maybe that is a Centos-ism (I've always used debian based systems). But I've removed it since it appears you are trying to run a user file anyway.
Normally (debian) if you want root to run something you put it on the sudo crontab. I also removed the . before the path to the file.
Finally the 2>&1 also redirects stderr into your log. Maybe there is an error happening early and thus nothing is getting written to log, this will catch this case (and is a good practice anyway if you are trying to log a file this way)
Related
I have a python script that works fine when executed from command line. But when I try to run it from cron, it does not work.
command = "rsync -avHP --delete --bwlimit=800 rsync://mirror.nsc.liu.se/CentOS/7.8.2003/ /home/me/reposync/centos"
proc=subprocess.run(command.split(), capture_output=True)
The cronjob runs. The cronfile looks something like this:
PATH=/home/me
40 13 * * * me cd $PATH && ./reposync.py sync 2> /tmp/test.txt
But I get this error (yes, twice) from print(proc.stderr.decode('utf-8')):
-avHP: rsync: command not found
It seems like the problem has to do with not finding rsync but I don't know what to do about it.
The output from /tmp/test.txt:
FileNotFoundError: [Errno 2] No such file or directory: 'rsync'
I have tried adding shell=True to the subprocess.run but it does not seem to make a difference. Or well it does not throw that exception, but I still get the same error when printing stderr from proc.
I suppose that I could solve it by including the absolute path to rsync but it feels like it's a bad idea. But I could be wrong about that. If that is the right approach, please explain why.
In your crontab, you are overwriting your $PATH variable, which now contains only the directory /home/me. Your rsync executable now cannot be found, since it is not in your home directory.
Change your crontab entry by removing the PATH=... line and calling your script with it's full path:
40 13 * * * me /home/me/reposync.py sync 2> /tmp/test.txt
You might have to define shell in your crontab.
The question has been asked previously.
crontab: python script being run but does not execute OS Commands
I currently have a python script at
/home/ubuntu/test/test.py
When this script runs, it writes to a file
/home/ubuntu/test/test.txt
I am completely new to cron, and not very familiar with linux in general. I am trying to set up a cronjob that basically runs this script every minute.
I saw some people suggest #!/usr/bin/env python so I added it, but I noticed I don't even have a env folder in /usr/bin
I then ran chmod -x test.py. Then added an entry to cron * * * * * /home/ubuntu/test/test.py. Noted this wasn't working and saw someone suggest trying * * * * * /home/ubuntu/test/test.py 2>&1 /tmp/testlog.log. But when I check /tmp i only see a folder crontab.8Rxowt/crontab/cron and i don't see any log file created.
I am kind of confused now, I can't figure out why nothing is being updated at all. I'm not sure if the script being run needs to be placed somewhere specific, or if I screwed something up with my cron installation, or something else altogether.
I noticed trying to run ./test.py gives permission denied, and sudo ./test.py gives command not found. Is my shebang not working? I verified im using unix line endings.
To make it run every minute you have to add the path to python from your system:
* * * * * /usr/bin/python cd /path_to/test.py
I suggest you to test it with a simple command such as "touch"
* * * * * /usr/bin/touch cd /path_to/test.txt
https://crontab.guru/every-1-minute
I tried using cron to start python script , at boot (as mentioned in the link).Running a python script At Boot using cron
I made a python script "hello.py":
#!usr/bin/env python
import time
print "Hello World!"
time.sleep(10)
Then chmod +x hello.py.
I checked,if it running or not,it is giving me output.
I used crontab -e command and added the line #reboot python /home/pi/hello.py &.
Reboot using sudo reboot , but nothing happened! I am newbie .Although I read many discussions but I am not able to fix that!
Generally when I want to verify whether a cronjob ran or not, I redirect all output to a log file like so:
12 0 * * * python /Path/To/File.py > /Path/ToLog/log 2>&1
Then you can check this log timestamp and for its contents
Python crontab script doesnt seem to work. When i run it manually,
python /home/ec2-user/code1.py
it works fine but when put into cron.txt file for the crontab, doesnt.
My crontab file is:
#hourly python /home/ec2-user/code1.py >/dev/null 2>&1
i also tried
0 * * * * python /home/ec2-user/code1.py >/dev/null 2>&1
But neither have much luck.
sudo crontab -l
#hourly python /home/ec2-user/code1.py >/dev/null 2>&1
Shows everything functional.
I tried Crontab not running my python script and couple others with not much luck either.
EDIT:
With
PATH=/opt/python2.7/bin
MAILTO=my#email
*/5 * * * * /home/ec2-user/code1.py
Email i get is:
/bin/sh: /home/ec2-user/code1.py : No such file or directory
Yet I can open and edit the file no problem. I tried many different thing but it comes down to this: cron doesnt see the file.
Feels like I went through entire https://askubuntu.com/questions/23009/reasons-why-crontab-does-not-work
and still no luck
Verify cron is running: ps aux | grep [c]ron should show a running cron process
Remove the redirects from the command so that cron emails you the output
Add a MAILTO=<email address> to your crontab, so that you get the email
Put the full path to python (/opt/python2.7/bin/python) instead of just python in the command
Add another command to crontab such as echo FOOBAR and verify that you get the email.
ls -l /homeec2-user/code1.py ? Should that be /home/ec2-user/code1.py
Only ever edit a user's crontab with crontab -e never from another platform, or by editing the file directly.
Run crontab -l | cat -A so that we can verify all the control characters are correct.
did you check the following points?
is your script executable? chmod 700 code1.py
the first line in your code should be, in most cases the python is installed at this place
#!/usr/bin/python
after that the crontab as follow should execute
0 * * * * /home/ec2-user/code1.py >/dev/null 2>&1
If the error message is correctly copy/pasted, it seems to reveal that there is a problem with the crontab file. If you created it on a foreign platform, it might be best to start over with an empty file, this time creating it in a native editor.
As others have already pointed out, redirecting output and errors to /dev/null basically makes debugging impossible, so don't do that. If your program creates copiously verbose uninformative output, run it in a wrapper which filters out the trivial diagnostics, or, if it is your own program, rewrite it to run silently in normal operation.
Did you try "/usr/bin/python" instead of "python"?
ps ax | grep python
will give you the path you could use.
try this command that should hopefully where your python is :
which python
very likely you will have something like
/usr/bin/python /home/ec2-user/code1.py
I'm trying to schedule a python script to run every minute or every hour.
What I did so far :
crontab -e
In the crontab file I added
* * * * * /usr/bin/python /path/to/script/script.py
After I save the file I get the message
crontab: installing new crontab
However crontab is not running the script.
Any ideas?
Thanks,
Diez
I found the problem, I will put here the solution maybe it helps someone.
I tried putting in crontab -e * * * * * /usr/bin/python /path/to/script/script.py >>/tmp/script.out 2>&1 as Rafal suggested however the output was blank.
I read a lot about crontab last night and I found out that if you use a script with crontab and you write your output in a certain file you will need to modify your script and input the exact path for the file. If you don't put the full path the script will work when you run it manually. The script will still work if you run it with crontab but it won't know where to place its output.
So modifying
with open('output.txt', 'a') as f
with
with open('/path/to/file/output.txt', 'a') as f
did the job for me.
Thanks,
Diez
Depending on the script (e.g. scripts which prints a message to the console) you won't be able to distinguish whether the script is running or not.
You can assure that crontab is running correctly by typing in the console:
tail -f /var/log/syslog