You probably know that you can use PHP as scripting language. PHP CLI (PHP Command Line Interface) is there for that. It was first released in PHP 4.2.0 as experimental, but as of PHP 4.3.0, it is fully functional and enabled by default. In this example, I will write a very simple script that will list and email me all running process on my linux box and then I will add an entry in the crontab to run the script every day at 23:00
To show all infos about running processes in linux I use:
[root@mylinux ~]#ps -auxw
So, my very simple script "send_procs.php" will be:
Try it from the command line:
Now, the script works fine, lets add an entry in the crontab. The format of an entry is:
The entry that we will add is this:
This will run the script "send_procs.php" every day at 23:00. Lets add it to the crontab:
save and quit the vi editor.
That's all. You will get an email every day at 23:00 containing all running process on your linux machine.
Conclusion
PHP as a scripting language is as powerful as perl. Try it with a real life example.
To show all infos about running processes in linux I use:
[root@mylinux ~]#ps -auxw
So, my very simple script "send_procs.php" will be:
#!/usr/bin/php -q
<?php
$result = shell_exec("ps -auxw");
mail("myname@mustap.com","Running Process at 23:00",$result);
?>
Try it from the command line:
[root@mylinux ~]#php send_procs.php
Now, the script works fine, lets add an entry in the crontab. The format of an entry is:
* * * * * command_to_be_executed
| | | | |
| | | | |_ day of the week (0-6)
| | | |__ month(1-12)
| | |____ day of the month(1-31)
|  |______ hour(0-23)
|________ minute(0-59)
The entry that we will add is this:
0 23 * * * /usr/bin/php -q /path/to/my/scripts/send_procs.php
This will run the script "send_procs.php" every day at 23:00. Lets add it to the crontab:
[root@mylinux ~]#crontab -e
0 23 * * * /usr/bin/php -q /path/to/my/scripts/send_procs.php
save and quit the vi editor.
That's all. You will get an email every day at 23:00 containing all running process on your linux machine.
Conclusion
PHP as a scripting language is as powerful as perl. Try it with a real life example.
20 Jun 2006 06:50:12
how do i do a cron job for multiple databases ,Using an array to loop through the addresses.
27 Mar 2008 09:00:56
I'm creating a GUI for crontab. How to execute a cron job???
27 Aug 2008 11:36:53
Hi mustapha,
This is really a nice tips for the beginners of PHP-Cron,
Thanks,
manoilayans