need help to stop/restart apache2 via crontab

Good morning/evening everyone.

I'm facing a little problem here.

I have a perl script which I use to backup my DB (a large db).

(I run it via crontab)..

and sometime it crushing mysql server.

so I want to do the following.

1- stop apache service (to stop any http sql requests)..

2- run the backup script.

3- restart mysql to make sure that it'll run smoothly.

4- start apache service.

I have added the following to my crontab file

MAILTO=my@mail.com
0 0 * * * /etc/init.d/apache2 stop
1 0 * * * perl /var/path/to/my/backup/script
4 0 * * * /etc/init.d/mysql restart
5 0 * * * /etc/init.d/apache2 start

the backup script runs at time just fine, the same for mysql restarting command..

but the apache command is not executed..

please tell me what do I miss?

15 Replies

What you should really do is put those four commands into a script, and execute that one script from cron. That way you're sure one won't start before the previous one is finished. Maybe that will resolve the issue you're seeing also.

by script, I think you mean shell script, right?

if so, I don't have any previous experiences with shell scripting..

but if that the only way to cross over this problem, I'll appreciate it if someone direct me to appropriate start point to learn about shell scripting.

It would look something like this:

#!/bin/sh
/etc/init.d/apache2 stop
perl /var/path/to/my/backup/script
/etc/init.d/mysql restart
/etc/init.d/apache2 start

Save that as backup.sh (or whatever) and point cron to that.

Start here : http://linuxcommand.org/writingshellscripts.php

Basically, what you want is a file like this:

#!/bin/bash
/etc/init.d/apache2 stop
perl /var/path/to/my/backup/script
/etc/init.d/mysql restart
/etc/init.d/apache2 start

Then make it executable:

chmod +x /path/to/your/new/script

And put it in your crontab instead of all the individual commands.

Edit: Oops, Xan beats me to it. #!/bin/sh might be a little more lightweight than #!/bin/bash, depending on the way your distro is wired up.

At the most basic level, a shell script is just a series of commands. It's the same as typing the contents of the script into the console.

#!/bin/bash

/etc/init.d/apache2 stop
perl /var/path/to/my/backup/script
/etc/init.d/mysql restart
/etc/init.d/apache2 start

That's not to say that what you're doing looks particularly proper for backing anything up…

Xan, hybinet and Guspaz

I can't find enough words in my dictionary to thank you.

I'll follow every example posted above; I don't why, but I feel sure that will work just fine..

I'll reply again if I get any trouble, thanks a lot guys.

am very happy guys.

!/bin/bash

working very will (with ubuntu 8.10)..

one thing I'd like to note for anyone in my situation is that,

if you wrote your shell script using notepad (or the like), you will end up with similar error message when you try to run your shell script

"path to your script: /bin/sh^M: bad interpreter: No such file or directory"

I installed tofrodos package and ran the command dos2unix path/to/my/shell/script

and that solve it..

thanks once more all of you guys.

That was probably the best forum race condition I've ever seen.

It's apache2ctl, not apache2.

It's

/usr/sbin/apache2ctl -k restart

or

/etc/init.d/apache2 restart

Jed, I think you need to install some kind of mutex on the forum here! I'd have grabbed it before writing the script that three of us wrote, and things would be much tidier. :-)

Hmm, a real time communication protocol exposing a way to discuss Linode (and other things)… I should invent this

@jed:

That was probably the best forum race condition I've ever seen.
And I thought at least some of us were trying to fix the OP's potential race condition! 8)

@Xan:

Jed, I think you need to install some kind of mutex on the forum here!
Naw. People fighting over replies is better, as it communicates to me the willingness of the community to help. :)

@mwalling:

Hmm, a real time communication protocol exposing a way to discuss Linode (and other things)… I should invent this

It looks like the latest stable version of CGI:IRC was released over 3 years ago. You should consider Mibbit (which, coincidentally, is hosted at Linode). It gets much more active development, is much more feature-rich and usable, and can be embedded in sites (http://widget.mibbit.com/manager/).

The downside? Closed-source, so it only runs on Mibbit's linode. Can't port it elsewhere.

For completeness' sake, I'll note that multiple commands can be placed on a single line, separated by semicolons. Instead of writing a script, you could put the line *0 0 * * * /etc/init.d/apache2 stop ; perl /var/path/to/my/backup/script ; /etc/init.d/mysql restart ; /etc/init.d/apache2 start* into your crontab. When the first command completes, the second one will run, and so on.

There might be line-length limitations in crontab, so I wouldn't go overboard and generate huge strings of commands.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct