Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if cronjob exists with PHP?

Is there a way to know if a cronjob already exists with php?

I want it to work on most hostings, even shared.

like image 422
phollz Avatar asked Feb 07 '11 20:02

phollz


3 Answers

NO, There's no such direct privilege in PHP.

But (On Dedicated Server) you can write a PHP script to read /etc/crontab file and parse the info to check if a specific cron exists on the server.

like image 102
Ish Avatar answered Sep 19 '22 03:09

Ish


You could try running the crontab -l command, which should work from PHP. You might have to qualify the full path, e.g., /usr/bin/crontab -l, which would depend on the host.

This will return the crontab entries for the user who runs the command, which is what you want if the PHP page and cron jobs run as the same user (many shared hosts use setuid php scripts these days). To get the crontab entries for another user you'd need superuser rights on most systems, and crontab -l -u otheruser.

like image 42
Nathan Avatar answered Sep 23 '22 03:09

Nathan


using shell_exec() or system() or something like that might solve the problem. But it won't work with safe_mode turned on.

and i think shared hostings won't have these features enabled.

@Nathan: wouldn't /usr/bin/crontab -l be returning the crontab for the user who runs the script? e.g. www-data, wwwrun, apache or something?

like image 45
david.wosnitza Avatar answered Sep 22 '22 03:09

david.wosnitza