TiBeN/CrontabManager

Run cronjob only once?

patriotaki opened this issue · 1 comments

Hello everyone,

I wanted to ask if its possible to somehow limit the cronjob to run only once? or is that the case by default?

This is how I set up the cron in the crontab, this means that the cron will run every minute, however I would like to run only one instance of each cron, is this supported somehow?
If the cronjob takes 3 minutes to complete I don't want to have 2 extra cronjobs that will overwrite the results of the first execution of the first cron job. I can obviously set the minutes to another number but that will not solve the issue, since i do not always know how much time each cron will take.

I hope my explanation is clear.

`$crontabRepository = new CrontabRepository(new CrontabAdapter());
$crontabJob = new CrontabJob();
$crontabJob->setMinutes('')
->setHours('
')
->setDayOfMonth('')
->setMonths('
')
->setDayOfWeek('*')
->setTaskCommandLine($task_cmd)
->setComments(strval($btid));

$crontabRepository->addJob($crontabJob);
$crontabJob->setEnabled(false);
$crontabRepository->persist();`

TiBeN commented

Hi @patriotaki,

This lib is nothing else than a wrapper on top on the Linux cron system. So you can use the
features of the underlaying Linux system.

In your case, to prevent many executions of the same cronjob, you can prepend your command with the flock tool.
See this SO answer for a good explanation of how to use it:
https://serverfault.com/a/82863/385266

Hope it helps.