Crontab Python Script Not Working - python

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

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.

Running a python script in every 30 minutes using Crontab?

I want to run a python script for every 30 minutes. For this, I am using crontab.
I am new to crontab, I read and to run a script for 30 mins I have to use a query something like this:
*/30 * * * * python filename.py
But where exactly I have to fire this command.
I tried,
crontab -e
and change the file into,
*/30 * * * * python filename.py
Can someone explain how can I use crontab properly?
PS: I want to run a script for every 30 mins on a server that I have created on AWS ec2 instance, is there any alternate solution?
I am using Ubuntu 16.04
Suppose I have a python file test.py with the contents
print "hello"
To schedule it to run every 30 minutes, use
crontab -e
Then edit to add
*/30 * * * * python /path-to-file/test.py
To check if cron ran succesfully
grep CRON /var/log/syslog
Here, you will see in logs, lines like
May 31 14:25:01 shivam-PC CRON[17805]: (shivam) CMD (python /home/shivam/test.py)
Note: print statement might not show in logs, so use
*/30 * * * * python /path-to-file/test.py >> /path-to-file/out.txt
and then check out.txt for print logs.
An alternate solution would be to use Celery.

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

Python crontab - How to check if cron is running?

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

Cronjob for python script not working

I have an ubuntu box I'm trying to setup a cronjob for. When I do crontab -e this is what I am using as the instructions. I need it run every 11 minutes.
0/11 * * * * python /path/to/file/foo.py
At the top of the python script I have put:
#!/usr/bin/python
And I have done:
sudo chmod a+x foo.py
I'm not really sure how I am supposed to figure out how it ran correctly. The script works fine on my local, and appends something to a text file. I have been checking the txt file and nothing appears. Any suggestions?
The cron line
0/11 * * * * python /path/to/file/foo.py
will fire only when the minutes equals 0. To make it fire every 11 minutes use
*/11 * * * * python /path/to/file/foo.py

Categories

Resources