Setting cron from PHP

Posted by k | Posted in Black Hat, Scripts | Posted on 20-11-2009

When you do a web search for cron and PHP, you always find how to run PHP from cron, but there’s no info what-so-ever on how to set cron from PHP.

Doing BH means dealing with a lot of automated tasks. Automation on PHP means cron: crons that run scripts that do web spidering, process data, scrap content, auto post, you name it.

Doing REAL BH, requires more complex scripting: scripts that dynamically change the cron: that’s to say change the cron table according to external conditions, found by the script itself.

It’s not rocket science, and you only need to know two things:

  • 1.To format a classical unix timestamp to cron, you need to use this: $cron_formated_date = date("i H d m *", $timestamp);
  • 2.To set a cron from PHP, you need to write the above + the command to call to a text file and call shell_exec ("crontab /home/user/mycronfile.txt");

Knowing those two tricks, you’re free to create anything.

Adding a new line to your actual cron table
$comandstring = $cron_formated_date." /usr/local/php5/bin/php /home/user/myphpscript.php > /home/user/myphpscriptoutput.txt 2>&1";
$actualcron = shell_exec ("crontab -l");
$newcron = $actualcron."$comandstring\n";
$f = fopen ("mycronjobtemp.txt","w");
fwrite($f, $newcron);
fclose($f);
shell_exec ("crontab /hone/user/mycronjobtemp.txt");

How do I delay my cron 15 minutes?
$temp_cron_date = date("Y-m-d H:i:s", $timestamp);
$new_cron_formated_date = date("i H d m * ", (strtotime("+15 min", strtotime($temp_cron_date))));

How do I list my cron table?
shell_exec ("crontab -l");

How do I clear my cron table?
shell_exec ("crontab -r");

How do I reset my cron table?
Save the crons you always run to a txt file and load that file to the cron table to reset it.

How do I create random crons?
I leave that to you.

Why are random crons very important?

  • 1. Because Google is smarter than ever and will detect your auto content feeding pattern. (Unless you make a clever use of WP and postpone your posts on a random base)
  • 2. Feeds where you’re getting the content from detect your pattern and ban your IP. It has happened to me reading RSS feeds from major newspapers.

Comments (2)

  1. “1. Because Google is smarter than ever and will detect your auto content feeding pattern. (Unless you make a clever use of WP and postpone your posts on a random base)”

    What about all those bloggers who write every day / week and pre-write posts and publish the same time every day?

  2. Wow, I didn’t even know you could this. This will prove useful. Bookmarked :D

Post a comment