Search code examples
linuxbashcronubuntu-server

linux cron script with function doesn't running


I'm trying to run a script as a job in the cron.

The script:

echo "Defining function"
function talkMe() {
        echo "me"
}
echo "Start talking"
talkMe

The cron job:

0 18 * * * /scripts/talk.sh > /tmp/talk.log

The cron errror:

Aug 23 11:24:01 database CRON[8497]: (root) CMD (/scripts/talk.sh > /tmp/talk.log)
Aug 23 11:24:01 database CRON[8494]: (CRON) error (grandchild #8497 failed with exit status 2)

The test.log:

Defining function

The script was working fine BEFORE I try this to change the ROOT password:

sudo su
passwd <my new pass for root>

This command seems to have no effect and the ROOT continues with the old password... but after this commands the CRON problems appeared!

How can I solve the CRON problem, to run the script correctly?

Note: running manually the script, everything goes fine!


Solution

  • Your script has no she-bang line. Insert this line to the top of the script:

    #!/bin/bash
    

    It is possible that the script is being run by a different shell that uses a different syntax to define functions.