Padmanathan J
Padmanathan J

Reputation: 4620

Crontab editing not working in Php script

I am creating dynamic cronjob using php script. cron job adding function is working fine for me. My script is given below.

<?
$output = shell_exec('crontab -l');
file_put_contents('/tmp/crontab.txt', $output.'* * * * * NEW_CRON'.PHP_EOL);
echo exec('crontab /tmp/crontab.txt');
?>

I need to edit separate cronjob using php. i am trying many ways but its not working. my edit cron Script isgiven below

-e (edit user's crontab)

<?
    $output = shell_exec('crontab -l');
    file_put_contents('/tmp/crontab.txt', $output.'* * * * * NEW_CRON'.PHP_EOL);
    echo exec('-e crontab /tmp/crontab.txt'); 
   ?>

My ref link LINK

How can i edit cron job using php. Please advise

Upvotes: 0

Views: 714

Answers (1)

alabama
alabama

Reputation: 422

First for the syntax: it's crontab -e With this command you open the crontab of the current user, if you want to change some cron information from a different user you have to use this syntax:

crontab -u your-user-here -e

But this opens a default text editor.

If you really want to edit the crontab of a different user with php, you have to read the content of the file, edit it and then write it back.

You must keep in mind that you can't edit all cron files, cause of user permission.

Upvotes: 1

Related Questions