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:

<?
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:

  Post a Comment

Delphi: Integrating MyODBC Driver with Application

As many programmers know, there are several ways connection link can be set up between MySQL database server & client application. One simplest way can be done through MyODBC, the ODBC driver for MySQL. Except for those who using Delphi native component such as dbExpress or from 3rd party component like Zeus or application built with Lazarus compiler, the application deployment within MyODBC driver would required 2 setup wizards. One for the application & one for the MyODBC setup. Somehow, - for me – it will decrease prestige & users manner since they will face some annoyed parameters within doing the 2nd setup of MyODBC wizard.

Integrating MyODBC driver with application is another way & become my favorite trick when I did a client server project from Delphi. There are some reason why I still use ODBC & integrating it to the main program. First, I don’t even suspended with 3rd party component for expanding or future application release. All I need is only BDE & Data Access component. Then, I feel free to compile the codes wherever the native component & compiler exist. My application can also deployed smoothly even from Linux (with emulation engine) because nothing complicated API’s driver involved. Other advantages is, my client only need to installing a single setup software (including BDE’s & MySQL ODBC driver).

Based on my researches before, CMIIW - MyODBC driver just work with a single library file (myodbc3.dll) & some parameters planted in Windows registry. So, anyone can build their custom ODBC wizard & replace the functionality of standard MyODBC setup. There are 2 fixed registry track path of MyODBC driver & below is each of it:


Main Configuration of MySQL ODBC



Application Parameter Linked in MySQL ODBC

The trick to integrating it is so simple as you have to create similar registry value according the above parameters. And don’t forget to take the myodbc3.dll library file taken from original MyODBC setup. Below is a sample in Delphi on how to create & integrating the driver into the application:

AppIni := TIniFile.Create('odbc.ini');
Reg:=TRegistry.Create;
AppIni.WriteString('ODBC 32 bit Data Sources',eddbname.Text,'MySQL ODBC 3.51 Driver (32 bit)');
AppIni.WriteString(eddbname.Text,'Driver32',extractfilepath(application.ExeName)+'myodbc3.dll');

Reg.RootKey:=HKEY_LOCAL_MACHINE;
Reg.OpenKey('SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources',true)
Reg.WriteString(eddbname.Text,'MySQL ODBC 3.51 Driver');
Reg.CloseKey;

Reg.OpenKey('SOFTWARE\ODBC\ODBC.INI\'+eddbname.Text,true)
Reg.WriteString('Database',eddbname.Text);
Reg.WriteString('Description',eddescription.Text);
Reg.WriteString('Driver',extractfilepath(application.ExeName)+'myodbc3.dll');
Reg.WriteString('Option','3');
Reg.WriteString('Password',edpasswd.Text);
Reg.WriteString('Port',edport.Text);
Reg.WriteString('Server',edhost.Text);
Reg.WriteString('Stmt','');
Reg.WriteString('User',eduser.Text);
Reg.CloseKey;

Reg.OpenKey('SOFTWARE\ODBC\ODBCINST.INI\MySQL ODBC 3.51 Driver',true)
Reg.WriteString('APILevel','2');
Reg.WriteString('ConnectFunctions','YYN');
Reg.WriteString('CPTimeout','60');
Reg.WriteString('Driver',extractfilepath(application.ExeName)+'myodbc3.dll');
Reg.WriteString('DriverODBCVer','03.51');
Reg.WriteString('FileExtns','*.txt');
Reg.WriteString('FileUsage','0');
Reg.WriteString('Setup',extractfilepath(application.ExeName)+'myodbc3.dll');
Reg.WriteString('SQLLevel','1');
Reg.WriteInteger('UsageCount',2);
Reg.CloseKey;

Reg.OpenKey('SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers',true)
Reg.WriteString('MySQL ODBC 3.51 Driver','Installed');
Reg.CloseKey;

Reg.Free;
AppIni.Free;

To make it more portable & dynamic, I suggest to save each of variable parameter into an encrypted text file. Here below is the sample of my own ODBC driver custom wizard:




That’s all, dude. Once you can build your custom MySQL ODBC driver, you will never need a standard MyODBC setup wizard. So that your users will feel more comfortable to the using of the application.

Labels:

  Post a Comment