need help to stop/restart apache2 via crontab
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
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.
#!/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.
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.
#!/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…
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.
!/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.
/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.
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 (
The downside? Closed-source, so it only runs on Mibbit's linode. Can't port it elsewhere.
There might be line-length limitations in crontab, so I wouldn't go overboard and generate huge strings of commands.