zozo
zozo

Reputation: 8582

create cron job from another cron job

Good day to all. I need to do this:

I have root access and everything. OS is centOS 5.5 (even thought I don't think it really matters as long as crons are supported)

To be more specific I need a cron that executes a php script that gets an exact moment of time from a database (hours, minutes, seconds) when to execute another script. The database can be updated at any moment. The first job is executed once every 10 minutes.

Upvotes: 0

Views: 608

Answers (2)

Arnaud Le Blanc
Arnaud Le Blanc

Reputation: 99879

You can simply create a /etc/cron.d/generated crontab file in which your script will add generated entries.

file_put_contents('/etc/cron.d/generated', ' ... entry contents ...', FILE_APPEND');

As suggested by @The MYYN in his comment, at may be a good solution too, if you want to execute the script only once. In PHP you could invoke it like this:

system(sprintf('echo %s|at %s',
    escapeshellarg($command),
    escapeshellarg(date('H:i d.m.y', $timestamp))
);

Or like this:

$fd = popen(sprintf('at %s', escapeshellarg(date('H:i d.m.y', $timestamp))), 'w');
fwrite($fd, $command);
pclose($fd);

Upvotes: 1

BobG
BobG

Reputation: 2171

Why not use 'at'?

If I'm understanding you correctly, you're using a cron script to check the database to execute one-shot jobs - the date/time when the job is to be executed is in the database, and if you have new one-shot jobs to execute, you want to schedule them, but only execute them once.

The commands at, batch, atq, atrm manipulate the batch queue on Linux systems to execute one-shot jobs. Schedule a job with 'at', it runs, then gets deleted from the 'at' spool automatically.

Upvotes: 0

Related Questions