How to invoke cron job from php script?
Asked Answered
A

1

1

I wanted to set cron job from php script file. I can able to execute php file using shell_exec() function. But Im not able to run cron job related commands. $output = shell_exec("crontab -l"); this command is not working. My cronjob located under /usr/bin/crontab. I set the file premission to 777 and im executing this command with root access. still no luck. can anyone help me?

Alarcon answered 9/5, 2012 at 7:54 Comment(1)
You set the permission of /usr/bin/crontab to 777? I suggest you change it back immediately!Unbalanced
A
0

Your "crontal -l" command just displays what's scheduled for your user in its personal crontab. It might return an empty string, depending on your current personal crontab. Don't mix up with the file /etc/crontab which is a system-wide crontab, for all users, writable by root only.

If your need is - as I think I understood - to add a job in the crontab from a php script, maybe you might simply want to try something like :

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

To schedule "my_cmd my_args", running as "user", 6:30 am every day, for example. This PHP script should be started as root as only him can write in /etc/crontab.

Careful : I hope your php script is not started from a web site, but in command-line from an access-restricted environment, to limit security risks, especially if you do something to make it start as root. That kind of script is a very big hole in your system. Do consider this. That's my advice.

By the way, /usr/bin/crontab's permissions back to :

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

Azedarach answered 11/5, 2012 at 5:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.