0

I have a python script that I've encapsulated in a bash function. I would like to be able to call this function as a cron job, but I cannot seem to get cron to execute it.
The function is as follows:

 #!/bin/bash

getmail(){
    local interp=/path/python3
    local cmd=/path/python-script
    local logfile=/path/logfile

    if [ "$1" == "-logs" ]; then 
        $interp $cmd >> $logfile

    else 
        $interp $cmd
     fi
 }

I've then created a script to source the function and execute it which I would like to be able to call from cron.

 #!/bin/bash

  source /path/getmail
  getmail

I've assigned this cron-script appropiate permissions, making it executable, but cron won't run the script. I can run the python script itself via cron, but not encapsulated withn the bash function. I would just like to know why. Might it have something to do with the interpreter that cron is using? I've set the SHELL=/bin/bash in cron tab. Can anyone shed some light on this for me?

| improve this question | | | | |
  • 2
    Use a complete destination directory path. For used Bash-shell and also for your Bash-script. Try this, for example: */10 * * * * /bin/bash -c /full-path/scriptname.sh > /dev/null 2>&1 or simple way: */10 * * * * /bin/bash -c /full-path/scriptname.sh. If you did, there are many answers with a similar problem (how to run a Bash script via CRON). – s3n0 Mar 3 at 17:50
  • Both bash and python scripts run on their own without problem. Meaning cron will execute them. The issue I'm having is the python script call encapsulated within the bash function. That doesn't execute. I am using full path for each file. – Le_Prometheen Mar 3 at 20:26
  • OK, well what is the error from your Python script ? There are no error messages ? You can also run a Python script directly via CRON. But you need to know the path to the Python interpreter - usually located here: /usr/bin/python (stackoverflow.com/questions/2589711/…). CRON example: */10 * * * * /usr/bin/python /full_path/python_script.py. Only importing modules can be a problem, but this can also be resolved by adding a suitable path to the python environments - sys.path.append("/path/to/python"). – s3n0 Mar 3 at 20:59
  • Show your Python script when you write that there is probably a bug in it. What is your first line of your Python script ? Do you miss this ? - #!/usr/bin/env python – s3n0 Mar 3 at 21:08
0

How did you determine that it doesn't work?
Actually, what you did should almost work.
Just two things to comment on:
1. the '#!/bin/bash/ in the file containing the function is not required and, as the file is sourced, has no functionality.
2. The "-logs" parameter will never make it into the "getmail()" function since you call the function (from your script) without any parameters.
Try again with the following modification to your bash script:

#!/bin/bash

source /path/getmail
getmail $@

This should 'forward' all parameters given to your bash script into the function.

| improve this answer | | | | |
  • Well - the obvious indication that it's not working is it doesn't deliver my mail. The script connects to a remote server and fetches messages. I have the server and local logs recording the transaction. None of this occurs when using cron in the method I'm trying. However, it does work when I run the script from the commandline and it even works when I just run the python script itself via cron using the /path/python3 /path/mailscript syntax. Just not encapsulated in the bash function. I'm going to do some more troubleshooting to see what I can learn about what's happening. – Le_Prometheen Mar 4 at 1:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.