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

6
  • does php have the correct permissions to write to the crontab file? Commented Jul 17, 2013 at 10:28
  • @jam6549 How can i set this permission and how can i access. Kindly guide me Commented Jul 17, 2013 at 10:38
  • if you are using linux, change the owner of the file to www-data: sudo chown www-data /tmp/crontab.txt Commented Jul 18, 2013 at 15:54
  • Why are you doing a echo exec('-e crontab /tmp/crontab.txt'); ? Commented Sep 11, 2013 at 11:35
  • See my Ref link @ansh0l Commented Sep 12, 2013 at 4:19

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

how can i edit my separate cron job please explain. i dont understand.
ok, you wrote your file to: "/tmp/crontab.txt"/ if you want to really edit it then read it back with: file-get-contents. After that you can edit the content of the file and write it back and execute it again
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
how can i get use this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.