Cron Job with PHP
This topic has been teasing me a moment before I went to bed yesterday night. Since it related to my current works, so I had to get it done soon or late. However, thinking about cron daemon will also think about the file permission & some annoyed things connected to it’s correlation flows. I mean, for example what crontab user signed in Linux when a cron job executed from PHP? Does it root, nobody, guest or what? Well, we’ll find it in a minutes.
In shorts, I made an HTML page containing a checkbox purposed to enabled or disabled a single cron service. The cron it self will running a simple wall command in every minutes. Here’s below the complete script:
When the checkbox checked, it will executing a file writing process to a file containing a line of cron command in temporary folder. In a wide public service such as in hosting services, you may use Linux global variable referring to temporary folder & temporary file since you’ll face a limited permission access to it.
In other condition, if the checkbox is unchecked, it’s action will simply removing the cron service from system by executing crontab –r command. Now, let’s test the script by loading it from client browser, checked the checkbox & hit the submit button. Then, switch back to Linux & find out what happened exactly from terminal.
From the first command – listing cron table from user root by giving command crontab –u root -l, you see that old crontab for root session still persist & there’s no new changes was made into it. The above script just not affected to root system anyway. What about the second command? It’s also work the same, nothing changes for nobody user cron session. So, who’s cron session changed? Take a look at last command, the above script influence apache user. That’s it, the puzzle has been solved! Within apache user, the cron worked great from PHP. According to this, the executable script listed as parameter in cron table line (such as /usr/bin/wall) must be visible to apache user. For custom bash shell script or any executable application, make sure to granted it by changing the attribute to 777 mode in order the script will work & visible to apache user. Good day!
In shorts, I made an HTML page containing a checkbox purposed to enabled or disabled a single cron service. The cron it self will running a simple wall command in every minutes. Here’s below the complete script:
<?
if (isset($_POST['Submit']))
{
if ($_POST['cbcrontab']==1)
{
$cron=1;
echo('Crontab Aktif');
system ("echo \*/1 \* \* \* \* /usr/bin/wall test cron dari PHP coy > /tmp/crontab");
system ("crontab /tmp/crontab");
}
else
{
$cron=0;
echo('Crontab TDK Aktif');
system ("crontab -r");
}
}
?>
<html>
<head>
<title>Cron Job Test</title>
</head>
<body>
<form name="form1" method="post" action="index.php">
<input name="cbcrontab" type="checkbox" id="cbcrontab" value="1" <? if ($cron) echo('checked'); ?>>
<font size="1" face="Verdana, Arial, Helvetica, sans-serif">Aktifkan Crontab</font>
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
When the checkbox checked, it will executing a file writing process to a file containing a line of cron command in temporary folder. In a wide public service such as in hosting services, you may use Linux global variable referring to temporary folder & temporary file since you’ll face a limited permission access to it.
In other condition, if the checkbox is unchecked, it’s action will simply removing the cron service from system by executing crontab –r command. Now, let’s test the script by loading it from client browser, checked the checkbox & hit the submit button. Then, switch back to Linux & find out what happened exactly from terminal.
From the first command – listing cron table from user root by giving command crontab –u root -l, you see that old crontab for root session still persist & there’s no new changes was made into it. The above script just not affected to root system anyway. What about the second command? It’s also work the same, nothing changes for nobody user cron session. So, who’s cron session changed? Take a look at last command, the above script influence apache user. That’s it, the puzzle has been solved! Within apache user, the cron worked great from PHP. According to this, the executable script listed as parameter in cron table line (such as /usr/bin/wall) must be visible to apache user. For custom bash shell script or any executable application, make sure to granted it by changing the attribute to 777 mode in order the script will work & visible to apache user. Good day!
Labels: PHP
PS: If you've benefit from this blog, you can support it by making a small contribution. |
Andy said,
Monday, July 25, 2011 4:43:00 PM
saya tertarik dengan artikel anda dan mencoba untuk menjalankannya di sebuah komputer yang terhubung dengan server.
if (isset($_POST['param'])) {
if ($_POST['param']=="activate") {
$cron=1;
echo('Crontab Aktif');
system("echo * * * * * php /home/andy/public_html/AndyResponder/SenderBot.php > /home/andy/public_html/AndyResponder/SenderBot.php");
system("crontab /home/andy/public_html/AndyResponder/SenderBot.php");
} else {
$cron=0;
echo('Crontab TDK Aktif');
system ("crontab -r");
}
}
itu kode yang saya gunakan tapi entah kenapa tidak dapat berfungsi dengan baik. bisa mohon bantuannya tidak bapak Eko ?]
Terima kasih
Hormat saya, Andy
Eko Wahyudiharto said,
Monday, July 25, 2011 5:11:00 PM
Silakan dicoba untuk menghilangkan baris berikut:
system("echo * * * * * php /home/andy/public_html/AndyResponder/SenderBot.php > /home/andy/public_html/AndyResponder/SenderBot.php");
Andy said,
Tuesday, July 26, 2011 9:41:00 AM
terima kasih atas balasannya.
kali ini saya menggunakan code berikut.
if ($_POST['param']=="activate") {
$output = shell_exec('crontab -l');
file_put_contents('./mycronjobtemp.txt', $output.$URL.PHP_EOL);
echo exec('crontab /home/andy/public_html/AndyResponder/mycronjobtemp.txt');
} else {
$cron=0;
echo('Crontab TDK Aktif');
system ("crontab -r");
}
system berjalan dengan baik. hanya saja ketika saya ketik "crontab -l" ataupun "crontab -e" di postfix saya, command crontab saya tidak berfungsi. sedangkan jika dari console tersebut saya ketik
"crontab /home/andy/public_html/AndyResponder/mycronjobtemp.txt" maka saat itu juga pada "crontab -e" dan "crontab -l" data saya langsung di input. apakah ada solusi agar saya tidak perlu mengetik langsung di console bapak eko ?
terima kasih sebelumnya.
Andy said,
Wednesday, July 27, 2011 1:24:00 PM
sudah berjalan dengan baik. terima kasih atas bantuannya.
Best regards,
Andy
𝖇𝖊𝖙𝖘𝖆𝖗𝖆𝖓𝖌885♡ said,
Tuesday, May 17, 2022 2:19:00 PM
This is a great article thanks for sharing this informative information. I will visit your blog regularly for some latest post
카지노사이트
토토