Is there a way to know if a cronjob already exists with php?
I want it to work on most hostings, even shared.
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.
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
.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With