crontab bash script execution - Raspberry Pi - python

I have a bash script that I'm using to execute a python file with a specific version of Python (3.6). The Bash script is currently located on my desktop (/home/pi/Desktop/go.sh)
#!/bin/bash
python3.6 /home/pi/scriptDir/myScript.py
Here is my crontab entry, when I do crontab -l (note, I've deleted my other jobs)
* * * * * bash /home/pi/Desktop/go.sh # JOB_ID_3
When I run this file using the command line or from the GUI it executes properly.
When I have crontab do it, nothing happens.
Both my python file and the bash script are executable. chmod +x
Is there something obvious I'm missing?
**my python script does depend on other files in the same script directory, could that be the issue?

Here's what got it working for me. I was not using the full path to my python install. Unless you log the bash file there's no indication that you have an issue.
This is my bash file now. echo's were just to determine that I was indeed running the bash file.
#!/bin/bash
echo started
/home/pi/Python-3.6.0/python home/pi/myScriptFolder/myScript.py
echo finished
To break down the line executing the script:
/home/pi/Python-3.6.0/python - is where python 3.6.0 is installed on my Pi, it could be different for you. home/pi/myScriptFolder/myScript.py is the script I want to run.
And here is my cron statement:
*/15 * * * * bash /home/pi/Desktop/go.sh > /home/pi/Desktop/clog.log 2>&1 -q -f
Breaking down this line:
*/15 * * * * is the cron time, in this case every 15 mins. bash /home/pi/Desktop/go.sh specifies to run a bash file and the directory of that file. > /home/pi/Desktop/clog.log 2>&1 -q -f this last section creates a log file named clog.log so you can see what's going on.
The key here was not just logging the go.sh bash file execution, but adding the 2>&1 -q -f to the end of the log request. Before I did that there was no indication of a problem, afterwards I was getting the python files error returned into the log file.

Cron jobs are surprisingly tricky. Aside from working directory ( which you'd have to set somewhere), you also need to handle environment setup ( $PATH, for example).
Start by redirecting your shell script's standard output and error to a log file so you can get feedback.

Related

Why is crontab not running a python script

This is how I configure crontab (by using crontab -e)
* * * * * /home/jeff/Desktop/scripts/job_pull_queue.sh >> /home/jeff/Desktop/scripts/log.txt
This is the content of /home/jeff/Desktop/scripts/job_pull_queue.sh
#!/bin/bash
echo "Running job_pull_queue.sh # $(date)"
cd /home/jeff/Documents/code/some_project
echo $(printenv)
/home/jeff/miniconda3/bin/python -m util.main
Now the problem is, when running ./job_pull_queue.sh in the terminal, it works, but I can tell from the log file that crontab never executes that last line /home/jeff/miniconda3/bin/python -m util.main (I can see the result from the previous echo in the log file, but not the python script itself), what happened? How do I fix it?
Update: here's the result from printenv when ran by crontab
SHELL=/bin/sh PWD=/home/jeff/Documents/code/some_project LOGNAME=jeff HOME=/home/jeff LANG=en_US.UTF-8 SHLVL=0 PATH=/usr/bin:/bin OLDPWD=/home/jeff _=/usr/bin/printenv
Ok...
My Python script reads several env variables from my user profile, and of course, these variables don't exist when crontab is running the script...
And I don't have detection/logging in place so I didn't know env variables are missing.

Bash script executes Python manually, but not from cron

I've been trying to debug this for a while now and I feel like I've tried everything.
Code is slightly modified with *** for company reasons.
The following executes as expected when run from a session as my local user.
/var/www/****/***/run.sh path_to_my/script.py 2>&1 >> /var/www/****/***/test.log
Where run.sh is just a wrapper for running Python in a virtualenv:
#!/usr/bin/env bash
wd=$(dirname $0)
source ${wd}/virtualenv/bin/activate
python ${wd}/$1
I have placed a print statement inside of the Python main to show that it's being executed.
if __name__ == "__main__":
print("I got in here...")
When running the command as my local user, the log will contain this printed statement. However, when run in cron as:
*/30 * * * * /var/www/****/***/run.sh path_to_my/script.py 2>&1 >> /var/www/****/***/test.log
I do not get any printed statement, nor do I receive any error output from the 2>&1.
My permissions are 755 on both the .sh and .py scripts.
Everything works as expected except when run via cron.
Am I missing something? Does cron not use .bashrc for the crontab user?
First make sure your local cronjob is running by putting the following in the crontab file and check to see if it get written out at /tmp/env.output after a minute or two
* * * * * env > /tmp/env.output
Second make sure the user running the crontab has permission to write to the /var/www/****/***/test.log file
Third try changing your script to
wd=$(dirname $0)
cd $wd
source activate
python ${wd}/$1
Edited: Anders was able to figure out the answer by himself by adding PYTHONPATH to the cron environment: export PYTHONPATH="${PYTHONPATH}:${wd}"

Python Script in Crontab: rm: cannot remove, no such file or directory

I have a crontab running on Linux 3.10.0-862.9.1.el7.x86_64 x86_64. In it, I have
a python script that prints out certain files to delete, and then I pipe it to xargs rm.
for example, i would run the script python delete_these.py and it would print out the following:
/directory/to/delete1.txt
/directory/to/delete2.txt
/directory/to/delete3.txt
/directory/to/delete4.txt
and these would be deleted through | xargs rm run on the same command prompt. so
python delete_these.py | xargs rm
The script runs fine when run manually, however when it's run on cron, it returns an error in stdout retrieved from the mail when the job is run:
rm: cannot remove '/directory/to/delete1.txt': No such file or directory
here's what i input in crontab:
* * * * * ssh [confidential#stuff.com] python /location/where/my/python/script/is | xargs rm
i've done chmod +x /path/to/python/script and putting the full command (minus the stars, but including the ssh) works fine on the command line, but running it on cron returns the 'no such file or directory' error.
I am sure that these files exist, and they are not symblinks.
i've retried it with attaching the associated absolute directory for python (usr/bin/) but it still doesn't work.
i have no idea why the cron doesn't work. the current PATH on the crontab set by another user is a directory to usr/bin/stuff that is specific to that box.
I thought it would be an issue with the PATH being different, but I don't think this is the case as I specify absolute directories within the command.
any ideas? i'm totally stumped on this
you're running the python command on one computer and the rm on another computer
you should probably quote the pipe (|) so it doesn't separate things
at a guess the crontab line should be:
* * * * * ssh [confidential#stuff.com] "python /location/where/my/python/script/is | xargs rm"

cronjob error in OSX: "no path for address"

I tried running a Python script using cronjob but I get the following error:
cron[44405]: no path for address 0x10ff7a000
in grep cron /var/log/system.log
When I ran the script without using cronjob it worked:
/usr/bin/python  /Users/anuj/Desktop/message.py
I tried adding the cron job using $sudo crontab. This is the CRON script:
*/1 11-17 * * 1-7 /usr/bin/python  /Users/anuj/Desktop/message.py
Both paths are correct for root mode and user mode as I am running cron with sudo.
try to create file like message.sh
inside it run your .py file
#!/bin/sh
python path/to/python_script.py
and make this file executable with chmod a+x message.sh
*/1 11-17 * * 1-7 path/to/message.sh 2>&1

Crontab job won't trigger or save

Trying to run my first crontab job (a python script) on Mac OS X El Capitan.
In the terminal I run: crontab -e
which opens a crontab.xyz123 file in Sublime Text (as I set it up). I put in the following cron syntax:
30 * * * * /Users/user.name/anaconda/bin/python /Users/user.name/Desktop/python_script.py
I just want it to run every half hour. The python script opens a youtube video and I've included it below:
#!/Users/user.name/anaconda/bin/python
print("this is a script")
When I run crontab -l I get the following error:
crontab: no crontab for user.name - using an empty one
crontab: no changes made to crontab
How can I get this to run properly? Is something wrong with the cron job? With the script?
Other note: I've changed the script permissions to be executable and I'm using only full paths in the crontab itself. What else could be wrong? I've run the script by itself in command line and it works fine.

Categories