How to call cron from a PHP script?

I wanted to install cron job from php script file. I can execute the php file using shell_exec () function. But Im not able to execute cron related commands. $output = shell_exec("crontab -l"); this command does not work. My cronjob is under / usr / bin / crontab. I set the file pre-logon to 777 and im executed this command using root access. still no luck. can someone help me?

+1
source share
1 answer

Your crontal -l command only displays what is planned for your user in your personal crontab. It may return an empty string, depending on your current personal crontab. Do not mix with the / etc / crontab file, which is a system-wide crontab, for all root-only users.

If you need - as it seems to me, I understand - add work to crontab from a php script, you might just want to try something like:

 $r=shell_exec('cat "30 6 * * * user my_cmd my_args" >> /etc/crontab'); 

To schedule "my_cmd my_args", for example, "user", 6:30 in the morning every day. This PHP script should be run as root, since only it can write in / etc / crontab.

Caution: I hope that your php script does not start from the website, but from the command line from the restricted environment to limit security risks, especially if you do something to start from the root. This kind of script is a very big hole in your system. Think about it. That is my advice.

By the way, / usr / bin / crontab allows:

-rwxr-sr-x 1 root crontab 35040 19 déc. 2010 / usr / bin / crontab (example from the Debian system).

0
source

All Articles