简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM