LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   crontab doesnt want to execute full script (https://www.linuxquestions.org/questions/linux-server-73/crontab-doesnt-want-to-execute-full-script-4175491475/)

czezz 01-16-2014 05:15 AM

crontab doesnt want to execute full script
 
Under Ubuntu 12.04 LTS Server I wrote very simple script to backup datastore - backup.sh
It looks like that:
Code:

#!/bin/bash
service mysql stop
tar -cpf /backup/mysql.tar /var/lib/mysql
service mysql start

If I execute this script manually ./backup.sh it works perfectly.
However, if I use crontab to execute this, then first command service mysql stop will NOT be executed.
Any ideas why ?

druuna 01-16-2014 05:24 AM

Are you running the script from root's crontab?
Are there any indicators in the log files about this?
Have you tried adding the full path to the command (/usr/sbin/service instead of a plain service)?

czezz 01-16-2014 06:00 AM

Hi, yes:
- I use root crontab
- I have tried with full path of service - /usr/sbin/service - no luck
- I have added file descriptor >> /tmp/backup.log to each line. The "stop" never appears there.

I forgot to mention that "start" works perfectly.

druuna 01-16-2014 06:57 AM

I wasn't talking about a self generated log but messages in /var/log.

Using this cron entry:
Code:

*/1 * * * * /root/blaat.sh >> /tmp/backup.log
If I do the following the cron job fails:
Code:

#!/bin/bash

service mysql stop
echo "sleeping 2 seconds"
service mysql start

This can be seen in /var/log/syslog:
Code:

<time stamp> plains /USR/SBIN/CRON[16149]: (root) CMD (/root/blaat.sh >>  /tmp/backup.log )
<time stamp> plains /USR/SBIN/CRON[16147]: (CRON) error (grandchild #16149 failed with exit status 127)

And this is mailed to me:
Code:

Subject: Cron <root@plains> /root/blaat.sh >>  /tmp/backup.log  (failed)
Date: <time stamp>

/root/blaat.sh: line 3: service: command not found
/root/blaat.sh: line 5: service: command not found

All works as expected when using this:
Code:

#!/bin/bash

/usr/sbin/service mysql stop
echo "sleeping 2 seconds"
/usr/sbin/service mysql start


czezz 01-16-2014 07:16 AM

Hi Druuna,
I think you had different problem than I am having.
As I wrote - this script works perfectly for me if I execute it manually.
It fails to execute command "/usr/sbin/service mysql stop" if I run it from crontab.
NOTE - it is only this one command and mind that "/usr/sbin/service mysql start" is executed correctly with no error.

I see no details in syslog and any other log file. It looks like this one is completely omitted... but why ???

druuna 01-16-2014 07:40 AM

Quote:

Originally Posted by czezz (Post 5099100)
I think you had different problem than I am having.
As I wrote - this script works perfectly for me if I execute it manually.

Both the scripts I posted run fine from the command line, but the first one doesn't run from cron.

Quote:

It fails to execute command "/usr/sbin/service mysql stop" if I run it from crontab.
NOTE - it is only this one command and mind that "/usr/sbin/service mysql stop" is executed correctly with no error.

I see no details in syslog and any other log file. It looks like this one is completely omitted... but why ???
What happens if you do the following:

- Change the script to:
Code:

#!/bin/bash

echo "---> stopping mysql"
/usr/sbin/service mysql stop

echo "---> creating backup"
/bin/tar -cpf /root/mysql.tar /var/lib/mysql

echo "---> starting mysql"
/usr/sbin/service mysql start

And change the crontab entry to:
Code:

*/1 * * * * /root/backup.sh >/root/backup.log 2>&1
BTW: It is rather strange that cron doesn't mail anything. Have you tried running the mail command (as root, from a terminal)?

czezz 01-16-2014 08:33 AM

Hi, thanks for reply.
I did like you said and here is result of the log file:

Code:

# cat /root/backup.log
---> stopping mysql
/usr/sbin/service: 123: exec: stop: not found
---> creating backup
/bin/tar: Removing leading `/' from member names
---> starting mysql
/usr/sbin/service: 123: exec: start: not found

This is extremely odd because I can execute each command manually at cli and it just works.

druuna 01-16-2014 08:53 AM

Only things that comes to mind:
- Your environment isn't set at all when using cron.
- The mysql script in /etc/init.d/ isn't up to par.

The best solution would be to give root's cron a minimal environment.

For now you could try adding this to your backup script (the bold part):
Code:

#!/bin/bash

export PATH=/usr/sbin:/usr/bin:/sbin:/bin

echo "---> stopping mysql"
.
.


druuna 01-16-2014 08:58 AM

I looked for Ubuntu's cron set up and came across this: Further Considerations

czezz 01-16-2014 09:26 AM

Ya, good point. Environmental variables seems not to be set for cron.
I have added PATH to script and it flies now :)
Thnaks!

By the way, do you know what is the proper way to set up PATH globally for corn (Ubuntu), so I dont need to mind about that in every other script ?

EDIT:
Your last post answers my last question.


All times are GMT -5. The time now is 08:21 AM.