LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 01-16-2014, 05:15 AM   #1
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 924

Rep: Reputation: 43
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 ?

Last edited by czezz; 01-16-2014 at 10:37 AM.
 
Old 01-16-2014, 05:24 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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)?
 
Old 01-16-2014, 06:00 AM   #3
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 924

Original Poster
Rep: Reputation: 43
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.
 
Old 01-16-2014, 06:57 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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
 
Old 01-16-2014, 07:16 AM   #5
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 924

Original Poster
Rep: Reputation: 43
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 ???

Last edited by czezz; 01-16-2014 at 07:18 AM.
 
Old 01-16-2014, 07:40 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by czezz View Post
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)?
 
Old 01-16-2014, 08:33 AM   #7
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 924

Original Poster
Rep: Reputation: 43
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.
 
Old 01-16-2014, 08:53 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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"
.
.

Last edited by druuna; 01-16-2014 at 08:55 AM.
 
1 members found this post helpful.
Old 01-16-2014, 08:58 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
I looked for Ubuntu's cron set up and came across this: Further Considerations
 
Old 01-16-2014, 09:26 AM   #10
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 924

Original Poster
Rep: Reputation: 43
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.

Last edited by czezz; 01-16-2014 at 09:29 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Crontab doesn't execute self-written script Annielover Linux - Newbie 12 05-18-2012 06:24 AM
I cant get crontab to execute my rsync script.... Theatre Linux - Server 3 09-10-2011 05:19 AM
crontab doesn't execute python script right msegmx Linux - Newbie 13 05-04-2009 09:29 AM
crontab is failed to execute my python script tcyeo Linux - Newbie 4 04-05-2009 11:14 PM
Script doesn't execute in crontab jis0501 Linux - General 2 08-04-2007 07:09 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 02:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration