I just started using Cron to automate this one python script I have. I understand how to use all the time parameters in nano, but I'm confused with how you would run the script. Normally just to run it right from the console, I would do:
cd /pi/home/weather/Adafruit_Python_BMP/examples
and then from there I would run the script with:
python weatherFINAL.py
Now that I'm trying to automate this in Cron, I can't do the multiple commands to cd into the directory, and then run the program. I know this is probably a really easy problem to fix, but I've been stuck on this for a while. Any help is appreciated
It is quite simple:
Write a shebang line on the top of your script in order to make it executable:
!/usr/bin/env python
Make sure you can execute that script with chmod command:
$ chmod +x
Programn a crontab task with contrab command:
$contrab -e
Related
I have a python application and I run it using python app.py but when the machine restart for any reason the application stoped
I searched and I found that I can make it run again using corn jobs
so I installed crontab and add the following code to it
#reboot /home/airnotifier/airnotifier/airnotifier.sh
and this is the code in the file airnotifier.sh
cd /home/airnotifier/airnotifier
python ./app.py
cd airnotifier
python ./app.py
cd /home/airnotifier/airnotifier
python app.py
cd airnotifier
python app.py
as you can see, I tried all combinations to make the application run again
can anyone tell me what did I do wrong?
I would try to just add this directly to the crontab
#reboot python3 /home/airnotifier/airnotifier/app.py
Unless you're on an older 2.x version of python, you should call it with python3
I am new to crontab and I'm struggling with the basics. I a lot of different issues. This is all being done on my Raspberry Pi. I am trying to schedule a python script to run every 10 minutes on weekdays. What makes this trickier is that my python script needs to use a virtual environment.
After doing some research I saw I could just activate the virtual environment with a bash script and then run the python script through that. This is the bash script:
#!/bin/bash
cd /home/pi/Desktop/projects/my_project
source env/bin/activate
python my_script.py
I have no idea if this is the best way to run python in a virtual environment through crontab but its all I could find online.
This is the code for the crontab itself:
SHELL=/bin/bash
0-59/10 * * * 1-5 /home/pi/Desktop/projects/my_project/cron_script.sh
I tested the crontab code by running this in the command line: sudo crontab -l | grep -v '^#' | cut -f 6- -d ' ' | while read CMD; do eval $CMD; done. Again I have no idea if this is the best way to test it immediately. This throws an error message:
bash: /home/pi/Desktop/projects/my_project/cron_script.sh: Permission denied
I'm really just confused at a lot of different steps here. Summary:
I don't know if I'm using a python virtual environment in crontab correctly or whether there's a cleaner way to do it.
I don't know if there is a better way to test crontab immediately. Writing the print statements from my python script to a txt file would also be nice.
I'm getting a permission denied error message.
The error you're having is likely due to the fact that your bash script is not marked as executable. You can use chmod to change access permissions.
Try this :
chmod +x /home/pi/Desktop/projects/my_project/cron_script.sh
I have a python program which I need to run at a particular day of a month, so I am using crontab for this task and create a shell script to run this python program.
This is part of my shell script:
#!/bin/bash
filepath='file2018'
cd ${filepath}
python3 file.py
When I run the crontab which executes the shell script, the log file shows the following error:
line 9: python3: command not found
I'm really confused about why this error occurs because I have already install python3 and I can run python3 directly from the command line.
Besides, if I replace python3 with python, the shell script works! My python version is python2, but I have to use python3 for this program, so I have to use python3 instead of python.
My operating system is Linux CentOS.
Hope someone can give me some tips!
You can give the full path to the python3 executable. You can get it using the which python3 command. Try it out.
in file.py add first line like below and add +x permission to file.py file
#!/usr/bin/python3
it will automatically execute, no need to mention python3 in the script
use "which python3" command to know exact path of python3 in your machine
I have a bash file, it works fine when executed from terminal.
#!/bin/bash
source activate tensorflow_p36
python /home/ec2-user/abc/wsgi.py
Note: tensorflow_p36 being an in-built conda environment, does not require to be called from specific /env/bin directory. It can be activated from any directory. I think it's a feature of Amazon Deep Learning AMIs.
If I run this bash script with sudo it doesnt activate the virtual environment and works in default python environment. The python file can run in that virtual environment only.
I have tried all 3 alternatives (rc.local, .conf file, init.d config)here, also tried to use crontab as suggested here. I have also tried using supervisord to add this bash script as a program.
When the program runs from these methods, I always get the same import errors because it is using default python 3 environment which doesn't have the required dependencies.
I am working on Amazon CentOS (Deep learning AMI). Can someone please suggest a method to run this script every time system restarts?
In the rc.local, instruct root to run it as you:
su --command /path/to/bash/file --login grimlock
You can run it from your personal Crontab.
( crontab -l; printf '#reboot /path/to/bash/file\n' ) | crontab -
If you don't have a crontab there will be an error message from crontab -l but it's harmless.
crontab: no crontab for ec2-user
You just need to do this once, and the job will execute as yourself once the system comes up.
try to change source by .
. activate tensorflow_p36
python /home/ec2-user/abc/wsgi.py
also check chmod +x your path file.
I have written some simple Linux shell script. Now I am trying to figure out is that can I have a GUI developed with python and trigger my shell scripts from the GUI.
Is this possible. How can I do this. Any ideas or examples please.
Thanks.
Just make sure the python executable is in your PATH environment variable then add in your script
python path/to/the/python_script.py
Details:
In the file job.sh, put this
!/bin/sh
python python_script.py
Execute this command to make the script runnable for you : chmod u+x job.sh
Run it : ./job.sh