0

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

Community
  • 1
  • 1
Padmanathan J
  • 4,480
  • 5
  • 30
  • 73

1 Answers1

1

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.

alabama
  • 353
  • 3
  • 17
  • how can i edit my separate cron job please explain. i dont understand. – Padmanathan J Jul 17 '13 at 10:37
  • ok, you wrote your file to: "/tmp/crontab.txt"/ if you want to really edit it then read it back with: [file-get-contents](http://php.net/manual/de/function.file-get-contents.php). After that you can edit the content of the file and write it back and execute it again – alabama Jul 17 '13 at 10:40
  • I am trying this way but how can i find exact edit lines. please give me if you have any script. I have some confusions. so only i am asking – Padmanathan J Jul 17 '13 at 10:52
  • how can i get use this – Padmanathan J Jul 17 '13 at 11:06