LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-30-2011, 08:56 PM   #1
ggalan
Member
 
Registered: Oct 2010
Posts: 57

Rep: Reputation: 0
cron php


i found this script which checks every few minutes if a file exists.
can anyone help me understand the highlight what looks like a shell script.
how does it trigger the php to look for the file?
also what extension would i save this as?
cron.sh
cron.cgi
?
Code:
#!/usr/local/bin/php -q
<?PHP
if( !file_exists( "./.pending-hup" ) )
{
		exit;
}
$qmailSendPID = intval( `ps -axw | grep qmail-send | grep -v grep | awk '{ print $1 }'` );
if( $qmailSendPID )
{
		if( !posix_kill( $qmailSendPID, 1 ) )
		{
				echo "ERRO: posix_kill( qmail-send, HUP ) falhou.\n";
		}
		else
		{
				@unlink( "./.pending-hup" );
		}
}
?>
 
Old 12-30-2011, 09:25 PM   #2
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Why not use shell script instead ?

Code:
#!/bin/bash

if [[ ! -f "./.pending-hup" ]]; then
    exit
fi

if ! killall -s 1 "qmail-send"; then
    echo "ERRO: posix_kill( qmail-send, HUP ) falhou"
else
    rm "./.pending-hup"
fi

Last edited by Cedrik; 12-30-2011 at 09:31 PM.
 
Old 12-30-2011, 09:31 PM   #3
ggalan
Member
 
Registered: Oct 2010
Posts: 57

Original Poster
Rep: Reputation: 0
thank you for responding
i am actually trying to reload vhost on apache and was told to use
apachectl graceful

where would i put the shell script? in the bin-cgi folder?

Last edited by ggalan; 12-30-2011 at 09:33 PM.
 
Old 12-30-2011, 09:34 PM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
You could put it outside your web accessible directory, but you may have to change the path to "pending-hup" file

The script will be run by cron, no ?
 
Old 12-30-2011, 09:36 PM   #5
ggalan
Member
 
Registered: Oct 2010
Posts: 57

Original Poster
Rep: Reputation: 0
where would you put it normally?
cron, i think thats what i want.
basically i have php generating a file and i want cron the check if a file exists, if so then reload apache then delete this file
 
Old 12-30-2011, 09:41 PM   #6
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Put the script in a place that has not public access

Then configure your crontab line to run this script.

Make sure you update the path to "pending-hup" file accordingly to the place where you will put the script

[edit]
The script will not do what you want, I think

To restart Apache, you also need to have root privilege

Last edited by Cedrik; 12-30-2011 at 09:44 PM.
 
Old 12-30-2011, 09:43 PM   #7
ggalan
Member
 
Registered: Oct 2010
Posts: 57

Original Poster
Rep: Reputation: 0
Quote:
configure your crontab line to run this script.
where would i do this?
 
Old 12-30-2011, 09:57 PM   #8
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Do you have a shell access to your web server ?
 
Old 12-30-2011, 09:58 PM   #9
ggalan
Member
 
Registered: Oct 2010
Posts: 57

Original Poster
Rep: Reputation: 0
yes, i use putty and can sign in as root
 
Old 12-30-2011, 10:24 PM   #10
ggalan
Member
 
Registered: Oct 2010
Posts: 57

Original Poster
Rep: Reputation: 0
does this make sense, i wanted to edit the original script to just reload apache in the presence of the file
Code:
#!/bin/bash

if [[ ! -f "./.pending-hup" ]]; then
    exit
fi

if apachectl graceful; then
    echo "ERRO: posix_kill( qmail-send, HUP ) falhou"
else
    rm "./.pending-hup"
fi
 
Old 12-30-2011, 10:46 PM   #11
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
It's close, but you inverted the if condition, I mean see the "!" negation:
Code:
if ! /usr/sbin/apachectl graceful; then
    echo "apachectl exited with error"
else
    rm "./.pending-hup"
fi
If apachectl exits with error, then echo...
else delete the file

Also, it is better to use full path to apachectl (/usr/sbin/apachectl ?)
Especially if you use the script with cron

Now to edit crontab, you just do:
Code:
crontab -e
see man crontab or google crontab, there are many documents about it

Last edited by Cedrik; 12-30-2011 at 10:50 PM.
 
Old 12-30-2011, 10:50 PM   #12
ggalan
Member
 
Registered: Oct 2010
Posts: 57

Original Poster
Rep: Reputation: 0
and this is where the file is located?
Code:
#!/bin/bash
so this is the final code?
Code:
#!/bin/bash

if [[ ! -f "./.pending-hup" ]]; then
    exit
fi

if ! /usr/sbin/apachectl graceful; then
    echo "apachectl exited with error"
else
    rm "./.pending-hup"
fi
that doesnt seem correct

Last edited by ggalan; 12-30-2011 at 11:07 PM.
 
Old 12-31-2011, 05:25 AM   #13
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
See where the php script creates the .pending-hup file, maybe modify it so it creates the file in same directory where the script is (outside the web directory).
 
Old 12-31-2011, 11:37 AM   #14
ggalan
Member
 
Registered: Oct 2010
Posts: 57

Original Poster
Rep: Reputation: 0
i was under the impression that "apachectl graceful" is a command that reloads apache like "service httpd reload"

but you said "path to apachectl". this is not a file

also what makes this code check for the presence of the file "pending-hup"?
i want to do a check every few minutes, not just a 1 time thing.

Last edited by ggalan; 12-31-2011 at 01:06 PM.
 
Old 01-01-2012, 02:45 PM   #15
ggalan
Member
 
Registered: Oct 2010
Posts: 57

Original Poster
Rep: Reputation: 0
anyone?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Use PHP to read cron launched php scripts? wh33t Programming 2 04-22-2011 07:32 PM
shell script using /etc/cron.hourly to execute cron.php file? rioguia Programming 3 06-11-2008 09:09 AM
cron/php/backups bitoine Linux - Newbie 2 05-05-2006 02:04 AM
cron and PHP bokini Programming 2 07-28-2005 05:56 PM
cron php under apache dave4545 Programming 25 04-17-2003 09:00 PM


All times are GMT -5. The time now is 04:25 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration