Python crontab - How to check if cron is running? - python

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

Related

Python script runs from terminal, but not crontab. Using absolute paths but to no avail

This is my crontab.
SHELL=/bin/bash
PATH=/home1/<user>/.pyenv/shims:/home1/<user>/.pyenv/bin:/usr/lib64/qt-3.3/bin:/usr/nhnkrb5/bin:/usr/bin:/bin:/usr/X11R6/bin:/home1/<user>/.local/bin:/home1/<user>/bin
HOME=/home1/<user>
# Do something
0 1 * * * /home1/<user>/.pyenv/shims/python /home1/<user>/folder/myscript.py >> /home1/<user>/folder/$(date "+%Y.%m.%d-%H.%M.%S").log 2>&1
It runs perfectly from the terminal no matter where I execute it from. I have tried every answer on this page, and my cron doesn't return any errors.
https://askubuntu.com/questions/23009/why-crontab-scripts-are-not-working
I have also checked that my $PATH the cron runs from is identical to the one in my bash. Checked using * * * * * env > /tmp/env.txt
Please tell me what am I doing wrong.
It is better to add the python header at line 1 to your main script. For example if your main script file is app.py then it's content can be following:
!#/home1/<user>/.pyenv/shims/python
# Here goes your Python Script.
and then
$ chmod +x app.py
Now you can add
# Do something
0 1 * * * /home1/<user>/folder/myscript.py >> /home1/<user>/folder/$(date "+%Y.%m.%d-%H.%M.%S").log 2>&1
I ran into the same problem, script working when I am running manually but didn't work with crontab. This method worked.

Cronjob is not running Python script on Raspberry

On my Raspberry I try to run a Python script as a cronjob. The script does run and execute perfect via the console when I ask him to. However I cannot manage to run the task in cron. Why is this cronjob not working?
What I’ve did so far
Logged in as root user
Granted all rights at the .py file in the Finder to anybody and chmodded the file to 777
Edited both sudo crontab -e and crontab -e files and I did edit the /etc/crontab file (they are three different files(?!)).
Tested crons with each of the below cron codes (I did reboot after every edit).
Checked the error log /var/log/syslog/ and saw no errors regarding these cron tasks.
Tested with an output file: >> /home/root/Desktop/pi.log 2>&1. That didn’t provide me output.
cronjob commands
/2 * * * * Python /home/root/Desktop/pi.py
/2 * * * * /usr/bin/python /home/root/Desktop/pi.py
/2 * * * * cd /home/root/Desktop/ Python pi.py
The Python script is a bit long. The main function is to retreive data from a usb device and store this in an sql database. See full code : https://github.com/gejanssen/slimmemeter-rpi/blob/master/p1db/p1uitlezerdb-ESMR50.py

Crontab Python Script Not Working

I am running a python script on a Linux EC2 instance (the standard AMI) and I am having trouble executing a python script through the Crontab. I have another cron job already running and followed the same format. I think I'm missing something simple, but have had trouble identifying the cause. Here is what pops up when I run crontab -e
*/5 * * * * ~/scripts/aws-scripts-mon/mon-put-instance-data.pl --mem-used-incl-cache-buff --mem-util --disk-space-util --disk-path=/ --from-cron
*/1 * * * * ~/scripts/python cpu-util.py
The error I get in the logs is /bin/sh: /root/scripts/python: No such file or directory
I'm a little confused about this error message because the path from when I log in is ~/scripts, which has my Python script.
I also tried */1 * * * * ~/scripts python cpu-util.py (which I thought made more sense), but rearranged my code based on this other post to no avail.
Also, does it matter if I run these tasks from root or ec2-user? I just put the same scripts in both to be safe (sorry if this is two questions in one, but just curious about this...)
Any input would be great. Thanks!
in your line you are looking for
the python application inside your scripts folder
I guess this is not what you intended. Try this out:
*/1 * * * * /usr/bin/python ~/scripts/cpu-util.py
I think this should work.
Also you can call it directly using ./ just putting inside your python script as the first line.
#!/usr/bin/env python
Then you can run it like this
*/1 * * * * /usr/bin/sh ~/scripts/cpu-util.py

Crontab and Python prog not running completely

I am trying to get a Python script to run hourly using crontab however I cannot seem to get it to work.
The Python program runs fine and completes if I manually run it from terminal.
$ python /home/pi/Documents/Project/Base_Prog.py
My crontab is setup like so:
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow command
#reboot /usr/bin/python /home/pi/Documents/Project/Base_Prog.py
0 * * * * /usr/bin/python /home/pi/Documents/Project/Base_Prog.py
0 * * * * /usr/bin/python /home/pi/Documents/SimpleCronTest.py
Let me clarify though, I followed suggestions on this similar post. I tried the simple test script that creates an output file and that worked. However under the same crontab with the same settings, my program doesn't complete.
The interesting point is that using TOP when the script is due to start, the Python window briefly pops up before disappearing again. So I assume cron is at least sort of working.
I have added the:
#!/usr/bin/env
Python line to the top of the Base_Prog.py file.
My Python program is an API scraper that finds its inputs from one file and writes the results to another, all files in the project directory have full write permissions using chmod 777.
I am at a loss as to what is causing this.
UPDATE
The output log for both the simple test and my programme in the syslog is:
Apr 2 14:29:01 raspberrypi CRON[1455]: (pi) CMD (python /home/pi/Documents/Project/Base_Prog.py)
Apr 2 14:29:01 raspberrypi CRON[1456]: (pi) CMD (python /home/pi/Documents/CronTest.py)
I think full solution is:
First add a SHEBANG line on top of your python script:
#!/usr/bin/env python
Make your script executable with chmod +x
If your script work with stdout or if you need to know what's wrong:
0 * * * * /usr/bin/python /home/pi/Documents/Project/Base_Prog.py >> tmp.log
And just to clarify:
0 * * * * -means the cron will run always when the minutes are 0 (hourly)
You need to see the output of the cron execution for any clues, not just running it from terminal. Perhaps there are some permission/ownership errors or something along those lines. From the cron execution, you can send output to a file to view:
0 * * * * python /home/pi/Documents/Project/Base_Prog.py 1> /dev/null 2> /home/pi/Documents/Project/Base_Prog.err
and you can view /home/pi/Documents/Project/Base_Prog.err after for clues. Or you can also have it emailed to you:
0 * * * * python /home/pi/Documents/Project/Base_Prog.py 1> /dev/null 2>&1 | mail -s "Base Program Output" you#company.com

Using Crontab with Python

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

Categories

Resources