I'm working on implementing some cronjobs with my application running on elastic beanstalk but am unsure of how to proceed. My current cron-linux.config file in the .ebextension folder looks like:
files:
"/etc/cron.d/mycron":
mode: "000644"
owner: root
group: root
content: |
* * * * * root /usr/bin/python opt/python/current/app/api/cron.py > /dev/null
commands:
remove_old_cron:
command: "rm -f /etc/cron.d/*.bak"
I've used eb ssh to make sure that the paths point to the correct location. The problem is that I'm not getting any error messages so it's quite hard to know where the problem is. Any help would be much appreciated!
You are supressing your outputs.
Try replacing the schedule line from:
* * * * * root /usr/bin/python opt/python/current/app/api/cron.py > /dev/null
To:
* * * * * root /usr/bin/python opt/python/current/app/api/cron.py > /home/<USER>/logs/backup.log 2>&1
You should be able to see the logs in /home/<USER>/logs/backup.log. Make sure that your script is outputing messages, success or error.
Related
Greetings fellow programmers,
I am currently using django-background-tasks(https://django-background-tasks.readthedocs.io/en/latest/)
to run some background tasks on AWS elasticbeanstalk. I initially use this command in the main .config container commands but I get timeout error in deployment because this management command will never finish(it continues to run on).
Now, I am trying using the approach suggested for running cron job on elasticbeanstalk(https://aws.amazon.com/premiumsupport/knowledge-center/cron-job-elastic-beanstalk/). Please take a look at my code, it is not running the command. what is wrong pls? I just need the command python manage.py process_tasks to keep running on. This is working properly on my local machine since i can easily open another terminal to fire up the python manage.py process_tasks command
files:
"/etc/cron.d/process_tasks_cron":
mode: "000644"
owner: root
group: root
content: |
* * * * * root /usr/local/bin/99_process_tasks.sh
"/usr/local/bin/99_process_tasks.sh":
mode: "000755"
owner: root
group: root
content: |
#!/bin/bash
date > /tmp/date
# Your actual script content
source /var/app/venv/*/bin/activate
python manage.py process_tasks
commands:
remove_old_cron:
command: "rm -f /etc/cron.d/*.bak"
this a working version of the code. thanks and hope it helps someone out there.
files:
"/etc/cron.d/cron_process":
mode: "000644"
owner: root
group: root
content: |
* * * * * root /usr/local/bin/task_process.sh
"/usr/local/bin/task_process.sh":
mode: "000755"
owner: root
group: root
content: |
#!/bin/bash
exec &>> /tmp/cron_capture_log.txt
date > /tmp/date
source /var/app/venv/staging-LQM1lest/bin/activate
cd /var/app/current
python manage.py process_tasks
commands:
remove_old_cron:
command: "rm -f /etc/cron.d/cron_process.bak"
I've been trying to figure out how to best call a script with cronjobs and am unable to figure it out. Either I go with a custom command where I use the following in .ebextension/"some config file":
container_commands:
01_some_cron_job:
command: "cat .ebextensions/some_cron_job.txt > /etc/cron.d/mycron && chmod 644 /etc/cron.d/mycron"
leader_only: true
some_cron_job.txt:
* * * * * root source /opt/python/run/venv/bin/activate && source /opt/python/current/env && /usr/bin/python /opt/python/current/app/manage.py cron_command >> /var/log/myjob.log 2>&1
This works when i run the command locally but after having uploaded it to eb I get the following error:
File "/opt/python/current/app/manage.py", line 18
) from exc
^ SyntaxError: invalid syntax
Or I could call the script directly:
* * * * * root source /opt/python/run/venv/bin/activate && source /opt/python/current/env && /usr/bin/python /opt/python/current/app/api/cron.py >> /var/log/myjob.log 2>&1
But am then getting import errors when trying to import a function from a another file in the same directory:
ImportError: attempted relative import with no known parent package
I'm quite lost and would appreciate any help.
I managed to find a working solution where I instead used:
files:
"/etc/cron.d/mycron":
mode: "000644"
owner: root
group: root
content: |
0/10 * * * * root source /opt/python/current/env && /opt/python/run/venv/bin/python3 /opt/python/current/app/manage.py cron_command >> /var/log/newjob.log 2>&1
commands:
remove_old_cron:
command: "rm -f /etc/cron.d/mycron.bak"
I think the problem came about because of some python version problems in the virtual environment.
So, I have a python script which appends me a file. I want to schedule it with cron, to be run every minute. I tried a lot of solutions, but still cron is not running my script.
Here is my python script:
#!/usr/bin/env python3
import datetime
f = open("test.txt", "a+")
f.write(str(datetime.datetime.now()))
And this is my cron file:
*/1 * * * * /home/iczyrskidc/PycharmProjects/testzone/testzone.py
I tried to give a permission to a file with:
chmod +x testzone.py
And this is output of log file:
Aug 26 11:39:01 OptiPlex-7010 CRON[12502]: (iczyrskidc) CMD (/home/iczyrskidc/PycharmProjects/testzone/testzone.py)
Any ideas what's happening that the cron is not working?
*/1 * * * * cd /home/iczyrskidc/PycharmProjects/testzone && /usr/bin/python3 testzone.py
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
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